Monday, October 12, 2009
DefaultSelenium Class
Defines an object that runs Selenium commands.
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.
o username
o 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.
o 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.
o dom=document.forms['myForm'].myDropdown
o dom=document.images[56]
o dom=function foo() { return document.links[1]; }; foo();
* xpath=xpathExpression: Locate an element using an XPath expression.
o xpath=//img[@alt='The image alt text']
o xpath=//table[@id='table1']//tr[4]/td[2]
o xpath=//a[contains(@href,'#id1')]
o xpath=//a[contains(@href,'#id1')]/@class
o xpath=(//table[@class='stylee'])//th[text()='theHeaderText']/../td
o xpath=//input[@name='name2' and @value='yes']
o xpath=//*[text()="right"]
* link=textPattern: Select the link (anchor) element which contains text matching the specified pattern.
o link=The link text
* css=cssSelectorSyntax: Select the element using css selectors. Please refer to CSS2 selectors,http://www.w3.org/TR/2001/CR-css3-selectors-20011113/ 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.
o css=a[href="#id3"]
o 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.
For a list of all members of this type, see DefaultSelenium Members.
System.Object
Wednesday, October 7, 2009
Commads for attributes
To store Source url of an image
storeAttribute //img[@alt='My Photo']@src imagePath
open ${imagePath}
Ex:
storeAttribute
//img[@alt='My Photo']@src
imagePath
open
${imagePath}
-------------------------------
1. Identified Element by its text using Contains
Ex. //a[contains(text(),'Documentation')]
storeAttribute
//a[contains(text(),'Documentation')]
Documentation
2. Get attribute of Element using storeAttribute function
Ex. //a[contains(text(),'Classic Home')]@href
storeAttribute
//a[contains(text(),'Classic Home')]@href
Documentation
Ex. //div[@id='guser']/nobr/a[2]@href
storeAttribute
//div[@id='guser']/nobr/a[2]@href
Documentation
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 html
2 body
3 form id="loginForm"
4 input name="username" type="text" /
5 input name="password" type="password" /
6 input name="password" type="password" /
7 input name="continue" type="submit" value="Login" /
8 input name="continue" type="button" value="Clear" /
9 /form
10 /body
11 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 html
2 body
3 form id="loginForm"
4 input name="username" type="text" /
5 input name="password" type="password" /
6 input name="password" type="password" /
7 input name="continue" type="submit" value="Login" /
8 input name="continue" type="button" value="Clear" /
9 /form
10 /body
11 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 html
2 body
3 form id="loginForm"
4 input name="username" type="text" /
5 input name="password" type="password" /
6 input name="password" type="password" /
7 input name="continue" type="submit" value="Login" /
8 input name="continue" type="button" value="Clear" /
9 /form
10 /body
11 html
* name=username (4)
* name=continue value=Clear (8)
* name=continue Clear (8)
* 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 UI element 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 html
2 body
3 form id="loginForm"
4 input name="username" type="text" /
5 input name="password" type="password" /
6 input name="password" type="password" /
7 input name="continue" type="submit" value="Login" /
8 input name="continue" type="button" value="Clear" /
9 /form
10 /body
11 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.
storeAttribute //img[@alt='My Photo']@src imagePath
open ${imagePath}
Ex:
-------------------------------
1. Identified Element by its text using Contains
Ex. //a[contains(text(),'Documentation')]
2. Get attribute of Element using storeAttribute function
Ex. //a[contains(text(),'Classic Home')]@href
Ex. //div[@id='guser']/nobr/a[2]@href
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 html
2 body
3 form id="loginForm"
4 input name="username" type="text" /
5 input name="password" type="password" /
6 input name="password" type="password" /
7 input name="continue" type="submit" value="Login" /
8 input name="continue" type="button" value="Clear" /
9 /form
10 /body
11 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 html
2 body
3 form id="loginForm"
4 input name="username" type="text" /
5 input name="password" type="password" /
6 input name="password" type="password" /
7 input name="continue" type="submit" value="Login" /
8 input name="continue" type="button" value="Clear" /
9 /form
10 /body
11 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 html
2 body
3 form id="loginForm"
4 input name="username" type="text" /
5 input name="password" type="password" /
6 input name="password" type="password" /
7 input name="continue" type="submit" value="Login" /
8 input name="continue" type="button" value="Clear" /
9 /form
10 /body
11 html
* name=username (4)
* name=continue value=Clear (8)
* name=continue Clear (8)
* 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 UI element 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 html
2 body
3 form id="loginForm"
4 input name="username" type="text" /
5 input name="password" type="password" /
6 input name="password" type="password" /
7 input name="continue" type="submit" value="Login" /
8 input name="continue" type="button" value="Clear" /
9 /form
10 /body
11 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.
Locating elment using 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
* 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.
/AAA/BBB[1]
Select the first BBB child of element AAA
/AAA/BBB[last()]
Select the last BBB child of element AAA
Attributes are specified by @ prefix.
//@id
Select all attributes @id
//BBB[@id]
Select BBB elements which have attribute id
//BBB[@name]
Select BBB elements which have attribute name
//BBB[@*]
Select BBB elements which have any attribute
//BBB[not(@*)]
Select BBB elements without an attribute
Values of attributes can be used as selection criteria. Function normalize-space removes leading and trailing spaces and replaces sequences of whitespace characters by a single space.
//BBB[@id='b1']
Select BBB elements which have attribute id with value b1
//BBB[normalize-space(@name)='bbb']
Select BBB elements which have attribute name with value bbb, leading and trailing spaces are removed before comparison
Function count() counts the number of selected elements
//*[count(BBB)=2]
Select elements which have two children BBB
//*[count(*)=2]
Select elements which have 2 children
Function name() returns name of the element, the starts-with function returns true if the first argument string starts with the second argument string, and the contains function returns true if the first argument string contains the second argument string.
//*[name()='BBB']
Select all elements with name BBB, equivalent with //BBB
//*[starts-with(name(),'B')]
Select all elements name of which starts with letter B
//*[contains(name(),'C')]
Select all elements name of which contain letter C
< BBB />
< BBB />
< BBB />
< BBB />
The string-length function returns the number of characters in the string. You must use < as a substitute for < and > as a substitute for > .
//*[string-length(name()) = 3]
Select elements with three-letter name
Several paths can be combined with | separator.
//CCC | //BBB
Select all elements CCC and BBB
/AAA/EEE | //BBB
Select all elements BBB and elements EEE which are children of root element AAA
/AAA/EEE | //DDD/CCC | /AAA | //BBB
Number of combinations is not restricted
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
* 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.
/AAA/BBB[1]
Select the first BBB child of element AAA
/AAA/BBB[last()]
Select the last BBB child of element AAA
Attributes are specified by @ prefix.
//@id
Select all attributes @id
//BBB[@id]
Select BBB elements which have attribute id
//BBB[@name]
Select BBB elements which have attribute name
//BBB[@*]
Select BBB elements which have any attribute
//BBB[not(@*)]
Select BBB elements without an attribute
Values of attributes can be used as selection criteria. Function normalize-space removes leading and trailing spaces and replaces sequences of whitespace characters by a single space.
//BBB[@id='b1']
Select BBB elements which have attribute id with value b1
//BBB[normalize-space(@name)='bbb']
Select BBB elements which have attribute name with value bbb, leading and trailing spaces are removed before comparison
Function count() counts the number of selected elements
//*[count(BBB)=2]
Select elements which have two children BBB
//*[count(*)=2]
Select elements which have 2 children
Function name() returns name of the element, the starts-with function returns true if the first argument string starts with the second argument string, and the contains function returns true if the first argument string contains the second argument string.
//*[name()='BBB']
Select all elements with name BBB, equivalent with //BBB
//*[starts-with(name(),'B')]
Select all elements name of which starts with letter B
//*[contains(name(),'C')]
Select all elements name of which contain letter C
< BBB />
< BBB />
< BBB />
< BBB />
The string-length function returns the number of characters in the string. You must use < as a substitute for < and > as a substitute for > .
//*[string-length(name()) = 3]
Select elements with three-letter name
Several paths can be combined with | separator.
//CCC | //BBB
Select all elements CCC and BBB
/AAA/EEE | //BBB
Select all elements BBB and elements EEE which are children of root element AAA
/AAA/EEE | //DDD/CCC | /AAA | //BBB
Number of combinations is not restricted
selenium uses XpathLibrary(".....")
selenium.useXpathLibrary has no impact on Firefox as there is a native XPath implementation available that is used by default (and btw it is also not possible to switch from native to javascript-xpath).
Regarding your page. Not only IDs are increasing XPath performance but also name and a single class field. So the XPathes
//div[@name='nameValue']
//div[@class='cssClass']
should also be performant as javascript-xpath uses document.getElementsByName() and document.getElementsByClassName() in this case. But I never tried this until now as our app always has some useful IDs (it's a Dojo app and Dojo adds some IDs), I only saw this in the source of javascript-xpath.
But I have another performance tipp - something that I am also doing in many cases. Following HTML:
...
...
We are having here a DOM with two forms (I did not include all the inputs that are in there, just something ...). The two forms are equal - something that is happening more and more offen in Web 2.0 applications where the end-user selects which content he wants to display on a page. A typical example is a weather widget that displays weather information for one location. But as users are interested in the weather in their hometown but also of there favorite holiday location they are placing the same widget two times on one page. This is difficult to differentiate when using XPathes. E. g. to access form elements we need to use the XPath:
//body/div[starts-with(@id, 'generatedId']/h2[.='Title 1']/../form/input[@name='zipCode']
//body/div[starts-with(@id, 'generatedId']/h2[.='Title 2']/../form/input[@name='zipCode']
But these are very inperformant. Especially if you need to fill other input fields and click a button and verify that some things happend you will run into hard performance issues on Internet Explorer. Therefore we need to first get the generated id and then continue:
String idTitle1 = selenium.getAttribute("//body/div[starts-with(@id, 'generatedId']/h2[.='Title 1']/..@id");
selenium.type("//div[id='" + idTitle1 + "']/form/input[@name='zipCode']", "12345");
...
Unfortunately that seems not to be something for your site but for the Selenium documentation.
Regarding your page. Not only IDs are increasing XPath performance but also name and a single class field. So the XPathes
//div[@name='nameValue']
//div[@class='cssClass']
should also be performant as javascript-xpath uses document.getElementsByName() and document.getElementsByClassName() in this case. But I never tried this until now as our app always has some useful IDs (it's a Dojo app and Dojo adds some IDs), I only saw this in the source of javascript-xpath.
But I have another performance tipp - something that I am also doing in many cases. Following HTML:
...
....
...
Title 1
Title 2
We are having here a DOM with two forms (I did not include all the inputs that are in there, just something ...). The two forms are equal - something that is happening more and more offen in Web 2.0 applications where the end-user selects which content he wants to display on a page. A typical example is a weather widget that displays weather information for one location. But as users are interested in the weather in their hometown but also of there favorite holiday location they are placing the same widget two times on one page. This is difficult to differentiate when using XPathes. E. g. to access form elements we need to use the XPath:
//body/div[starts-with(@id, 'generatedId']/h2[.='Title 1']/../form/input[@name='zipCode']
//body/div[starts-with(@id, 'generatedId']/h2[.='Title 2']/../form/input[@name='zipCode']
But these are very inperformant. Especially if you need to fill other input fields and click a button and verify that some things happend you will run into hard performance issues on Internet Explorer. Therefore we need to first get the generated id and then continue:
String idTitle1 = selenium.getAttribute("//body/div[starts-with(@id, 'generatedId']/h2[.='Title 1']/..@id");
selenium.type("//div[id='" + idTitle1 + "']/form/input[@name='zipCode']", "12345");
...
Unfortunately that seems not to be something for your site but for the Selenium documentation.
Subscribe to:
Comments (Atom)
About Me
- Niraj Kumar
- Chennai, Tamil nadu, India
- I am working as Sr. Software engineer in eBay Inc. Automation in selenium Web Driver using java, TestNG and many more..