Željko Filipin's Blog on Software and Testing

Test like you do not need the money.

Archive for the ‘code’ tag

Watir: Select Element Using Multiple Attributes

with 2 comments

There are two links in a page that I am testing.

<a href="1">reply</a>
<a href="2">reply</a>

I need to get value of href attribute for the second link.

I can not use :text, because it will return href attribute for the first link.

browser.link(:text, "reply").href
=> "{site}1"

I can use :index, but if anything on that page changes, it could break.

browser.link(:index, 2).href
=> "{site}2"

Bret Pettichord posted posted a solution for this at wtr-general a few months ago.

In 1.5, this syntax works:

browser.div(:name => 'foo', :index => 2).click

This will find the second div with the name ‘foo’.

And it really works.

browser.link(:text => "reply", :index => 2).href
=> "{site}2"

Written by Željko Filipin

February 6th, 2007 at 2:21 pm

Posted in Watir

Tagged with

Watir: Select Element Using HTML

without comments

Bret Pettichord posted this at wtr-general few days ago.

If you have a link that looks like this:

<a href="#" onclick="new Ajax.Request('/data_entry/ajax_add_term/131?contract_id=227', {asynchronous:true, evalScripts:true}); return false;">add a term</a>

you can click it using:

browser.link(:html, /contract_id=227/).click

Written by Željko Filipin

February 1st, 2007 at 11:35 pm

Posted in Watir

Tagged with

Watir: Select Element Using Custom Attribute

without comments

Paul Rogers and Prema Arya just started a very interesting thread at wtr-general.

If you have text field

<input custom_attribute="so cool" type="text" />

you can get value of custom_attribute

browser.text_field(:index, 1).attribute_value("custom_attribute")
#=> "so cool"

and you can set value of that text field using custom_attribute

browser.text_field(:xpath , "//input[@custom_attribute='so cool']/").set("even more cool")

Written by Željko Filipin

January 29th, 2007 at 5:56 pm

Posted in Watir

Tagged with