Wednesday, October 5, 2011

Selenium Locators

Locators
Selenium uses what is called locators to find and match the elements of your page that it needs to interact with.Selenium Locators can be classified into two categories:
Structure-based locators: locators that rely on the structure of the page to find elements.


  • XPath



  • DOM



  • CSS

  • Attributes-based locators: locators that relies on the attributes of the elements to locate them


  • Identifier



  • Id



  • Name



  • Link



  • CSS


  • In General there are 8 locators strategies included in Selenium:
    • Identifier
    • Id
    • Name
    • Link
    • DOM
    • XPath
    • CSS
    • UI-element


    Will look each of the locator in detail:

    1. Identifier:
    works with the id and name attributes of your html tags. Let’s consider the following example:


    <html>
    <body>
    <form id="login">
    <input name="username" type="text"/>
    <input name="password" type="password"/>
    <input name="submit" type="submit" value="Continue!"/>
    </form>
    </body>
    </html>

    Valid locators for this snippet are :
    • identifier=login
    • identifier=username
    • submit


    Id

    The Id strategy looks for an element in the page having an id attribute corresponding to the specified pattern. <label id="my_id" /> will be matched by a locator like id=my_id or just my_id

    Name

    Like the Id strategy, but on the name attribute. You can also specify a filter to refine your locator. Currently, there are two filter types :
    Value : matches elements with a name attribute and where the value follows a pattern. The following example illustrates the interest of filters :
    <html>
    <body>
    <div id="pancakes">
    <button type="button" name="pancake" value="Blueberry">Blueberry</button>
    <button type="button" name="pancake" value="Banana">Banana</button>
    <button type="button" name="pancake" value="Strawberry">Strawberry</button>
    </div>
    </body>
    </html>

    Scenario:
    we just added a strawberry pancake in our application and we want to test that the button that adds it into the cart works. With a locator like name=pancake, Selenium will find 3 elements and return the first one : the test will never fail even if the strawberry button is not here! Use a value filter like name=pancake value=Strawberry and the locator successfully identifies the Strawberry button.
    Index : same as name but works with an index. Using the previous example, the locator name=pancake index=2 will select the Strawberry button.





    Link
    This strategy is intended to select links only and selects the anchor element containing the specified text: link=The text of the link


    DOM
    The DOM strategy works by locating elements that matches the javascript expression refering to an element in the DOM of the page.
    • dom=document.div['pancakes'].button[0]
    • document.div[0].button[2]
    • dom=function foo() { return document.getElementById("pancakes"); }; foo();


    XPath
    While DOM is the recognized standard for navigation through an HTML element tree, XPath is the standard navigation tool for XML; and an HTML document is also an XML document (xHTML). XPath is used everywhere where there is XML. Valid XPath locators can be:
    • xpath=//button[@value="Blueberry"]: matches the Blueberry button
    • //div[@id="pancakes"]/button[0]: same thing


    CSS
    The CSS locator strategy uses CSS selectors to find the elements in the page. Selenium supports CSS 1 through 3 selectors


    UI-Elements
    • UI-element is a rather new locator
    • It was at first a Selenium IDE extension
    • It is now fully integrated into Selenium





    If you are not sure that you didnt got from above, below is reference:

    Element Locators

    Element Locators tell Selenium which HTML element a command refers to. The format of a locator is:
    locatorType=argument
    We support the following strategies for locating elements:
    • identifier=id: Select the element with the specified @id attribute. If no match is found, select the first element whose @name attribute is id. (This is normally the default; see below.)
    • id=id: Select the element with the specified @id attribute.
    • name=name: Select the first element with the specified @name attribute.
      • username
      • name=username
      The name may optionally be followed by one or more element-filters, separated from the name by whitespace. If the filterType is not specified, value is assumed.
      • name=flavour value=chocolate
    • dom=javascriptExpression: Find an element by evaluating the specified string. This allows you to traverse the HTML Document Object Model using JavaScript. Note that you must not return a value in this string; simply make it the last expression in the block.
      • dom=document.forms['myForm'].myDropdown
      • dom=document.images[56]
      • dom=function foo() { return document.links[1]; }; foo();
    • xpath=xpathExpression: Locate an element using an XPath expression.
      • xpath=//img[@alt='The image alt text']
      • xpath=//table[@id='table1']//tr[4]/td[2]
      • xpath=//a[contains(@href,'#id1')]
      • xpath=//a[contains(@href,'#id1')]/@class
      • xpath=(//table[@class='stylee'])//th[text()='theHeaderText']/../td
      • xpath=//input[@name='name2' and @value='yes']
      • xpath=//*[text()="right"]
    • link=textPattern: Select the link (anchor) element which contains text matching the specified pattern.
      • link=The link text
    • css=cssSelectorSyntax: Select the element using css selectors. Please refer to CSS2 selectors, CSS3 selectors for more information. You can also check the TestCssLocators test in the selenium test suite for an example of usage, which is included in the downloaded selenium core package.
      • css=a[href="#id3"]
      • css=span#firstChild + span
      Currently the css selector locator supports all css1, css2 and css3 selectors except namespace in css3, some pseudo classes(:nth-of-type, :nth-last-of-type, :first-of-type, :last-of-type, :only-of-type, :visited, :hover, :active, :focus, :indeterminate) and pseudo elements(::first-line, ::first-letter, ::selection, ::before, ::after).
    Without an explicit locator prefix, Selenium uses the following default strategies:
    • dom, for locators starting with "document."
    • xpath, for locators starting with "//"
    • identifier, otherwise

    Element Filters

    Element filters can be used with a locator to refine a list of candidate elements. They are currently used only in the 'name' element-locator.
    Filters look much like locators, ie.
    filterType=argument
    Supported element-filters are:
    value=valuePattern
    Matches elements based on their values. This is particularly useful for refining a list of similarly-named toggle-buttons.
    index=index
    Selects a single element based on its position in the list (offset from zero).

    String-match Patterns

    Various Pattern syntaxes are available for matching string values:
    • glob:pattern: Match a string against a "glob" (aka "wildmat") pattern. "Glob" is a kind of limited regular-expression syntax typically used in command-line shells. In a glob pattern, "*" represents any sequence of characters, and "?" represents any single character. Glob patterns match against the entire string.
    • regexp:regexp: Match a string using a regular-expression. The full power of JavaScript regular-expressions is available.
    • exact:string: Match a string exactly, verbatim, without any of that fancy wildcard stuff.
    If no pattern prefix is specified, Selenium assumes that it's a "glob" pattern.

    No comments:

    Post a Comment