Archive

Archive for the ‘watir’ Category

Watir, find input elements by (dynamically generated) id

June 24th, 2010 dwright 1 comment

problem space:
web testing, using watir. Need to find the status of a radio button which has a dynamically generated id

html:

<div id="9_status_544_654321">
<!-- On -->
<input type="radio" id="9__status_544_on__654321" value="on"/>
<!-- Off -->
<input type="radio" id="9__status_544_off__654321" value="off"/>
</div>

watir:
(we know the first and last values in the string, but not the middle value,
i.e. 9__status_(?)_off__654321)

   id1, id2 = 9, 654321
    re = Regexp.new("#{id1}__status_\\d+_on__#{id2}")
    p @ie.radio(:id, re).text   # prints 'checked'
    p @ie.radio(:id, /re/).text # also prints 'checked'
Categories: watir Tags:

watir checkbox clear vs checked? vs value

June 18th, 2010 dwright No comments

Geez, what an unpleasant surprise this was.

Problem space:
Use Watir to test if a checkbox is checked or not and to uncheck it (clear).

Confession, I am actually now using Celerity in place of Watir. (So this could behavior may be with Celerity, not watir, I have not confirmed yet)

html:
<input type="checkbox" name="is_moveable" id="is_moveable" checked="yes"/>

celerity:
p @ie.checkbox(:name, 'is_moveable').value # returns 'on'
p @ie.checkbox(:name, 'is_moveable').checked? # returns true
assert @ie.checkbox(:name, 'is_moveable').clear # uncheck it
p @ie.checkbox(:name, 'is_moveable').value # returns 'on'
p @ie.checkbox(:name, 'is_moveable').checked? # returns false

I would think that if you cleared a checkbox with no value, that the value would be 'off', but I 'assume' the default value is 'on', so that is still the value returned.

In this situation use 'checked?' instead of 'value'

element_by_xpath with two attributesx

March 30th, 2010 dwright No comments

Watir can not access the 'for' attribute of the 'label' element. Here is a work around.

html: <label class="error" generated="true" for="partner_status">This field is required.</label>

xpath: lpe = "//label[@class='error' and @for='partner_status']"
p @ie.element_by_xpath(lpe).text

will print: 'This field is required.'

Categories: ruby, watir Tags: ,

watir div content by class

December 11th, 2009 dwright No comments

Here are 3 identical ways to capture the content in a div by only the class name.

<div class="error">error message here</div>

@ie.div(:class, 'error').text
@ie.div(:class, 'error').innerText
@ie.element_by_xpath("//div[@class='error']").text

all the above return 'error message here'

Categories: watir Tags:

watir advanced xpath onclick example

December 7th, 2009 dwright 3 comments

problem space:
you are trying to click a (click-able) link element, with no real discerning qualities. (i.e. there is no name, no id, a generic css class same as the other links on the page, etc. Luckily, In this case there is a javascript onclick handler which is unique to this element.

<a class="showhide" href="javascript:void(0)" onclick="setDisplay('notes','block');">show

you could accomplish this two ways. (that I know of)
1. find the number of the link in relation to the page, (i.e. it's the 13th)
ie.link(:index, 13).click
2. specify an xpath.
ie.link(:xpath, "//a[contains(@onclick, \"setDisplay('notes','block')\")]").click

(I just also, 'discovered' that firebug (Firefox JavaScript debugging extension) offers an xpath to elements, (in the right click). it offered this: (untested)
/html/body/div[2]/table/tbody/tr/td/table/tbody/tr/td/div/form/table/tbody/tr/td/div[2]/table[7]/tbody/tr[3]/td/a

ref: groups.google.com/watir-general

Categories: ruby, watir Tags:

watir, ie, utf8, latin1 extended, html form input

July 27th, 2009 dwright No comments

Problem space:
Testing web applications on windows via watir on IE.
(i.e. how to input latin1 extended characters into html form elements)

Need to test form input such as:

str = 'Iñtërnâtiônàlizætiøn éàáéè碣¥¤'
assert @ie.text_field(:name, 'cat_name').set(str)

This works for me:

require ‘watir’
require 'win32ole'
require 'jcode'

# make utf8 html form input work
WIN32OLE.codepage = WIN32OLE::CP_UTF8
$KCODE = "u"

str = 'Iñtërnâtiônàlizætiøn éàáéè碣¥¤' (is all latin1 extended)
assert @ie.text_field(:name, 'cat_name').set(str)

# this however doesn't:
str = 'Iñtërnâtiônàlizætiøn 蒂蒀蒁蒃蒄 éàáéè碣¥¤' # latin1 ext with utf8

I would have to do the excel trick mentioned in the article. (haven't been able try yet, missing excel license)

You can of course skip all this and specify the actual code points of each character, such as: Vid\351o (Vidéo) or “\xc3\xa6″‘ (æ) but that get's really tedious for any non trivial international strings ;)

ref: http://zeljkofilipin.com/2007/02/26/enter-non-english-character-in-text-field

nutshell:
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
ie = Watir::IE.start(”http://www.google.com/”) # start IE
ie.text_field(:index, 1).set(cell) # set text field to value from cell

watir and ajax

June 8th, 2009 dwright No comments

So, reminder to myself. Keep in mind that an if you are checking the functionality on an 'onClick' or some seemingly innocent request which is behind the scenes really an ajax request, you have to keep in mind, ajax is a HTTP post request and will need time to return.

example.

Watir: ajax, proving certain states.

So, you fill out a form and hit the 'Add' button, which then
should produce another form element.

assert @ie.button(:value, "Add").click
assert @ie.select_list(:name, 'added_type').value == 'added'

but this fails, stating select_list does not exist.

You probably need to wait till the call has been processed.

i.e.
assert @ie.button(:value, "Add").click
Watir::Waiter.wait_until{@ie.select_lists.length > 2} # or whatever the state change will produce
assert @ie.select_list(:name, 'added_type').value == 'added'

another example:
assert @ie.button(:value, "Add").click
Watir::Waiter.wait_until{@ie.contains_text('Review Info')}
assert !@ie.contains_text('Available Reviewed Information')

Categories: watir Tags: ,

watir select elements

June 8th, 2009 dwright No comments

I seem to always have to look this up, so adding a quick reminder here

When checking for the lack of a select form element (or value in one)

I would think I could do:

assert_false(
@ie.select_list(:name, 'pid').select_item_in_select_list(:text, site)
)

as in, "I cannot select the following value in the select list" or if not that, at least:
assert_false @ie.select_list(:name, 'pid')
as in, "there is not a select list with name 'pid'.

but these will fail with something such as:

Watir::Exception::NoValueFoundException: No option with text of,...

Here is another, (slightly misleading) example using the handy, Watir::Waiter.wait_until.

1) Error:
test_01_add_credit_consumers(TestManualCreditNoMerchant):
Watir::Exception::UnknownObjectException: Unable to locate element, using :name, "pid"
c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.2/lib/watir/element.rb:52:in `assert_exists'
c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.2/lib/watir/input_elements.rb:63:in `select_item_in_select_list'


if site and not site.empty?
 Watir::Waiter.wait_until{@ie.select_list(:name, 'pid')}
 #apparently, this doesn't do what i think it should !?
 assert(
  @ie.select_list(:name, 'pid').
 select_item_in_select_list(:text, site)
 )
end

# find website
if site and not site.empty?
 Watir::Waiter.wait_until{@ie.select_list(:name, 'pid').exists?}
 #now this does what i think it should no more Exception
 assert(
  @ie.select_list(:name, 'pid').select_item_in_select_list(:text, site)
 )
end

synopsis:
Watir:
Checking that a select_list does not exist.

I would have thought, I could do
assert ! @ie.select_list(:name, 'rebate_type')

But this does not work. Then I tried putting that within
a begin/rescue block, which didn't work either.

Luckily, this works great:
assert_false @ie.select_list(:name, 'rebate_type').exists?

Categories: watir Tags:

watir: handle javascript confirm popups

March 27th, 2009 dwright No comments

Watir 1.6.2
IE 6

recently I had a rough time getting javascript confirmation popup's interaction to work, although, I know it must, as there is a unit test for it. (C:\ruby\lib\ruby\gems\1.8\gems\watir-1.6.2\unittests\popups_test.rb)

There is also a page of potential solutions, although, the few I tried did not work for me (watir javascript pop up page)

which I couldn't run, because the shipped unit tests don't work 'the easy way' in 1.6, see Run+the+Watir+Unit+Tests

I would have had to get the tree from github and build it, details:
Building+Watir
which, I didn't really have the time to do this morning.)

I had to modify the code from the unit test to get it to work in my test. (I run Win XP in Parrallels from Mac OSX, it's possible that has something to do with it not working as advertised)

Anyway, short story, I used '"start ruby" instead of "start rubyw"'
ruby.exe and rubyw.exe are ruby interpreters for windows
the difference is rubyw.exe doesn't launch a DOS shell to run.
(ref: Running Ruby Under Windows)
Yes, it's uglier and less convenient to have the dos window pop up but at this time, I'm just psyched to have my tests passing

Example:

html:
-------
<html>
<head>
<script type="text/javascript" language="javascript"> 

document.write('Hello World!');
function process(args){
   document.write("You said " + args)
}

</script>
</head>

<body>
<form>
<input type="button" value="Delete"
    on Click="return confirm('Really, Delete?') && process('OK')"
</form>

</body>
</html>

ruby/watir code:
------------------
  startClicker("OK")
  @ie.button(:value, 'Delete').click
  @ie.contains_text('Successfully  removed!')

  def startClicker( button , waitTime = 0.5)
    w = WinClicker.new
    jsd = 'c:\\ruby\\lib\\ruby\\gems\\1.8\\gems\\WATIR-~1.2\\lib\\watir\\clickJSDialog.rb'
    c = "start ruby #{jsd } #{button } #{ waitTime} "
    puts "Starting #{c}"
    w.winsystem(c )
    w = nil
  end
Categories: watir Tags:

watir table().row_values as array indices

March 16th, 2009 dwright No comments

Using 'table.row_values(2) I would like to get array as a result.

for example, given a table of

<table>
 <tbody>
  <tr>
   <td>Name</td>
   <td></td>
  </tr>
  <tr>
   <td>value</td>
   <td>Select List (Item1, Item2, Item3)</td>
 </tr>
</table>

I would like

ie.table(:index, 1).row_values(2)

to return an array of element value per indices, like so:

["Name", "\r\n", "value", " - Select - Item1, Item2, Item3"]

Then I could do something like:

ie.table(:index, 1).row_values(2)[1]

to get

'value'

currently the result is returned as an one element array. Which is not helpful for matching a specific value to a specific row.
i.e.

["Name: \r\nInformatique \r\n - Select - Item1, Item2, Item3"]

In order to test specific rows for specific values I have been using something such as:

ie.table(:index, 1).row_values(2).to_s.split(/\r\n/)[1].match("value")

=> # (Note, the above code is all untested, just an example of the top of my head)

----------------------------------------------------------

EDIT: I need to amend the above post which is incorrect.

The behavior I expect is the behavior provided, the key is making sure you
specify the correct table ;) If you do, the results are returned as array elements.

If you specify an index of a parent table by mistake, then row values are returned as an array as expected,
but will include any nested tables row values in the content.

Hopefully, this example, which is tested, will demonstrate what I mean.

<table border=1>
 <tbody>
  <tr>
   <td>
   Contacts
    <table>
     <tbody>
      <tr>
       <td>
        <table border=1>
         <tbody>
          <tr>
           <td>First</td>
           <td>Last</td>
           <td>Addr1</td>
           <td>City</td>
          </tr>
          <tr>
           <td>Billy</td>
           <td>McLilly</td>
           <td>123 Street</td>
           <td>My Town</td>
         </tr>
          <tr>
           <td>Jan</td>
           <td>Smith</td>
           <td>456 B Street</td>
           <td>Your Town</td>
         </tr>
        </table>
        </td>
        </tr>
    </table>
    </td>
    </tr>
    </table>
irb(main):067:0> ie.table(:index, 1).row_values(1)
=> ["Contacts FirstLastAddr1City\r\nBillyMcLilly123 StreetMy Town\r\nJanSmith456
 B StreetYour Town"]
irb(main):068:0> ie.table(:index, 2).row_values(1)
=> ["FirstLastAddr1City\r\nBillyMcLilly123 StreetMy Town\r\nJanSmith456 B Street
Your Town"]
irb(main):069:0> ie.table(:index, 3).row_values(1)
=> ["First", "Last", "Addr1", "City"]
irb(main):070:0> ie.table(:index, 3).row_values(2)
=> ["Billy", "McLilly", "123 Street", "My Town"]
irb(main):071:0> ie.table(:index, 3).row_values(3)
=> ["Jan", "Smith", "456 B Street", "Your Town"]
irb(main):073:0> ie.table(:index, 3).row_values(2)[0]

=> "Billy"
Categories: watir Tags: