Module: Watir::Simple

Defined in:
lib/watir/watir_simple.rb

Overview

revision: $Revision$

Instance Method Summary collapse

Instance Method Details

#assert_image_with_id(id, mesg = nil) ⇒ Object

Make a Test::Unit assertion that an image exists on the page with the given id attribute. (Preferred method)

  • mesg - String - An optional assertion-failed message.



443
444
445
446
447
448
449
# File 'lib/watir/watir_simple.rb', line 443

def assert_image_with_id(id, mesg=nil)
  if mesg.nil? then
    assert( get_image_with_id(id) != nil, "image with id: [#{id}] is not present")
  else
    assert( get_image_with_id(id) != nil, mesg)
  end
end

#assert_image_with_src(src, mesg = nil) ⇒ Object

Make a Test::Unit assertion that an image exists on the page with the given src attribute.

  • mesg - String - An optional assertion-failed message.



430
431
432
433
434
435
436
# File 'lib/watir/watir_simple.rb', line 430

def assert_image_with_src(src, mesg=nil)
  if mesg.nil? then
    assert( get_image_with_src(src) != nil, "image with src: [#{src}] is not present")
  else
    assert( get_image_with_src(src) != nil, mesg)
  end
end

#assert_text_in_body(text, mesg = nil) ⇒ Object

Make a Test::Unit assertion that the given text appears in the text body.

  • text - String or RegExp - The text or regular expression to search for.

  • mesg - String - An optional assertion-failed message.



274
275
276
277
278
279
280
# File 'lib/watir/watir_simple.rb', line 274

def assert_text_in_body(text,mesg=nil)
  if mesg.nil? then
    assert(@@browser.pageContainsText(text), "couldn't find in body: [#{text}]")
  else
    assert(@@browser.pageContainsText(text), mesg)
  end
end

#assert_text_in_combobox_by_id(id, text, mesg = nil) ⇒ Object

This method returns true|false if the text is selected in the combobox with the supplied id.

  • id - string - Id of field to examine.

  • text - string/Array of Strings - The string or array of strings to search for.

  • mesg - Optional! string - Set this if you want to supply your own error message



356
357
358
# File 'lib/watir/watir_simple.rb', line 356

def assert_text_in_combobox_by_id(id, text, mesg=nil)
  assert_text_in_combobox_wrapper(:id, name, text, mesg)
end

#assert_text_in_combobox_by_name(name, text, mesg = nil) ⇒ Object

This method returns true|false if the text is selected in the combobox with the supplied name.

  • name - string - Name of field to examine.

  • text - string/Array of Strings - The string or array of strings to search for.

  • mesg - Optional! string - Set this if you want to supply your own error message



335
336
337
# File 'lib/watir/watir_simple.rb', line 335

def assert_text_in_combobox_by_name(name, text, mesg=nil)
  assert_text_in_combobox_wrapper(:name, name, text, mesg)
end

#assert_text_in_combobox_wrapper(how, what, text, mesg = nil) ⇒ Object

  • how - symbol - the way we look for the object. Supported values are

    - :name
    - :id
    - :index
    
  • what - string - What field, id or name to examine.

  • text - string/Array of Strings - The string or array of strings to search for.

  • mesg - Optional! string - Set this if you want to supply your own error message



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/watir/watir_simple.rb', line 304

def assert_text_in_combobox_wrapper(how, what, text, mesg=nil)
  assert(@@browser.selectBox(how, what), "could not find a combobox with what: #{what} and how: #{how}")
  selectedItems = @@browser.selectBox(how, what).getSelectedItems
  
  if text.kind_of? String
    if mesg.nil? then
      assert(selectedItems[0] == text, "couldn't find text in combobox with #{how}: #{what} - [#{text}], had [#{selectedItems[0]}]")
    else
      assert(selectedItems[0] == text, mesg)
    end
    
  elsif text.kind_of? Array
    if mesg.nil? then
      text.each do |item|
        assert(selectedItems.include?(item), "couldn't find text in combobox  with #{how}: #{what} - [#{text}], had [#{selectedItems}]")
      end
    else
      text.each do |item|
        assert(selectedItems.include?(item), mesg)
      end
    end
  end
end

#assert_text_in_field(name, text, mesg = nil) ⇒ Object

This method returns true|false if the text/reg exp supplied is in a the text field “name”.

  • name - String - Name of field to examine.

  • text - String or RegExp - The text or regular expression to search for.

  • mesg - String - An optional assertion-failed message.



288
289
290
291
292
293
294
# File 'lib/watir/watir_simple.rb', line 288

def assert_text_in_field(name, text, mesg=nil)
   if mesg.nil? then
     assert(@@browser.textField(:name, name).verify_contains(text), "couldn't find in field #{name}: [#{text}]")
   else
     assert(@@browser.textField(:name, name).verify_contains(text), mesg)
   end
end

#assert_text_not_in_body(text, mesg = nil) ⇒ Object

Make a Test::Unit assertion that the given text does not appear in the text body.

  • mesg - An assertion-failed message.



260
261
262
263
264
265
266
# File 'lib/watir/watir_simple.rb', line 260

def assert_text_not_in_body(text,mesg=nil)
  if mesg.nil? then
    assert_false( @@browser.pageContainsText(text), "found in body: [#{text}]")
  else
    assert_false( @@browser.pageContainsText(text), mesg)
  end
end

#click_button_with_caption(caption) ⇒ Object

Tell the browser to click on a form button with the specified caption text.



245
246
247
# File 'lib/watir/watir_simple.rb', line 245

def click_button_with_caption(caption)
  wait_before_and_after {  @@browser.button(:caption, caption).click }
end

#click_button_with_id(id) ⇒ Object

Tell the browser to click on a form button with the specified id attribute.



233
234
235
# File 'lib/watir/watir_simple.rb', line 233

def click_button_with_id(id)
  wait_before_and_after {  @@browser.button(:id, id).click }
end

#click_button_with_index(index) ⇒ Object

Tell the browser to click on the index‘th form button on the page.



251
252
253
# File 'lib/watir/watir_simple.rb', line 251

def click_button_with_index(index)
  wait_before_and_after {  @@browser.button(:index, index).click }
end

#click_button_with_name(name) ⇒ Object

Tell the browser to click on a form button with name.



227
228
229
# File 'lib/watir/watir_simple.rb', line 227

def click_button_with_name(name)
   wait_before_and_after {  @@browser.button(:name, name).click }
end

#click_button_with_value(value) ⇒ Object

Tell the browser to click on a form button with the specified value attribute.



239
240
241
# File 'lib/watir/watir_simple.rb', line 239

def click_button_with_value(value)
  wait_before_and_after {  @@browser.button(:value, value).click }
end

Tell the browser to click on the first link with the specified id attribute (the preferred method.)



136
137
138
# File 'lib/watir/watir_simple.rb', line 136

def click_link_with_id(id)
   wait_before_and_after { @@browser.link(:id, id).click }
end

Tell the browser to click on the specified link as determined by the sequential ordering of links on the document.



149
150
151
# File 'lib/watir/watir_simple.rb', line 149

def click_link_with_index(index)
   wait_before_and_after { @@browser.link(:index, index).click }
end

Tell the browser to click on the first link with the specified name attribute



142
143
144
# File 'lib/watir/watir_simple.rb', line 142

def click_link_with_name(name)
   wait_before_and_after { @@browser.link(:name, name).click }
end

Tell the browser to click on the first link with the specified text in the link body.



156
157
158
# File 'lib/watir/watir_simple.rb', line 156

def click_link_with_text(text)
   wait_before_and_after { @@browser.link(:text, text).click }
end

Tell the browser to click on the first link with the specified URL. This takes the address of the link instead of the text displayed.

  • url - can be a string to match exactly, or a regular expression.

Example:

click_link_with_url "http://google.com"

or:

click_link_with_url /goo*/


129
130
131
# File 'lib/watir/watir_simple.rb', line 129

def click_link_with_url(url)
   wait_before_and_after { @@browser.link(:url, url).click }
end

#close_browserObject

Close the browser window. Useful for automated test suites to reduce test interaction.



364
365
366
367
# File 'lib/watir/watir_simple.rb', line 364

def close_browser
  @@browser.getIE.quit
  sleep 2
end

#combobox_default_selection(name) ⇒ Object



414
415
416
417
# File 'lib/watir/watir_simple.rb', line 414

def combobox_default_selection(name)
   # FIXME _where_ is this used?

  @@browser.selectBox(:name, name).value
end

#count_instances_of(text) ⇒ Object

Returns the number of times text appears in the body text of the page.



421
422
423
# File 'lib/watir/watir_simple.rb', line 421

def count_instances_of(text)
  @@browser.getDocument.body.innerText.scan(text).size
end

#enter_text_into_field_with_id(id, text) ⇒ Object

Set the text of the field with a given id attribute (the preferred method.) This only types characters into the field and does not submit the form.



170
171
172
# File 'lib/watir/watir_simple.rb', line 170

def enter_text_into_field_with_id(id, text)
   wait_before_and_after { @@browser.textField(:id, id).set(text) }
end

#enter_text_into_field_with_index(index, text) ⇒ Object

Set the text of the indexed field. This only types characters into the field and does not submit the form.



177
178
179
# File 'lib/watir/watir_simple.rb', line 177

def enter_text_into_field_with_index(index, text)
   wait_before_and_after { @@browser.textField(:index, index).set(text) }
end

#enter_text_into_field_with_name(name, text) ⇒ Object

Set the text of the field with a given name (the preferred method.) This only types characters into the field and does not submit the form.



163
164
165
# File 'lib/watir/watir_simple.rb', line 163

def enter_text_into_field_with_name(name, text)
   wait_before_and_after { @@browser.textField(:name, name).set(text) }
end

#fill_text_field(field_name, text) ⇒ Object

Fill a single textfield with a value



401
402
403
# File 'lib/watir/watir_simple.rb', line 401

def fill_text_field(field_name, text)
  @@browser.textField(:name, field_name).set(text)
end

#fill_text_fields(data) ⇒ Object

Fill a series of text fields. This takes a hash of textfield names to values for those fields.

Example:

fill_text_fields {
  'username' => 'joe',
  'password' => 'blahblah',
  'email' => '[email protected]',
  'favorite_num' => 42
}


393
394
395
396
397
# File 'lib/watir/watir_simple.rb', line 393

def fill_text_fields(data)
  data.each do |field, value|
    @@browser.textField(:name, field).set(value)
  end
end

#go_backObject

Tell the browser to cick the Back button.



371
372
373
# File 'lib/watir/watir_simple.rb', line 371

def go_back
  @@browser.back
end

#go_forwardObject

Tell the browser to cick the Forward button.



377
378
379
# File 'lib/watir/watir_simple.rb', line 377

def go_forward
  @@browser.forward
end

Tell the browser to load a particular URL.



64
65
66
# File 'lib/watir/watir_simple.rb', line 64

def navigate_to(url)
  @@browser.goto url
end

Much like click_link_with_id but navigates to link instead of clicking it, thereby not invoking OnClick for links.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/watir/watir_simple.rb', line 97

def navigate_to_link_with_id(id)
  # FIXME: this should be moved into Watir!

  wait_before_and_after do
    doc = @@browser.getDocument
    links = doc.links
    link = nil
    links.each do |n|
      match = false
      case id
      when Regexp
        match = (n.invoke("id") =~ id)
      when String
        match = (n.invoke("id") == id)
      end
      if match
        link = n
        break
      end
    end
    raise "Couldn't find link with id #{id}" unless link
    @@browser.goto link
  end
end

Much like click_link_with_url but navigates to link instead of clicking it, thereby not invoking OnClick for links.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/watir/watir_simple.rb', line 71

def navigate_to_link_with_url(url)
  # FIXME: this should be moved into Watir!

  wait_before_and_after do
    doc = @@browser.getDocument
    links = doc.links
    link = nil
    links.each do |n|
      match = false
      case url
      when Regexp
        match = (n.invoke("href") =~ url)
      when String
        match = (n.invoke("href") == url)
      end
      if match
        link = n
        break
      end
    end
    raise "Couldn't find link with url #{url}" unless link
    @@browser.goto link
  end
end

#new_browser_at(url) ⇒ Object

Open up a browser and point it at a certain URL.



56
57
58
59
60
# File 'lib/watir/watir_simple.rb', line 56

def new_browser_at(url)
  @@browser = IE.new
  @@browser.typingspeed = 0
  @@browser.goto url
end

#select_from_combobox_with_id(id, text) ⇒ Object

Select an item from a selectbox (a.k.a “combo box”, or “pulldown”) The selectbox is chose by matching its id attribute. The item is selected based on the text content of <OPTION> tags.



193
194
195
# File 'lib/watir/watir_simple.rb', line 193

def select_from_combobox_with_id(id, text)
  wait_before_and_after {  @@browser.selectBox(:id, id).select(text) }
end

#select_from_combobox_with_index(index, text) ⇒ Object

Select an item from a selectbox (a.k.a “combo box”, or “pulldown”) The selectbox is chose by matching its order of appearance in the document. The item is selected based on the text content of <OPTION> tags.



202
203
204
# File 'lib/watir/watir_simple.rb', line 202

def select_from_combobox_with_index(index, text)
  wait_before_and_after {  @@browser.selectBox(:index, index).select(text) }
end

#select_from_combobox_with_name(name, text) ⇒ Object

Select an item from a selectbox (a.k.a “combo box”, or “pulldown”) The selectbox is chose by matching its name attribute. The item is selected based on the text content of <OPTION> tags.



185
186
187
# File 'lib/watir/watir_simple.rb', line 185

def select_from_combobox_with_name(name, text)
  wait_before_and_after {  @@browser.selectBox(:name, name).select(text) }
end

#select_radio_button_with_id(id, value) ⇒ Object

Select an item (value) from the radio button collection with a matching id attribute.



221
222
223
# File 'lib/watir/watir_simple.rb', line 221

def select_radio_button_with_id(id, value)
   wait_before_and_after {  @@browser.radio(:id, id, value).click }
end

#select_radio_button_with_name(index, value) ⇒ Object

Select an item (value) from the index‘th radio button collection.



208
209
210
# File 'lib/watir/watir_simple.rb', line 208

def select_radio_button_with_name(name, value)
  wait_before_and_after {  @@browser.radio(:name, name, value).click }
end

#wait_before_and_afterObject

A convenience method to wait at both ends of an operation for the browser to catch up.



454
455
456
457
458
# File 'lib/watir/watir_simple.rb', line 454

def wait_before_and_after
  wait_for_browser
  yield
  wait_for_browser
end

#wait_for_browserObject

Some browsers (i.e. IE) need to be waited on before more actions can be performed. Most action methods in Watir::Simple already call this before and after.



409
410
411
# File 'lib/watir/watir_simple.rb', line 409

def wait_for_browser
  @@browser.waitForIE
end