Skip to content
This repository has been archived by the owner on Jun 6, 2018. It is now read-only.

Finding Page Elements

Justin Ko edited this page Jul 8, 2013 · 1 revision

Finding Page Elements

Not all HTML tags will look the same. If you have difficulty getting Watir to interact with an object on your web page, try the Watir method with different attributes. The tag may come with one attribute, or it may come with several. If you still have trouble, see if the developers will add a id attribute to the offending tag.

Choosing Attributes

If you are having difficulty getting Watir to interact with an object, try using the method with different tag attributes. Tag declarations in HTML are varied. Some developers use tools that generate HTML, and there are many variations on tag attributes. Watir is very flexible and offers several variations on each method. There are also attributes that are part of the DOM within Internet Explorer. Anything the DOM can see, you can access using Ruby.

Accessing Tags Using Alternate Methods

In some situations, you may not know what the tag attributes of an HTML object are. They may be missing, or the tag attributes might be dynamically generated. Some web architectures dynamically generate tags, and the attributes might change each time you run a test. To work around this, try the following.

CSS

Watir supports div tags. Some web applications use one div for one state of the application, and another for a different state (such as error or success messages). Watir can check the status of the div to allow for tests on pages using CSS as part of the application.

Index

Elements on a web page can be accessed by where they appear on a page. If an element you want to interact with always appears in the same place, you can access it by a one-based index. Here's an example using a radio list:

What you see in the web browser:

Radio 1:
Radio 2:
Radio 3:

Watir code to set the first radio list item using its index:

ie.radio(:index, 1).set

Watir code to clear the second radio list item using its index:

ie.radio(:index, 2).clear

IRB

See Using IRB

Multiple Attributes

I have more than one object of the same name. How do I commit an action on anything but the first one?

Lets imagine we have two 'Employees' links. This will click the first link that is found on the page:

$ie.link(:text, 'Employees').click

Use this syntax for the second (or more)

$ie.link(:text => 'Employees', :index  => 2).click

Regular Expressions

Watir supports regular expressions in method calls.

Here is an example using the link method to click a link using the url attribute:

ie.link(:url, /shtml/).click

This will click the link that matches shtml.

How do I use regular expressions for object recognition?

Watir allows the tester to use Regular Expressions for object recognition instead of using the full string literal. This is useful for HTML objects that are dynamically created.

For example in my WebSphere application we may have a link with this URL

 $ie.link(:url, "javascript:PC_7_0_G7_selectTerritory('0',%20'amend')").click

However this: 'PC_7_0_G7_' will change dependant on the environment.

With RegEx we could find that link by doing :

$ie.link(:url, /selectTerritory\('0',%20'amend'\)/).click

or this

$ie.link(:url, /javascript.*selectTerritory\('0',%20'amend'\)/).click

note: '.*' will match any character or digit any number of times.

RegEx contains special characters that need to be escaped. For example here ')' we use the backward slash to escape a closing bracket.

To find out what characters need to be escaped, go into irb, then enter

Regexp.escape "javascript:PC_7_0_G7_selectTerritory('0',%20'amend')"

the escape sequences will be returned

=> "javascript:PC_7_0_G7_selectTerritory\\('0',%20'amend'\\)"

note: use only one backslash; we are shown two, because they are escaped within a string.