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>