Module: WatirPageHelper::ClassMethods

Defined in:
lib/watir-page-helper.rb,
lib/watir-page-helper/generated.rb

Overview

A collection of class methods that generate helper methods that are mixed into a calling class.

Instance Method Summary collapse

Instance Method Details

#button(name, identifier = nil, &block) ⇒ Nil

Generates two button methods to:

  • click a button;

  • return the button element.

button :info, :text => “Information” page.info page.info_button.exist?.should be_true

Examples:

Specify a button to generate methods

Parameters:

  • name (Symbol)

    The name of the button element (used to generate the methods)

  • identifier (optional, Hash) (defaults to: nil)

    A set of key, value pairs to identify the element

  • block

Returns:

  • (Nil)


317
318
319
320
321
322
# File 'lib/watir-page-helper/generated.rb', line 317

def button name, identifier=nil, &block
  define_method(name) do
    self.send("#{name}_button").click
  end
  create_element_getter "#{name}_button", identifier, __method__, block
end

#cell(name, identifier = nil, &block) ⇒ Nil

Generates two row methods to:

  • return the text from a table cell;

  • return the cell element.

Examples:

Specify a cell to generate methods

cell(:test_table_row_1_cell_1) { |test_table_row_1 | test_table_row_1.td }
page.test_table_row_1_cell_1.should == "Test Table Col 1"
page.test_table_row_1_cell_1_cell.exist?.should be_true

Parameters:

  • name (Symbol)

    The name of the cell element (used to generate the methods)

  • identifier (optional, Hash) (defaults to: nil)

    A set of key, value pairs to identify the element

  • block

Returns:

  • (Nil)


187
188
189
190
191
192
# File 'lib/watir-page-helper.rb', line 187

def cell name, identifier=nil, &block
  define_method(name) do
    self.send("#{name}_cell").text
  end
  create_element_getter "#{name}_cell", identifier, 'td', block
end

#checkbox(name, identifier = nil, &block) ⇒ Nil

Generates four checkbox methods to:

  • check the checkbox;

  • uncheck the checkbox;

  • see whether a the checkbox is checked; and

  • return the checkbox element.

Examples:

Specify a checkbox to generate methods

checkbox :agree, :name => "agree"
page.check_agree
page.agree?.should be_true
page.uncheck_agree
page.agree?.should be_false
page.agree_checkbox.exist?.should be_true

Parameters:

  • name (Symbol)

    The name of the checkbox element (used to generate the methods)

  • identifier (optional, Hash) (defaults to: nil)

    A set of key, value pairs to identify the element

  • block

Returns:

  • (Nil)


110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/watir-page-helper.rb', line 110

def checkbox name, identifier=nil, &block
  define_method("check_#{name}") do
    self.send("#{name}_checkbox").set
  end
  define_method("uncheck_#{name}") do
    self.send("#{name}_checkbox").clear
  end
  define_method("#{name}?") do
    self.send("#{name}_checkbox").set?
  end
  create_element_getter "#{name}_checkbox", identifier, __method__, block
end

#dd(name, identifier = nil, &block) ⇒ Nil

Generates two dd methods to:

  • return the text from a dd;

  • return the dd element.

dd :my_element, :id => “my_element” page.my_element.should == “My Element Text” page.my_element_dd.exist?.should be_true

Examples:

Specify a dd to generate methods

Parameters:

  • name (Symbol)

    The name of the dd element (used to generate the methods)

  • identifier (optional, Hash) (defaults to: nil)

    A set of key, value pairs to identify the element

  • block

Returns:

  • (Nil)


217
218
219
220
221
222
# File 'lib/watir-page-helper/generated.rb', line 217

def dd name, identifier=nil, &block
  define_method(name) do
    self.send("#{name}_dd").text
  end
  create_element_getter "#{name}_dd", identifier, __method__, block
end

#direct_url(url) ⇒ Nil

Provides a way to define a direct URL for a page, and creates a method for the page to go to that URL.

Examples:

Set the direct URL for the Google Home Page

direct_url "http://www.google.com"
page.goto # navigates to the Google URL

Parameters:

  • url (String)

    the URL to directly access the page

Returns:

  • (Nil)


56
57
58
59
60
# File 'lib/watir-page-helper.rb', line 56

def direct_url url
  define_method("goto") do
    @browser.goto url
  end
end

#div(name, identifier = nil, &block) ⇒ Nil

Generates two div methods to:

  • return the text from a div;

  • return the div element.

div :my_element, :id => “my_element” page.my_element.should == “My Element Text” page.my_element_div.exist?.should be_true

Examples:

Specify a div to generate methods

Parameters:

  • name (Symbol)

    The name of the div element (used to generate the methods)

  • identifier (optional, Hash) (defaults to: nil)

    A set of key, value pairs to identify the element

  • block

Returns:

  • (Nil)


137
138
139
140
141
142
# File 'lib/watir-page-helper/generated.rb', line 137

def div name, identifier=nil, &block
  define_method(name) do
    self.send("#{name}_div").text
  end
  create_element_getter "#{name}_div", identifier, __method__, block
end

#dl(name, identifier = nil, &block) ⇒ Nil

Generates two dl methods to:

  • return the text from a dl;

  • return the dl element.

dl :my_element, :id => “my_element” page.my_element.should == “My Element Text” page.my_element_dl.exist?.should be_true

Examples:

Specify a dl to generate methods

Parameters:

  • name (Symbol)

    The name of the dl element (used to generate the methods)

  • identifier (optional, Hash) (defaults to: nil)

    A set of key, value pairs to identify the element

  • block

Returns:

  • (Nil)


197
198
199
200
201
202
# File 'lib/watir-page-helper/generated.rb', line 197

def dl name, identifier=nil, &block
  define_method(name) do
    self.send("#{name}_dl").text
  end
  create_element_getter "#{name}_dl", identifier, __method__, block
end

#dt(name, identifier = nil, &block) ⇒ Nil

Generates two dt methods to:

  • return the text from a dt;

  • return the dt element.

dt :my_element, :id => “my_element” page.my_element.should == “My Element Text” page.my_element_dt.exist?.should be_true

Examples:

Specify a dt to generate methods

Parameters:

  • name (Symbol)

    The name of the dt element (used to generate the methods)

  • identifier (optional, Hash) (defaults to: nil)

    A set of key, value pairs to identify the element

  • block

Returns:

  • (Nil)


237
238
239
240
241
242
# File 'lib/watir-page-helper/generated.rb', line 237

def dt name, identifier=nil, &block
  define_method(name) do
    self.send("#{name}_dt").text
  end
  create_element_getter "#{name}_dt", identifier, __method__, block
end

#expected_element(type, identifier, timeout = 30) ⇒ Nil

Creates a method that provides a way to initialize a page based upon an expected element. This is useful for pages that load dynamic content.

Examples:

Specify a text box as expected on the page within 10 seconds

expected_element(:text_field, :name => "firstname", 10)
page.expected_element

Parameters:

  • type (Symbol)

    the type of element you are expecting

  • identifier (Hash)

    the name, value pair used to identify the object

  • timeout (optional, Integer) (defaults to: 30)

    default value is 30 seconds

Returns:

  • (Nil)


43
44
45
46
47
# File 'lib/watir-page-helper.rb', line 43

def expected_element type, identifier, timeout=30
  define_method("expected_element") do
    @browser.send("#{type.to_s}", identifier).wait_until_present timeout
  end
end

#expected_title(expected_title) ⇒ Nil

Creates a method that compares the expected_title of a page against the actual.

Examples:

Specify ‘Google’ as the expected title of a page

expected_title "Google"
page.has_expected_title?

Parameters:

  • expected_title (String)

    the literal expected title for the page

  • expected_title (Regexp)

    the expected title pattern for the page

Returns:

  • (Nil)

Raises:

  • An exception if expected_title does not match actual title



26
27
28
29
30
31
# File 'lib/watir-page-helper.rb', line 26

def expected_title expected_title
  define_method("has_expected_title?") do
    has_expected_title = expected_title.kind_of?(Regexp) ? expected_title =~ @browser.title : expected_title == @browser.title
    raise "Expected title '#{expected_title}' instead of '#{@browser.title}'" unless has_expected_title
  end
end

#file_field(name, identifier = nil, &block) ⇒ Nil

Generates two file_field methods to:

  • return the text from a file field;

  • set a file field

file_field :upload, :id => ‘upload’

page.upload = image_path #set
page.upload.should == image_path #check

Examples:

Specify a file_field to generate methods

Parameters:

  • name (Symbol)

    The name of the file field element (used to generate the methods)

  • identifier (optional, Hash) (defaults to: nil)

    A set of key, value pairs to identify the element

  • block

Returns:

  • (Nil)


208
209
210
211
212
213
214
215
216
# File 'lib/watir-page-helper.rb', line 208

def file_field name, identifier=nil, &block
  define_method(name) do
    self.send("#{name}_file_field").value
  end
  define_method("#{name}=") do |value|
    self.send("#{name}_file_field").set value
  end
  create_element_getter "#{name}_file_field", identifier, __method__, block
end

#form(name, identifier = nil, &block) ⇒ Nil

Generates two form methods to:

  • return the text from a form;

  • return the form element.

form :my_element, :id => “my_element” page.my_element.should == “My Element Text” page.my_element_form.exist?.should be_true

Examples:

Specify a form to generate methods

Parameters:

  • name (Symbol)

    The name of the form element (used to generate the methods)

  • identifier (optional, Hash) (defaults to: nil)

    A set of key, value pairs to identify the element

  • block

Returns:

  • (Nil)


257
258
259
260
261
262
# File 'lib/watir-page-helper/generated.rb', line 257

def form name, identifier=nil, &block
  define_method(name) do
    self.send("#{name}_form").text
  end
  create_element_getter "#{name}_form", identifier, __method__, block
end

#frame(name, identifier = nil, &block) ⇒ Nil

Generates a frame method to return a frame element.

Examples:

Specify a frame to generate a method

table :my_element, :id => 'my_element'
page.my_element.exists?.should be_true

Parameters:

  • name (Symbol)

    The name of the frame element (used to generate the method)

  • identifier (optional, Hash) (defaults to: nil)

    A set of key, value pairs to identify the element

  • block

Returns:

  • (Nil)


384
385
386
# File 'lib/watir-page-helper/generated.rb', line 384

def frame name, identifier=nil, &block
  create_element_getter "#{name}", identifier, __method__, block
end

#h1(name, identifier = nil, &block) ⇒ Nil

Generates two h1 methods to:

  • return the text from a h1;

  • return the h1 element.

h1 :my_element, :id => “my_element” page.my_element.should == “My Element Text” page.my_element_h1.exist?.should be_true

Examples:

Specify a h1 to generate methods

Parameters:

  • name (Symbol)

    The name of the h1 element (used to generate the methods)

  • identifier (optional, Hash) (defaults to: nil)

    A set of key, value pairs to identify the element

  • block

Returns:

  • (Nil)


17
18
19
20
21
22
# File 'lib/watir-page-helper/generated.rb', line 17

def h1 name, identifier=nil, &block
  define_method(name) do
    self.send("#{name}_h1").text
  end
  create_element_getter "#{name}_h1", identifier, __method__, block
end

#h2(name, identifier = nil, &block) ⇒ Nil

Generates two h2 methods to:

  • return the text from a h2;

  • return the h2 element.

h2 :my_element, :id => “my_element” page.my_element.should == “My Element Text” page.my_element_h2.exist?.should be_true

Examples:

Specify a h2 to generate methods

Parameters:

  • name (Symbol)

    The name of the h2 element (used to generate the methods)

  • identifier (optional, Hash) (defaults to: nil)

    A set of key, value pairs to identify the element

  • block

Returns:

  • (Nil)


37
38
39
40
41
42
# File 'lib/watir-page-helper/generated.rb', line 37

def h2 name, identifier=nil, &block
  define_method(name) do
    self.send("#{name}_h2").text
  end
  create_element_getter "#{name}_h2", identifier, __method__, block
end

#h3(name, identifier = nil, &block) ⇒ Nil

Generates two h3 methods to:

  • return the text from a h3;

  • return the h3 element.

h3 :my_element, :id => “my_element” page.my_element.should == “My Element Text” page.my_element_h3.exist?.should be_true

Examples:

Specify a h3 to generate methods

Parameters:

  • name (Symbol)

    The name of the h3 element (used to generate the methods)

  • identifier (optional, Hash) (defaults to: nil)

    A set of key, value pairs to identify the element

  • block

Returns:

  • (Nil)


57
58
59
60
61
62
# File 'lib/watir-page-helper/generated.rb', line 57

def h3 name, identifier=nil, &block
  define_method(name) do
    self.send("#{name}_h3").text
  end
  create_element_getter "#{name}_h3", identifier, __method__, block
end

#h4(name, identifier = nil, &block) ⇒ Nil

Generates two h4 methods to:

  • return the text from a h4;

  • return the h4 element.

h4 :my_element, :id => “my_element” page.my_element.should == “My Element Text” page.my_element_h4.exist?.should be_true

Examples:

Specify a h4 to generate methods

Parameters:

  • name (Symbol)

    The name of the h4 element (used to generate the methods)

  • identifier (optional, Hash) (defaults to: nil)

    A set of key, value pairs to identify the element

  • block

Returns:

  • (Nil)


77
78
79
80
81
82
# File 'lib/watir-page-helper/generated.rb', line 77

def h4 name, identifier=nil, &block
  define_method(name) do
    self.send("#{name}_h4").text
  end
  create_element_getter "#{name}_h4", identifier, __method__, block
end

#h5(name, identifier = nil, &block) ⇒ Nil

Generates two h5 methods to:

  • return the text from a h5;

  • return the h5 element.

h5 :my_element, :id => “my_element” page.my_element.should == “My Element Text” page.my_element_h5.exist?.should be_true

Examples:

Specify a h5 to generate methods

Parameters:

  • name (Symbol)

    The name of the h5 element (used to generate the methods)

  • identifier (optional, Hash) (defaults to: nil)

    A set of key, value pairs to identify the element

  • block

Returns:

  • (Nil)


97
98
99
100
101
102
# File 'lib/watir-page-helper/generated.rb', line 97

def h5 name, identifier=nil, &block
  define_method(name) do
    self.send("#{name}_h5").text
  end
  create_element_getter "#{name}_h5", identifier, __method__, block
end

#h6(name, identifier = nil, &block) ⇒ Nil

Generates two h6 methods to:

  • return the text from a h6;

  • return the h6 element.

h6 :my_element, :id => “my_element” page.my_element.should == “My Element Text” page.my_element_h6.exist?.should be_true

Examples:

Specify a h6 to generate methods

Parameters:

  • name (Symbol)

    The name of the h6 element (used to generate the methods)

  • identifier (optional, Hash) (defaults to: nil)

    A set of key, value pairs to identify the element

  • block

Returns:

  • (Nil)


117
118
119
120
121
122
# File 'lib/watir-page-helper/generated.rb', line 117

def h6 name, identifier=nil, &block
  define_method(name) do
    self.send("#{name}_h6").text
  end
  create_element_getter "#{name}_h6", identifier, __method__, block
end

#image(name, identifier = nil, &block) ⇒ Nil

Generates a image method to return a image element.

Examples:

Specify a image to generate a method

table :my_element, :id => 'my_element'
page.my_element.exists?.should be_true

Parameters:

  • name (Symbol)

    The name of the image element (used to generate the method)

  • identifier (optional, Hash) (defaults to: nil)

    A set of key, value pairs to identify the element

  • block

Returns:

  • (Nil)


371
372
373
# File 'lib/watir-page-helper/generated.rb', line 371

def image name, identifier=nil, &block
  create_element_getter "#{name}", identifier, __method__, block
end

#li(name, identifier = nil, &block) ⇒ Nil

Generates two li methods to:

  • return the text from a li;

  • return the li element.

li :my_element, :id => “my_element” page.my_element.should == “My Element Text” page.my_element_li.exist?.should be_true

Examples:

Specify a li to generate methods

Parameters:

  • name (Symbol)

    The name of the li element (used to generate the methods)

  • identifier (optional, Hash) (defaults to: nil)

    A set of key, value pairs to identify the element

  • block

Returns:

  • (Nil)


277
278
279
280
281
282
# File 'lib/watir-page-helper/generated.rb', line 277

def li name, identifier=nil, &block
  define_method(name) do
    self.send("#{name}_li").text
  end
  create_element_getter "#{name}_li", identifier, __method__, block
end

Generates two link methods to:

  • click a link;

  • return the link element.

link :info, :text => “Information” page.info page.info_link.exist?.should be_true

Examples:

Specify a link to generate methods

Parameters:

  • name (Symbol)

    The name of the link element (used to generate the methods)

  • identifier (optional, Hash) (defaults to: nil)

    A set of key, value pairs to identify the element

  • block

Returns:

  • (Nil)


297
298
299
300
301
302
# File 'lib/watir-page-helper/generated.rb', line 297

def link name, identifier=nil, &block
  define_method(name) do
    self.send("#{name}_link").click
  end
  create_element_getter "#{name}_link", identifier, __method__, block
end

#p(name, identifier = nil, &block) ⇒ Nil

Generates two p methods to:

  • return the text from a p;

  • return the p element.

p :my_element, :id => “my_element” page.my_element.should == “My Element Text” page.my_element_p.exist?.should be_true

Examples:

Specify a p to generate methods

Parameters:

  • name (Symbol)

    The name of the p element (used to generate the methods)

  • identifier (optional, Hash) (defaults to: nil)

    A set of key, value pairs to identify the element

  • block

Returns:

  • (Nil)


177
178
179
180
181
182
# File 'lib/watir-page-helper/generated.rb', line 177

def p name, identifier=nil, &block
  define_method(name) do
    self.send("#{name}_p").text
  end
  create_element_getter "#{name}_p", identifier, __method__, block
end

#radio(name, identifier = nil, &block) ⇒ Nil

Generates three radio methods to:

  • select a radio;

  • see whether a radio is selected; and

  • return the radio element.

Examples:

Specify a radio button to generate methods

radio :medium, :value => "Medium"
page.select_medium
page.medium_set?.should be_true
page.medium_radio.exist?.should be_true

Parameters:

  • name (Symbol)

    The name of the radio element (used to generate the methods)

  • identifier (optional, Hash) (defaults to: nil)

    A set of key, value pairs to identify the element

  • block

Returns:

  • (Nil)


138
139
140
141
142
143
144
145
146
147
# File 'lib/watir-page-helper.rb', line 138

def radio name, identifier=nil, &block
  define_method("select_#{name}") do
    self.send("#{name}_radio").set
  end
  define_method("#{name}_set?") do
    self.send("#{name}_radio").set?
  end
  create_element_getter "#{name}_radio", identifier, __method__, block
  create_element_getter "#{name}_radio_button", identifier, __method__, block
end

#radio_button(name, identifier = nil, &block) ⇒ Object



149
150
151
152
# File 'lib/watir-page-helper.rb', line 149

def radio_button name, identifier=nil, &block
  warn 'radio_button is a deprecated method in the watir-page-helper gem, and will be removed in future versions.'
  radio name, identifier, &block
end

#row(name, identifier = nil, &block) ⇒ Nil

Generates two row methods to:

  • return the text from a table row;

  • return the row element.

Examples:

Specify a row to generate methods

row(:test_table_row_1) { | test_table |  test_table.tr }
page.test_table_row_1.should == "Test Table Col 1 Test Table Col 2"
page.test_table_row_1_row.cells.length.should == 2

Parameters:

  • name (Symbol)

    The name of the row element (used to generate the methods)

  • identifier (optional, Hash) (defaults to: nil)

    A set of key, value pairs to identify the element

  • block

Returns:

  • (Nil)


167
168
169
170
171
172
# File 'lib/watir-page-helper.rb', line 167

def row name, identifier=nil, &block
  define_method(name) do
    self.send("#{name}_row").text
  end
  create_element_getter "#{name}_row", identifier, 'tr', block
end

#select_list(name, identifier = nil, &block) ⇒ Nil

Generates four select_list methods to:

  • get the value specified in a select_list

  • select a value in a select list;

  • see whether a value is selected; and

  • return the select_list element.

Examples:

Specify a select list to generate methods

select_list :cars, :name => "cars"
page.cars = "Mazda" #select
page.cars.should == "Mazda" #check
page.cars_selected?("Mazda").should be_true #selected?
page.cars_select_list.exists?.should be_true #object

Parameters:

  • name (Symbol)

    The name of the select_list element (used to generate the methods)

  • identifier (optional, Hash) (defaults to: nil)

    A set of key, value pairs to identify the element

  • block

Returns:

  • (Nil)


79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/watir-page-helper.rb', line 79

def select_list name, identifier=nil, &block
  define_method(name) do
    self.send("#{name}_select_list").value
  end
  define_method("#{name}=") do |value|
    self.send("#{name}_select_list").select(value)
  end
  define_method("#{name}_selected?") do |value|
    self.send("#{name}_select_list").selected?(value)
  end
  create_element_getter "#{name}_select_list", identifier, __method__, block
end

#span(name, identifier = nil, &block) ⇒ Nil

Generates two span methods to:

  • return the text from a span;

  • return the span element.

span :my_element, :id => “my_element” page.my_element.should == “My Element Text” page.my_element_span.exist?.should be_true

Examples:

Specify a span to generate methods

Parameters:

  • name (Symbol)

    The name of the span element (used to generate the methods)

  • identifier (optional, Hash) (defaults to: nil)

    A set of key, value pairs to identify the element

  • block

Returns:

  • (Nil)


157
158
159
160
161
162
# File 'lib/watir-page-helper/generated.rb', line 157

def span name, identifier=nil, &block
  define_method(name) do
    self.send("#{name}_span").text
  end
  create_element_getter "#{name}_span", identifier, __method__, block
end

#table(name, identifier = nil, &block) ⇒ Nil

Generates a table method to return a table element.

Examples:

Specify a table to generate a method

table :my_element, :id => 'my_element'
page.my_element.exists?.should be_true

Parameters:

  • name (Symbol)

    The name of the table element (used to generate the method)

  • identifier (optional, Hash) (defaults to: nil)

    A set of key, value pairs to identify the element

  • block

Returns:

  • (Nil)


358
359
360
# File 'lib/watir-page-helper/generated.rb', line 358

def table name, identifier=nil, &block
  create_element_getter "#{name}", identifier, __method__, block
end

#text_field(name, identifier = nil, &block) ⇒ Nil

Generates three text_field methods to:

  • set a text_field;

  • get a text_field’s value; and

  • return the text_field element.

text_field first_name, :name => “firstname” page.first_name = “Finley” #set page.first_name.should == “Finley” #check page.first_name_text_field.exists?.should be_true #object

Examples:

Specify a text_field to generate methods

Parameters:

  • name (Symbol)

    The name of the text_field element (used to generate the methods)

  • identifier (optional, Hash) (defaults to: nil)

    A set of key, value pairs to identify the element

  • block

Returns:

  • (Nil)


339
340
341
342
343
344
345
346
347
# File 'lib/watir-page-helper/generated.rb', line 339

def text_field name, identifier=nil, &block
  define_method(name) do
    self.send("#{name}_text_field").value
  end
  define_method("#{name}=") do |value|
    self.send("#{name}_text_field").set value
  end
  create_element_getter "#{name}_text_field", identifier, __method__, block
end