Pages

Search This Blog

Saturday, May 22, 2010

How to test element is editable in selenium

We can verify weather an element is editable or not using isEditable selenium command.

If your element is in below format

<input value="black" name="plain_text">

then we can use selenium isEditable to find this element is editable or not

assertTrue(selenium.isEditable("plain_text"));

<select name="plain_select"><option>Niraj</option></select>

assertTrue(selenium.isEditable("plain_select"));

<input disabled="yup" value="black" name="disabled_text">

assertTrue(!selenium.isEditable("disabled_text"));

<select disabled="yup" name="disabled_select"><option>Niraj</option></select>

assertTrue(!selenium.isEditable("disabled_select"));

How to test based on attributes of element using CSS

We can test based on attributes of elements by using css. We can use regular expression in css attributes to identify the element.
If your element is in this css format.

<div id="css3test"><a name="foobar">foobar</a><a name="barfoo">barfoo</a><a id="foobar" name="foozoobar">foozoobar</a></div>

  • How to store the text of and element which css attributes start with some specific letters.

    Ex. in above element how to store the text whose element name start with "foo"
    Selenium IDE
    
    
    <tr>
    <td>storeText</td>
    <td>css=a[name^="foo"]</td>
    <td>str</td>
    </tr>
    
    
    Selenium RC
    
    String str = selenium.getText("css=a[name^=\"foo\"]");
    
    
    This will store the "foobar" in str variable because element <a name="foobar">foobar</a> name start with "foo"

  • How to store the text of and element which css attributes ends with some specific letters.

    Ex. in above element how to store the text whose element name ends with "foo"

    Selenium IDE
    
    <tr>
    <td>storeText</td>
    <td>css=a[name$="foo"]</td>
    <td>str</td>
    </tr>
    
    
    Selenium RC
    
    String str = selenium.getText("css=a[name$=\"foo\"]");
    
    
    This will store the "barfoo" in str variable because element <a name="barfoo">barfoo</a> name ends with "foo"

  • How to store the text of and element which css attributes starts with any letters and ends with any letters but containing some specific letters in middle.

    Ex. in above element how to store the text whose element name ends with any letter and ends with any letter but containing some specific letters "zoo"

    Selenium IDE
    
    <tr>
    <td>storeText</td>
    <td>css=a[name*="zoo"]</td>
    <td>str</td>
    </tr>
    
    

    Selenium RC
    
    String str = selenium.getText("css=a[name*=\"zoo\"]");
    
    
    This will store the "foozoobar" in str variable because element <a name="foozoobar">foozoobar</a> name contains "zoo" in the middle.

    There is another method to store
    
    String str = selenium.getText("css=a:contains(\"zoo\")");
    
    

  • How to identify element using more than one css attributes.

    Lets say your element is in this format.
    
    <a alt="foo" class="a2" href="#id2" name="name1" id="id2">this is the <b>second</b> <span selenium:foo="bar">element</span></a>
    
    
    then if you want to store "this is the second element" in a variable called str the

    Selenium IDE
    
    <tr>
    <td>storeText</td>
    <td>css=a[name*="name"][alt="foo"]</td>
    <td>str</td>
    </tr>
    
    
    Selenium RC
    
    String str = selenium.getText("css=a[name*=\"name\"][alt=\"foo\"]");
    
    
    If you want to store only "element" from above element then use below code
    Selenium IDE
    
    <tr>
    <td>storeText</td>
    <td>css=a[name*="name"][alt="foo"]>span</td>
    <td>str</td>
    </tr>
    
    
    Selenium RC
    
    String str = selenium.getText("css=a[name*=\"name\"][alt=\"foo\"]>span");
    
    
  • How to test Pseudo-classes : Pseudo-elements and pseudo-classes

    Lets say your element looks like
    
    <div id="structuralPseudo">
    <span>span1</span>
    <span>span2</span>
    <span>span3</span>
    <span>span4</span>
    <div>div1</div>
    <div>div2</div>
    <div>div3</div>
    <div>div4</div>
    </div>
    
    
    to identify the element within the element we will use
    
    css=div#structuralPseudo :nth-child(n)
    
    

    1. To identify first element in pseudo element id="structuralPseudo and store its text.
    
    String str = selenium.getText("css=div#structuralPseudo :nth-child(n)");
    
    
    this will store "span1" in str variable.

    2. To identify second element in pseudo element id="structuralPseudo and store its text.
    
    String str = selenium.getText("css=div#structuralPseudo :nth-child(2n)");
    
    
    this will store "span2" in str variable.

    3. To identify third element in pseudo element id="structuralPseudo and store its text.
    
    String str = selenium.getText("css=div#structuralPseudo :nth-child(3n)");
    
    
    this will store "span3" in str variable.
    ...
    ...
    ...

    8.. To identify the last element in pseudo element id="structuralPseudo and store its text.
    
    String str = selenium.getText("css=div#structuralPseudo :nth-child(3n)");
    
    
    this will store "div4" in str variable.


    9. But you can access first and last child directly

    to access first element
    
    String str = selenium.getText("css=div#structuralPseudo :first-child");
    
    
    this will store "span1" in str variable
    To access last element
    
    String str = selenium.getText("css=div#structuralPseudo :last-child");
    
    
    this will store "div4" in str variable
    For more details about css selector please visit .

    http://www.w3.org/TR/CSS2/selector.html

    
    <input type="text" disabled="true" value="disabled" name="disabled">
    
    assertTrue(selenium.isElementPresent("css=input[type=\"text\"]:disabled"));
    
    <input type="checkbox" checked="true" value="checked" name="checked">
    
    assertTrue(selenium.isElementPresent("css=input[type=\"checkbox\"]:checked"));
    
    

  • 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();
    
    }
    }