Pages

Search This Blog

Friday, May 21, 2010

How to test the immediate element using CSS


We can test the elements and their immediate elements using CSS.
If your element is in this format.

<span id="firstChild">first child <a>second child</a></span>

  • How to store "first child" in a variable called str then use
    
    String str = selenium.getText("css=div#combinatorTest > span");
    
    
  • If you need to store the text of immediate element <a>second child</a> in a variable called str then use

    
    String str = selenium.getText("css=div#combinatorTest > span>a");
    
    
  • If you element has many value in its class then you can identify the element or you can store the element text in a variable

    <a class="class1 class2 class3">this is the element</a>

    You can use below code for verifying the element or storing the text in the element.
    Verifying element in RC
    
    verifyTrue(selenium.isElementPresent("css=a[class~=\"class2\"]"));
    
    
    Verifying element in IDE
    
    <tr>
    <td>verifyElementPresent</td>
    <td>css=a[class~="class2"]</td>
    <td></td>
    </tr>
    
    

    storing element text in a variable using RC
    
    String str = selenium.getText("css=a[class~=\"class3\"]");
    
    
    storing element text in a variable using IDE
    
    <tr>
    <td>storeText</td>
    <td>css=a[class~="class3"]</td>
    <td>str</td>
    </tr>
    
    
  • Testing preceding combinator elements

    
    <div id="combinatorTest">this is the parent element. <span id="firstChild">this is a child element <a>and grandson element</a></span>, <span>another child element</span>, <span>last child element<span></span></span></div>
    
    
    How to store the text of preceding combinator element in RC

    to store "another chile element"
    
    String str = selenium.getText("css=span#firstChild + span");
    
    
    to store "last child element"
    
    String str = selenium.getText("css=span#firstChild + span + span");
    
    
    How to store the text of preceding combinator element in IDE

    to store "another chile element"
    
    <tr>
    <td>storeText</td>
    <td>css=span#firstChild + span</td>
    <td>str</td>
    </tr>
    
    
    to store "last child element"
    
    <tr>
    <td>storeText</td>
    <td>css=span#firstChild + span + span </td>
    <td>str</td>
    </tr>
    
    
  • Thursday, May 20, 2010

    How to make selenium to wait for an element every second

    Sometimes waitForPageToLoad() and pause fails then to handle this condition we just need to wait for an element we are waiting to load on the page. Once its loaded our execution should proceed.

    
    boolean Condition = false;
    for (int second = 0; second < 60; second++) {
    try {
    
    if ((selenium.isElementPresent("//a[@id='overridelink']"))) {
    selenium.click("//a[@id='overridelink']");
    Condition = true;
    break;
    selenium.waitForPageToLoad(MAX_WAIT_TIME_IN_MS);
    
    }
    
    }
    catch (Exception ignore) {
    }
    pause(1000);
    }
    assertTrue(Condition);
    
    

    How to compare two variables in selenium

    We can compare two variable and store the result as Boolean (true or false ) in a Boolean variable. Using javascript we can compare
    
    <tr>
    <td>store</td>
    <td>5</td>
    <td>a</td>
    </tr>
    <tr>
    <td>store</td>
    <td>2</td>
    <td>b</td>
    </tr>
    <tr>
    <td>store</td>
    <td>javascript{var bool=(storedVars['a']==storedVars['b']);bool;}</td>
    <td>bool</td>
    </tr>
    
    

    Wednesday, May 19, 2010

    How to handle SSL certificate issue in Selenium / firefox

    Selenium creates a new and clean profile each time it runs a test, so it won't keep any cookies of the previous session and it wont use any browser plug-in however you can force selenium to use your own profile.

    Steps
    1. Close all instances of firefox browser.
    2. Create a new firefox profile : Go to Run and type firefox -p and press OK . This will open a dialog bod where you create new firefox profile, have a name "nk" and save it in C: drive.
    3. Install all plug-ins in this profile ; For SSL certificate issue install SSL security bypass

    https://addons.mozilla.org/en-US/firefox/addon/10246/

    4. Now you write you test cases like below

    import org.openqa.selenium.server.RemoteControlConfiguration;
    import org.openqa.selenium.server.SeleniumServer;
    import org.testng.annotations.*;
    import com.thoughtworks.selenium.*;
    import java.io.File;

    public class firefoxprofile extends SeleneseTestCase {

    private SeleniumServer seleniumServer;
    private Selenium selenium;
    public static final String MAX_WAIT_TIME_IN_MS="60000";

    @BeforeClass
    public void setUp() throws Exception {

    File template = new File("C:\\nk");
    RemoteControlConfiguration rc = new RemoteControlConfiguration();
    rc.setSingleWindow(true);
    rc.setFirefoxProfileTemplate(template);
    seleniumServer = new SeleniumServer(rc);
    selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.com/");
    seleniumServer.start();
    selenium.start();

    }


    @Test
    public void googling() {
    selenium.open("/");
    selenium.waitForPageToLoad(MAX_WAIT_TIME_IN_MS);
    selenium.type("q", "http://automationtricks.blogspot.com");
    selenium.click("btnG");
    selenium.waitForPageToLoad(MAX_WAIT_TIME_IN_MS);
    assertTrue(selenium.isTextPresent("http://automationtricks.blogspot.com"));
    }


    @AfterTest
    public void tearDown() throws InterruptedException{
    selenium.stop();
    seleniumServer.stop();

    }
    }

    How to run test cases in specified browser profile

    Selenium creates a new and clean profile each time it runs a test, so it won't keep any cookies of the previous session and it wont use any browser plug-in however you can force selenium to use your own profile.

    Steps
    1. Close all instances of firefox browser.
    2. Create a new firefox profile : Go to Run and type firefox -p and press OK . This will open a dilog bod where you create new firefox profile, have a name "nk" and save it in C: drive.
    3. Start the browser: Go to run and type firefox -p that will open a dialog box. Select the profile "NK" which you created in the 2nd step.
    4. Install all plug-ins in this profile ; like SSL security bypass

    https://addons.mozilla.org/en-US/firefox/addon/10246/

    5. Now you write you test cases like below
    
    import org.openqa.selenium.server.RemoteControlConfiguration;
    import org.openqa.selenium.server.SeleniumServer;
    import org.testng.annotations.*;
    import com.thoughtworks.selenium.*;
    import java.io.File;
    
    public class firefoxprofile extends SeleneseTestCase {
    
    private SeleniumServer seleniumServer;
    private Selenium selenium;
    public static final String MAX_WAIT_TIME_IN_MS="60000";
    
    @BeforeClass
    public void setUp() throws Exception {
    
    File template = new File("C:\\nk");
    RemoteControlConfiguration rc = new RemoteControlConfiguration();
    rc.setSingleWindow(true);
    rc.setFirefoxProfileTemplate(template);
    seleniumServer = new SeleniumServer(rc);
    selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.com/");
    seleniumServer.start();
    selenium.start();
    
    }
    
    
    @Test
    public void googling() {
    selenium.open("/");
    selenium.waitForPageToLoad(MAX_WAIT_TIME_IN_MS);
    selenium.type("q", "http://automationtricks.blogspot.com");
    selenium.click("btnG");
    selenium.waitForPageToLoad(MAX_WAIT_TIME_IN_MS);
    assertTrue(selenium.isTextPresent("http://automationtricks.blogspot.com"));
    }
    
    
    @AfterTest
    public void tearDown() throws InterruptedException{
    selenium.stop(); 
    seleniumServer.stop();
    
    }
    }
    
    

    Tuesday, May 18, 2010

    How to identify dynamic element in selenium

    Many web sites create dynamic element on their web pages where Ids of the elements gets generated dynamically. Each time id gets generated differently. So to handle this situation we use some JavaScript functions.

    starts-with

    if your dynamic element's ids have the format <button id="continue-12345" /> where 12345 is a dynamic number you could use the following
    
    XPath: //button[starts-with(@id, 'continue-')]  
    
    
    contains

    Sometimes an element gets identfied by a value that could be surrounded by other text, then contains function can be used.
    To demonstrate, the element <input class="top suggest business"> can be located based on the ‘suggest’ class without having to couple it with the ‘top’ and ‘business’ classes using the following
    
    XPath: //input[contains(@class, 'suggest')].
    
    

    How to use Css instead of Xpath in selenium

    Sometimes selenium gets confused to identify an element on the web page because more than one elements have the same name and ids so to handle this situation we instructs selenium to identify the object using CSS instead of Xpath.

    
    <tr>
    <td>open</td>
    <td>http://economictimes.indiatimes.com/</td>
    <td></td>
    </tr>
    <tr>
    <td>type</td>
    <td>css=input[style='width: 120px; font-size: 12px;']</td>
    <td>Business</td>
    </tr>
    
    
    In selenium RC you can use below code
    
    selenium.open("http://economictimes.indiatimes.com/");
    
    selenium.type("css=input[style='width: 120px; font-size: 12px;']", "Business");
    
    
    There is another way to use css instead of Xpath

    Try below code for clicking on search button on www.google.com

    
    <tr>
    <td>type</td>
    <td>q</td>
    <td>www.automationtricks.blogspot.com</td>
    </tr>
    <tr>
    <td>click</td>
    <td>css=span.ds > span.lsbb > input.lsb</td>
    <td></td>
    </tr>