My Blog List

Wednesday, November 9, 2011

Selenium Rare Tips - Part I

Hi all,
Here I am going to list out some rare and useful tips for selenium users.


1. Start/Shutdown SeleniumServer in proper way
  1. public static void startSeleniumServer() throws Exception {    
  2.     try {
  3.         ServerSocket serverSocket = new ServerSocket(RemoteControlConfiguration.DEFAULT_PORT);
  4.         serverSocket.close();
  5.        
  6.                 //Server not up, start it
  7.                 try {
  8.                          RemoteControlConfiguration rcc = new RemoteControlConfiguration();
  9.                          rcc.setPort(RemoteControlConfiguration.DEFAULT_PORT);
  10.                          server = new SeleniumServer(false, rcc);    
  11.                 } catch (Exception e) {
  12.                         System.err.println("Could not create Selenium Server because of: " + e.getMessage());
  13.                         e.printStackTrace();
  14.                 }
  15.                
  16.                 try {
  17.                         server.start();
  18.                         System.out.println("Server started");
  19.                 } catch (Exception e) {
  20.                         System.err.println("Could not start Selenium Server because of: " + e.getMessage());
  21.                         e.printStackTrace();
  22.                 }
  23.                
  24.         } catch (BindException e) {
  25.                 System.out.println("Selenium server already up, will reuse...");
  26.         }
  27. }

  1. public void tearDown() throws Exception {
  2.     selenium.stop();
  3.     if(seleniumServer != null) {
  4.         try {
  5.             selenium.shutDownSeleniumServer();
  6.             seleniumServer .stop();
  7.             seleniumServer = null;
  8.         }  catch(Exception e) {
  9.             e.printStackTrace();
  10.         }
  11.     }
  12. }

OR you want to shoudown via Browser, use below command
http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer


2. To Scroll by postion
selenium.getEval("window.scrollBy(0,200)");
--- Here 0 and 200 are x and y postions to scroll


3. Get No of CheckBoxes Checked/Unchecked
  1. selenium.getXpathCount("//input[@type='checkbox' and @checked]");
  2. selenium.getXpathCount("//input[@type='checkbox' and not(@checked)]");
    Also You can use like
    css=input[type=\"checkbox\"]:checked

4. Drop-down lists handling
To get selected value:
  1. selenium.isElementPresent("//select[@name='listbox']/option[contains(text(),'item2')]")
  2. selenium.getSelectedValue("name=listbox");

To get list items:
String[] items = selenium.getSelectOptions("name=listbox");


To set or select a item on dropdown:
selenium.select("listbox""label=item1");         //For Simple Select List

There is a select list, which contains a text box and down arrow image button. Clicking on button image, opens a drop down which contains element with dynamic id's.
Dynamic elements are : drop down elements (div's) , and image button.
This can be done by 
selenium.click("//input[@id='listbox']/following-sibling::img");
This clicks on image. Now we need to select the dropdown list element, which also has dynamic id.
So, I have used text function as,
selenium.click("//div[text()='item1']");            //which clicks on "item1" in the drop-down list.
Also have a look at this link Select an option from an ExtJS ComboBox


5. HTTP to HTTPS problems
http://qaselenium.blogspot.com/2011/04/how-to-handle-https-and-security-popups.html

6. Locate Elements by position
If you have 2 buttons on a page. Both have same ID, type, and available attributes are same. How you will identify which element to click?. Here is the answer.

  1. //input[@id='button' and position()=1] OR //input[@id='button']  //1st button
  2. //input[@id='button' and position()=2]    //2nd button

7. XPath Type : Functions
Always we have chances to meet locator problems while elements with same attributes, ids, names, etc. These kind of scenarios can be solved by using below techniques. Normally we can use 4 ways to identify the elements.
1. String
2. Boolean
3. Number
4. Node-Set
Here they are,
  1. Node-Set: last(), position(), count(), id(), local-name(), namespace-uri(), name()
  2. String: string(), concat(), starts-with(), contains(), substring-before(), substring-after(), substring(), string-length(), normalize-space(), translate()
  3. Boolean: boolean(), not(), true(), false(), lang()
  4. Number: number(), sum(), floor(), ceiling(), round()
Here are some examples.
  1. selenium.click("xpath=(//input[@type='checkbox'])[last()]");
  2. selenium.click("xpath=(//input[@type='submit'])[last()-1]");
  3. selenium.click("xpath=(//input[@type='checkbox'])[position()=2]");

3 comments:

  1. Wonderful blog & good post.Its really helpful for me, awaiting for more new post. Keep Blogging!




    Selenium Training in Chennai

    ReplyDelete
  2. Excellent post. Keep update your blog. Suppose if anyone interested to learn Selenium Training in Chennai, reach us besant technologies. They provide real time training for all trainees.

    ReplyDelete
  3. Thanks for sharing great information in your blog. Got to learn new things from your Blog . It was very nice blog to learn about Selenium

    ReplyDelete