Željko Filipin's Blog on Software and Testing

Test like you do not need the money.

Archive for the ‘code’ tag

Watir Has A New Method: after?

with 2 comments

Imagine that you have two links. Both links are the same.

<a>text</a>

The only thing that makes them different is that the second one is located after an image.

<a>text</a>
<img src="image.png" />
<a>text</a>

Of course, you just have to click the second link. You have already tried this.

browser.link(:text, "text").click

But, it always clicks the first link. There is a way.

browser.link(:after?, browser.image(:src, /image/)).click

This is so cool.

(You will need Watir 1.5. Bret Pettichord posted that at wtr-general.)

Written by Željko Filipin

May 25th, 2007 at 2:35 pm

Posted in Watir

Tagged with

Enter Non-English Character in Text Field

with 3 comments

google_zeljko.PNG

I use Watir a lot. I wanted to set a text field to željko. I did not know it would not be an easy task. I tried this code.

browser.text_field(:index, 1).set("željko")

But, text field was set to §eljko, Ĺľeljko, just eljko or some other string (depending if I try from irb, or from file that is saved in some encoding).

I searched wtr-general and found different solutions.

I added

require "win32ole"
WIN32OLE.codepage = WIN32OLE::CP_UTF8

and

$KCODE = 'utf8'
require 'jcode'

to the top of the file.

I tried TextField#value= instead of TextField#set.

browser.text_field(:index, 1).value=("željko")

I saved file as UTF-8.

Nothing worked. I sent my question to wtr-general and Paul Carvalho answered.

[...] I have a Watir script [...] It reads the inputs from an Excel file into an Array and then I use the array data to populate the text fields. [...] I didn’t use any special ‘require’ lines or KCodes. I just let Excel worry about holding the data [...]

I tried it, and it worked! It was simple, too. Just a few lines of code.

require 'watir'
excel = WIN32OLE::new('excel.Application')
workbook = excel.Workbooks.Open('C:\data.xls') # open file
worksheet = workbook.Worksheets(1) # the first worksheet
cell = worksheet.Range('a1')['Value'] # value of single cell
browser = Watir::IE.start('http://www.google.com/') # start IE
browser.text_field(:index, 1).set(cell) # set text field to value from cell

There is a page about scripting Excell, but it is unavailable at the moment. Fortunately, there is Google cache version.

Written by Željko Filipin

February 26th, 2007 at 3:48 pm

Posted in Watir

Tagged with

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