Thursday, December 17, 2009

How Selenium recognises the objects

Locating Elements

For many Selenium commands a target is required. This target identifies an element in the content of the web application, and consists of the location strategy followed by the location in the format locatorType=location. The locator type can be omitted and one of the default strategies will be used depending on the initial characters of the location. The various locator types are explained below with examples for each.

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 element with the @id attribute value matching the location will found. If no element has a matching @id attribute then the first element with a @name attribute matching the location will be found.

  • identifier=loginForm
  • identifier=username
  • identifier=continue

For instance, your page source could have identifier (ID) and name attributes as follows:




id="loginForm">
    name="username" type="text" />
    name="password" type="password" />
    name="continue" type="submit" value="Login" />
    name="continue" type="button" value="Continue" />

Locating by id

More limited than the identifier locator type but also more explicit. Use this when you know an element’s @id attribute.

  • id=loginForm

   id="loginForm">
    name="username" type="text" />
    name="password" type="password" />
    name="continue" type="submit" value="Login" />
    name="continue" type="button" value="Continue" />

Locating by name

Similar to the identifier locator type when an @id attribute is not found, 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).

  • name=username
  • name=continue
  • name=continue Continue
  • name=continue value=Continue
  • name=continue type=button

   id="loginForm">
    name="username" type="text" />
    name="password" type="password" />
    name="continue" type="submit" value="Login" />
    name="continue" type="button" value="Continue" />

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 or similar.

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.

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.

  • xpath=/html/body/form[1] - Absolute path (would break if the HTML was changed only slightly)
  • xpath=//form[1] - First form element in the HTML
  • xpath=//form[@id=’loginForm’] - The form element with @id of ‘loginForm’
  • xpath=//form[input/@name=’username’] - First form element with an input child element with @name of ‘username’
  • xpath=//input[@name=’username’] - First input element with @name of ‘username’
  • xpath=//form[@id=’loginForm’]/input[1] - First input child element of the form element with @id of ‘loginForm’
  • xpath=//input[@name=’continue’][@type=’button’] - Input with @name ‘continue’ and @type of ‘button’
  • xpath=//form[@id=’loginForm’]/input[4] - Fourth input child element of the form element with @id of ‘loginForm’

   id="loginForm">
    name="username" type="text" />
    name="password" type="password" />
    name="continue" type="submit" value="Login" />
    name="continue" type="button" value="Continue" />

There are also a couple of very useful Firefox Add-ons that can assist in discovering the XPath of an element:

  • XPath Checker - suggests XPath and can be used to test XPath results.
  • Firebug - very useful, XPath suggestions are just one of the many powerful features of this add-on.

Locating hyperlinks by link text

This is a simple method of locating a hyperlink in your web page by using the text of the link. If two links with the same text are present then the first match will be used.

  • link=Continue
  • link=Cancel

  

Are you sure you want to do this?

   href="continue.html">Continue
   href="cancel.html">Cancel

Locating by DOM

The Document Object Model represents a HTML document and can be accessed using JavaScript. This location strategy takes JavaScript that evaluates to an element on the page, which can be simply the element’s location using the hierarchical dotted notation.

  • dom=document.getElementById(‘loginForm’)
  • dom=document.forms[‘loginForm’]
  • dom=document.forms[0]
  • dom=document.forms[0].username
  • dom=document.forms[0].elements[‘username’]
  • dom=document.forms[0].elements[0]
  • dom=document.forms[0].elements[3]

   id="loginForm">
    name="username" type="text" />
    name="password" type="password" />
    name="continue" type="submit" value="Login" />
    name="continue" type="button" value="Continue" />

You can use Selenium itself as well as other sites and extensions to explore the DOM of your web application. A good reference exists on W3Schools.

Locating by CSS

The “AndWait” commands

The difference that any user should see between a command and it’s AndWaitclick) will do the action and continue with the following command as fast as it can. While the AndWaitclickAndWait) tells Selenium to wait for the page to load after the action has been done. alternative is that the regular command (e.g. alternative (e.g.

The andWait alternative is always used when an action causes the browser to navigate to another page or reload the present one.

No comments:

Post a Comment