WORKING WITH QTP

TestEveryThinG

Selenium Command:

Posted by rajivkumarnandvani on September 20, 2009

1.    Identified Element by its text using Contains

Ex.   //a[contains(text(),’Documentation’)]

<td>storeAttribute</td>

<td>//a[contains(text(),’Documentation’)]</td>

<td>Documentation</td>

2.    Get attribute of Element using  storeAttribute function

Ex.       //a[contains(text(),’Classic Home’)]@href

<td>storeAttribute</td>

<td>//a[contains(text(),’Classic Home’)]@href</td>

<td>Documentation</td>

Ex.       //div[@id=’guser’]/nobr/a[2]@href

<td>storeAttribute</td>

<td>//div[@id=’guser’]/nobr/a[2]@href</td>

<td>Documentation</td>

3.    Identified Element by its Atrribute

Ex.           //a[@onclick=”]

Ex.        //div[@id=’guser’]/nobr/a[2]

Ex.      //div/div[position()=’1′ and@style=’font-size: 80%;’]/a[position()=’2′ and @href=’/search’]

Ex.    //input[@id=’q’ and @type =’text’]

4.    Locating by Identifier

This is probably the most common method of locating elements and is the catch-all default when no recognised locator type is used. With this strategy, the first element with the id attribute value matching the location will be used. If no element has a matching id attribute, then the first element with an name attribute matching the location will be used.

For instance, your page source could have id and name attributes as follows:

1
2
3
4
5
6
7
8
9
<html>
 <body>
  <form id="loginForm">
   <input name="username" type="text" />
   <input name="password" type="password" />
   <input name="continue" type="submit" value="Login" />
  </form>
 </body>
<html>

The following locator strategies would return the elements from the HTML snippet above indicated by line number:

  • identifier=loginForm (3)
  • identifier=username (4)
  • identifier=continue (5)
  • continue (5)

Since the identifier type of locator is the default, the identifier= in the first three examples above is not necessary.

Locating by Id

This type of locator is more limited than the identifier locator type, but also more explicit. Use this when you know an element’s id attribute.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
 <html>
  <body>
   <form id="loginForm">
    <input name="username" type="text" />
    <input name="password" type="password" />
    <input name="continue" type="submit" value="Login" />
    <input name="continue" type="button" value="Clear" />
   </form>
  </body>
 <html>
  • id=loginForm (3)

Locating by Name

The name locator type will locate the first element with a matching name attribute. If multiple elements have the same value for a name attribute, then you can use filters to further refine your location strategy. The default filter type is value (matching the value attribute).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
 <html>
  <body>
   <form id="loginForm">
    <input name="username" type="text" />
    <input name="password" type="password" />
    <input name="continue" type="submit" value="Login" />
    <input name="continue" type="button" value="Clear" />
   </form>
 </body>
 <html>
  • name=username (4)
  • name=continue value=Clear (7)
  • name=continue Clear (7)
  • name=continue type=button (7)

Note

Unlike some types of XPath and DOM locators, the three types of locators above allow Selenium to test a UI element independent of its location on the page. So if the page structure and organization is altered, the test will still pass. One may or may not want to also test whether the page structure changes. In the case where web designers frequently alter the page, but its functionality must be regression tested, testing via id and name attributes, or really via any HTML property, becomes very important.

Locating by XPath

XPath is the language used for locating nodes in an XML document. As HTML can be an implementation of XML (XHTML), Selenium users can leverage this powerful language to target elements in their web applications. XPath extends beyond (as well as supporting) the simple methods of locating by id or name attributes, and opens up all sorts of new possibilities such as locating the third checkbox on the page.

One of the main reasons for using XPath is when you don’t have a suitable id or name attribute for the element you wish to locate. You can use XPath to either locate the element in absolute terms (not advised), or relative to an element that does have an id or name attribute. XPath locators can also be used to specify elements via attributes other than id and name.

Absolute XPaths contain the location of all elements from the root (html) and as a result are likely to fail with only the slightest adjustment to the application. By finding a nearby element with an id or name attribute (ideally a parent element) you can locate your target element based on the relationship. This is much less likely to change and can make your tests more robust.

Since only xpath locators start with “//”, it is not necessary to include the xpath= label when specifying an XPath locator.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
 <html>
  <body>
   <form id="loginForm">
    <input name="username" type="text" />
    <input name="password" type="password" />
    <input name="continue" type="submit" value="Login" />
    <input name="continue" type="button" value="Clear" />
   </form>
 </body>
 <html>
  • xpath=/html/body/form[1] (3) – Absolute path (would break if the HTML was changed only slightly)
  • //form[1] (3) – First form element in the HTML
  • xpath=//form[@id=’loginForm’] (3) – The form element with @id of ‘loginForm’
  • xpath=//form[input/\@name=’username’] (4) – First form element with an input child element with @name of ‘username’
  • //input[@name=’username’] (4) – First input element with @name of ‘username’
  • //form[@id=’loginForm’]/input[1] (4) – First input child element of the form element with @id of ‘loginForm’
  • //input[@name=’continue’][@type=’button’] (7) – Input with @name ‘continue’ and @type of ‘button’
  • //form[@id=’loginForm’]/input[4] (7) – Fourth input child element of the form element with @id of ‘loginForm’
  • Expresion in square brackets can further specify an element. A number in the brackets gives the position of the element in the selected set. The function last() selects the last element in the selection.
Advertisements

2 Responses to “Selenium Command:”

  1. One Two Three

    You can also access any of those three cells using XPath – here is a way that should work in IE and Firefox:

    browser.cell(:xpath, “//td[.=’Three’]”)

    The dot being compared to ‘Three’ represents the text of the cell.

    And if the cell text has leading or trailing whitespace, you can strip it from the text before doing the comparison:

    browser.cell(:xpath, “//td[normalize-space(.)=’Three’]”)
    //exact match text
    browser.cell(:xpath,”//td[@class=’v-menubar-menuitem-caption’ and text()=’Offers’]”)
    // contain text
    browser.cell(:xpath,”//td[@class=’v-menubar-menuitem-caption’ and contains(text(),’Offers’)]”)

    Thanks

  2. div class=”criteria tab-history selected” id=”history”>
    div class=”criteria tab-results” id=”results”>
    div class=”criteria tab-suggested” id=”suggested”>

    here i have to select the tab which is currently selected by user

    so i have used the xpath for this

    xpath: //div[contains(@class,’selected’) and contains(@class,’criteria tab’)]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: