Java Reference
In-Depth Information
Here's a brief sample of how tests can manipulate page elements:
■
Call
click
and
doubleClick
to click an element and get the resulting page.
■
Call
check
and
uncheck
to toggle a radio button or check box.
■
Call
type
to set the value of an input field.
We now look at the different ways to access elements.
12.8.4
Accessing elements with references
In our first example, we saw
HTML
elements referred to by locators. Selenium pro-
vides a
String
format to address elements with different schemes. The two locators we
saw are the default
scheme
,
id
, and
link
used to find anchor elements. The format for
API
arguments that take a locator is
LocatorType=Argument
. Table 12.3 describes the
different locators and their formats.
Table 12.3
Locators and their formats
Locator type
Argument
Example
W3C CSS2 and CSS3 selectors
css
css=a[href="#AnId"]
dom
JavaScript expression
dom=document.forms['f1'].intxt
id
@id
attribute
id=AnId
identifier
@id
attribute or the first element
where the
@name
attribute
equals
id
identifier=AnId
link
Anchor element matching the text
link=I'm feeling lucky
name
First element with the
@name
attribute
name=lucky
ui
Uses a Selenium
UI-Element
ui=loginPages::loginButton()
XPath expression
xpath
xpath=//*[text()="lucky"]
To get the value of a field, for example, you'd write
String actualMsg = selenium.getText("name=serverMessage");
If you run into
XP
ath cross-browser compatibility issues, you can either refactor tests
with browser-specific
XP
ath expressions or call
allowNativeXpath(false)
to force
expressions to be evaluated in Selenium's JavaScript library.
12.8.5
Failing tests with exceptions
Although a generated test method throws an exception, it doesn't throw any checked
exceptions, nor do
API
s you use to write tests. The generated code and
API
s throw
unchecked exceptions to make sure your tests fail under the proper conditions. Even