P. 1
Selenium Documentation

Selenium Documentation

|Views: 848|Likes:
Publicado porDivyesh Dave

More info:

Published by: Divyesh Dave on Mar 16, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

04/21/2011

pdf

text

original

There are multiple ways of selecting an object on a page. But what are the trade offs of each of these
locator types? Recall we can locate an object using

• the element’s ID

• the element’s name attribute

• an XPath statement

• by a links text

• document object model (DOM)

Using an element ID or name locator is the most efficient in terms of test performance, and also makes
your test code more readable, assuming the ID or name within the page source is well-named. XPath
statements take longer to process since the browser must run it’s XPath processor. Xpath has been
known to be especially slow in Internet Explorer version 7. Locating via a link’s text is often convenient
and performs well. This technique is speciic to links though. Also, if the link text is likely to change
frequently, locating by the element would be the better choice.

Sometimes though, you must use an XPath locator. If the page source does not have an ID or name
attribute you may have no choice but to use an XPath locator. (DOM locators are no longer commonly
used since Xpath can do everything they can and more. DOM locators are available simply to support
legacy tests.)

There is an advantage to using XPath that locating via ID or name attributes do not have. With XPath
(and DOM) you can locate an object with respect to another object on the page. For example, if there is
a link that must occur within the second paragraph within a

section, you can use XPath to specify
this. With ID and name locators, you can only specify that they occur on the page that is, somewhere
on the page. If you must test that an image displaying the company logo appears at the top of the page
within a header section XPath may be the better locator.

7.4.1 Locating Dynamic Elements

As was described earlier in the section on types of tests, a dynamic element is a page element who’s
identifer varies with each instance of the page. For example,

View Archived Allocation Events

This HTML anchor tag defines a button with an ID attribute of “adminHomeForm”. It’s a fairly complex
anchor tag when compared to most HTML tags, but it is still a static tag. The HTML will be the same
each time this page is loaded in the browser. Its ID remains constant with all instances of this page. That
is, when this page is displayed, this UI element will always have this Identifier. So, for your test script
to click this button you simply need to use the following selenium command.

click

adminHomeForm

Or, in Selenium 1.0

104

Chapter 7. Test Design Considerations

Selenium Documentation, Release 1.0

selenium.click( "adminHomeForm" );

Your application, however, may generate HTML dynamically where the identifier varies on different
instances of the webpage. For instance, HTML for a dynamic page element might look like this.

name= "addForm:_ID74:_ID75:0:_ID79:0:checkBox" />

This defines a checkbox. Its ID and name attributes (both addForm:_ID74:_ID75:0:_ID79:0:checkBox)
are dynamically generated values. In this case, using a standard locator would look something like the
following.

click

addForm:_ID74:_ID75:0:_ID79:0:checkBox

Or, again in Selenium-RC

selenium.click("addForm:_ID74:_ID75:0:_ID79:0:checkBox);

Given the dynamically generated Identifier, this approach would not work. The next time this page is
loaded the Identifier will be a different value from the one used in the Selenium command and therefore,
will not be found. The click operation will fail with an “element not found” error.

To correct this, a simple solution would be to just use an XPath locator rather than trying to use an ID
locator. So, for the checkbox you can simply use

click

//input

Or, if it is not the first input element on the page (which it likely is not) try a more detailed XPath
statement.

click

//input[3]

Or

click

//div/p[2]/input[3]

If however, you do need to use the ID to locate the element, a different solution is needed. You can
capture this ID from the website before you use it in a Selenium command. It can be done like this.

String[] checkboxids = selenium.getAllFields(); // Collect all input IDs on page.
for(String checkboxid:checkboxids) {
if(checkboxid.contains( "addForm" )) {
selenium.click(expectedText);

}

}

This approach will work if there is only one check box whose ID has the text ‘expectedText’ appended
to it.

7.4. Choosing a Location Strategy

105

Selenium Documentation, Release 1.0

You're Reading a Free Preview

Descarga
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->