Module: PageObject::Accessors

Defined in:
lib/page-object/accessors.rb

Overview

Contains the class level methods that are inserted into your page objects when you include the PageObject module. These methods will generate another set of methods that provide access to the elements on the web pages.

See Also:

  • for the watir implementation of the platform delegate
  • for the selenium implementation of the platform delegate

Instance Method Summary collapse

Instance Method Details

#button(name, identifier = {:index => 0}, &block) ⇒ Object

adds three methods - one to click a button, another to return the button element, and another to check the button’s existence.

Examples:

button(:purchase, :id => 'purchase')
# will generate 'purchase', 'purchase_element', and 'purchase?' methods

Parameters:

  • the (Symbol)

    name used for the generated methods

  • identifier (Hash) (defaults to: {:index => 0})

    how we find a button. You can use a multiple paramaters by combining of any of the following except xpath. The valid keys are:

    • :class => Watir and Selenium

    • :css => Watir and Selenium

    • :id => Watir and Selenium

    • :index => Watir and Selenium

    • :name => Watir and Selenium

    • :text => Watir and Selenium

    • :value => Watir and Selenium

    • :xpath => Watir and Selenium

    • :src => Watir and Selenium (input type=image only)

    • :alt => Watir and Selenium (input type=image only)

  • optional

    block to be invoked when element method is called



404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/page-object/accessors.rb', line 404

def button(name, identifier={:index => 0}, &block)
  define_method(name) do
    return platform.click_button_for identifier.clone unless block_given?
    self.send("#{name}_element").click
  end
  define_method("#{name}_element") do
    return call_block(&block) if block_given?
    platform.button_for(identifier.clone)
  end
  define_method("#{name}?") do
    return call_block(&block).exists? if block_given?
    platform.button_for(identifier.clone).exists?
  end
  alias_method "#{name}_button".to_sym, "#{name}_element".to_sym
end

#cell(name, identifier = {:index => 0}, &block) ⇒ Object

adds three methods - one to retrieve the text from a table cell, another to return the table cell element, and another to check the cell’s existence.

Examples:

cell(:total, :id => 'total_cell')
# will generate 'total', 'total_element', and 'total?' methods

Parameters:

  • the (Symbol)

    name used for the generated methods

  • identifier (Hash) (defaults to: {:index => 0})

    how we find a cell. You can use a multiple paramaters by combining of any of the following except xpath. The valid keys are:

    • :class => Watir and Selenium

    • :id => Watir and Selenium

    • :index => Watir and Selenium

    • :name => Watir and Selenium

    • :text => Watir and Selenium

    • :xpath => Watir and Selenium

  • optional

    block to be invoked when element method is called



540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
# File 'lib/page-object/accessors.rb', line 540

def cell(name, identifier={:index => 0}, &block)
  define_method("#{name}") do
    return platform.cell_text_for identifier.clone unless block_given?
    self.send("#{name}_element").text
  end
  define_method("#{name}_element") do
    return call_block(&block) if block_given?
    platform.cell_for(identifier.clone)
  end
  define_method("#{name}?") do
    return call_block(&block).exists? if block_given?
    platform.cell_for(identifier.clone).exists?
  end
  alias_method "#{name}_cell".to_sym, "#{name}_element".to_sym
end

#checkbox(name, identifier = {:index => 0}, &block) ⇒ Object

adds five methods - one to check, another to uncheck, another to return the state of a checkbox, another to return a PageObject::Elements::CheckBox object representing the checkbox, and a final method to check the checkbox’s existence.

Examples:

checkbox(:active, :name => "is_active")
# will generate 'check_active', 'uncheck_active', 'active_checked?', 
# 'active_element', and 'active?' methods

Parameters:

  • the (Symbol)

    name used for the generated methods

  • identifier (Hash) (defaults to: {:index => 0})

    how we find a checkbox. You can use a multiple paramaters by combining of any of the following except xpath. The valid keys are:

    • :class => Watir and Selenium

    • :id => Watir and Selenium

    • :index => Watir and Selenium

    • :name => Watir and Selenium

    • :value => Watir and Selenium

    • :xpath => Watir and Selenium

  • optional

    block to be invoked when element method is called



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/page-object/accessors.rb', line 310

def checkbox(name, identifier={:index => 0}, &block)
  define_method("check_#{name}") do
    return platform.check_checkbox(identifier.clone) unless block_given?
    self.send("#{name}_element").check
  end
  define_method("uncheck_#{name}") do
    return platform.uncheck_checkbox(identifier.clone) unless block_given?
    self.send("#{name}_element").uncheck
  end
  define_method("#{name}_checked?") do
    return platform.checkbox_checked?(identifier.clone) unless block_given?
    self.send("#{name}_element").checked?
  end
  define_method("#{name}_element") do
    return call_block(&block) if block_given?
    platform.checkbox_for(identifier.clone)
  end
  define_method("#{name}?") do
    return call_block(&block).exists? if block_given?
    platform.checkbox_for(identifier.clone).exists?
  end
  alias_method "#{name}_checkbox".to_sym, "#{name}_element".to_sym
end

#div(name, identifier = {:index => 0}, &block) ⇒ Object

adds three methods - one to retrieve the text from a div, another to return the div element, and another to check the div’s existence.

Examples:

div(:message, :id => 'message')
# will generate 'message', 'message_element', and 'message?' methods

Parameters:

  • the (Symbol)

    name used for the generated methods

  • identifier (Hash) (defaults to: {:index => 0})

    how we find a div. You can use a multiple paramaters by combining of any of the following except xpath. The valid keys are:

    • :class => Watir and Selenium

    • :id => Watir and Selenium

    • :index => Watir and Selenium

    • :name => Watir and Selenium

    • :text => Watir and Selenium

    • :xpath => Watir and Selenium

  • optional

    block to be invoked when element method is called



439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/page-object/accessors.rb', line 439

def div(name, identifier={:index => 0}, &block)
  define_method(name) do
    return platform.div_text_for identifier.clone unless block_given?
    self.send("#{name}_element").text
  end
  define_method("#{name}_element") do
    return call_block(&block) if block_given?
    platform.div_for(identifier.clone)
  end
  define_method("#{name}?") do
    return call_block(&block).exists? if block_given?
    platform.div_for(identifier.clone).exists?
  end
  alias_method "#{name}_div".to_sym, "#{name}_element".to_sym
end

#element(name, tag, identifier = {:index => 0}, &block) ⇒ Object

adds three methods - one to retrieve the text of an element, another to retrieve an element, and another to check the element’s existence.

Examples:

element(:title, :header, :id => 'title')
# will generate 'title', 'title_element', and 'title?' methods

Parameters:

  • the (Symbol)

    name used for the generated methods

  • the (Symbol)

    name of the tag for the element

  • identifier (Hash) (defaults to: {:index => 0})

    how we find an element. You can use a multiple paramaters by combining of any of the following except xpath. The valid keys are:

    • :class => Watir and Selenium

    • :id => Watir and Selenium

    • :index => Watir and Selenium

    • :name => Watir and Selenium

    • :xpath => Watir and Selenium

  • optional

    block to be invoked when element method is called



1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
# File 'lib/page-object/accessors.rb', line 1039

def element(name, tag, identifier={:index => 0}, &block)
  define_method("#{name}") do
    self.send("#{name}_element").text
  end
  define_method("#{name}_element") do
    return call_block(&block) if block_given?
    platform.element_for(tag, identifier.clone)
  end
  define_method("#{name}?") do
    self.send("#{name}_element").exists?
  end      
end

#expected_element(element_name, timeout = 5) ⇒ boolean

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 named :address expected on the page within 10 seconds

expected_element(:address, 10)
page.has_expected_element?

Parameters:

  • the (Symbol)

    name given to the element in the declaration

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

    default value is 5 seconds

Returns:

  • (boolean)


57
58
59
60
61
# File 'lib/page-object/accessors.rb', line 57

def expected_element(element_name, timeout=5)
  define_method("has_expected_element?") do
    self.respond_to? "#{element_name}_element" and self.send("#{element_name}_element").when_present timeout
  end
end

#expected_title(expected_title) ⇒ boolean

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:

  • (boolean)

Raises:

  • An exception if expected_title does not match actual title



38
39
40
41
42
43
44
# File 'lib/page-object/accessors.rb', line 38

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

#file_field(name, identifier = {:index => 0}, &block) ⇒ Object

adds three methods - one to set the file for a file field, another to retrieve the file field element, and another to check it’s existence.

Examples:

file_field(:the_file, :id => 'file_to_upload')
# will generate 'the_file=', 'the_file_element', and 'the_file?' methods

Parameters:

  • the (Symbol)

    name used for the generated methods

  • identifier (Hash) (defaults to: {:index => 0})

    how we find a file_field. You can use a multiple paramaters by combining of any of the following except xpath. The valid keys are:

    • :class => Watir and Selenium

    • :id => Watir and Selenium

    • :index => Watir and Selenium

    • :name => Watir and Selenium

    • :title => Watir and Selenium

    • :xpath => Watir and Selenium

  • optional

    block to be invoked when element method is called



970
971
972
973
974
975
976
977
978
979
980
981
982
983
# File 'lib/page-object/accessors.rb', line 970

def file_field(name, identifier={:index => 0}, &block)
  define_method("#{name}=") do |value|
    return platform.file_field_value_set(identifier.clone, value) unless block_given?
    self.send("#{name}_element").value = value
  end
  define_method("#{name}_element") do
    return call_block(&block) if block_given?
    platform.file_field_for(identifier.clone)
  end
  define_method("#{name}?") do
    return call_block(&block).exists? if block_given?
    platform.file_field_for(identifier.clone).exists?
  end
end

#form(name, identifier = {:index => 0}, &block) ⇒ Object

adds two methods - one to retrieve the form element, and another to check the form’s existence.

Examples:

form(:login, :id => 'login')
# will generate 'login_element' and 'login?' methods

Parameters:

  • the (Symbol)

    name used for the generated methods

  • identifier (Hash) (defaults to: {:index => 0})

    how we find a form. You can use a multiple paramaters by combining of any of the following except xpath. The valid keys are:

    • :action => Watir and Selenium

    • :class => Watir and Selenium

    • :id => Watir and Selenium

    • :index => Watir and Selenium

    • :xpath => Watir and Selenium

  • optional

    block to be invoked when element method is called



606
607
608
609
610
611
612
613
614
615
616
# File 'lib/page-object/accessors.rb', line 606

def form(name, identifier={:index => 0}, &block)
  define_method("#{name}_element") do
    return call_block(&block) if block_given?
    platform.form_for(identifier.clone)
  end
  define_method("#{name}?") do
    return call_block(&block).exists? if block_given?
    platform.form_for(identifier.clone).exists?
  end
  alias_method "#{name}_form".to_sym, "#{name}_element".to_sym
end

#h1(name, identifier = {:index => 0}, &block) ⇒ Object

adds three methods - one to retrieve the text of a h1 element, another to retrieve a h1 element, and another to check for it’s existence.

Examples:

h1(:title, :id => 'title')
# will generate 'title', 'title_element', and 'title?' methods

Parameters:

  • the (Symbol)

    name used for the generated methods

  • identifier (Hash) (defaults to: {:index => 0})

    how we find a H1. You can use a multiple paramaters by combining of any of the following except xpath. The valid keys are:

    • :class => Watir and Selenium

    • :id => Watir and Selenium

    • :index => Watir and Selenium

    • :name => Watir and Selenium

    • :xpath => Watir and Selenium

  • optional

    block to be invoked when element method is called



731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
# File 'lib/page-object/accessors.rb', line 731

def h1(name, identifier={:index => 0}, &block)
  define_method(name) do
    return platform.h1_text_for identifier.clone unless block_given?
    self.send("#{name}_element").text
  end
  define_method("#{name}_element") do
    return call_block(&block) if block_given?
    platform.h1_for(identifier.clone)
  end
  define_method("#{name}?") do
    return call_block(&block).exists? if block_given?
    platform.h1_for(identifier.clone).exists?
  end
  alias_method "#{name}_h1".to_sym, "#{name}_element".to_sym
end

#h2(name, identifier = {:index => 0}, &block) ⇒ Object

adds three methods - one to retrieve the text of a h2 element, another to retrieve a h2 element, and another to check for it’s existence.

Examples:

h2(:title, :id => 'title')
# will generate 'title', 'title_element', and 'title?' methods

Parameters:

  • the (Symbol)

    name used for the generated methods

  • identifier (Hash) (defaults to: {:index => 0})

    how we find a H2. You can use a multiple paramaters by combining of any of the following except xpath. The valid keys are:

    • :class => Watir and Selenium

    • :id => Watir and Selenium

    • :index => Watir and Selenium

    • :name => Watir and Selenium

    • :xpath => Watir and Selenium

  • optional

    block to be invoked when element method is called



765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
# File 'lib/page-object/accessors.rb', line 765

def h2(name, identifier={:index => 0}, &block)
  define_method(name) do
    return platform.h2_text_for identifier.clone unless block_given?
    self.send("#{name}_element").text
  end
  define_method("#{name}_element") do
    return call_block(&block) if block_given?
    platform.h2_for(identifier.clone)
  end
  define_method("#{name}?") do
    return call_block(&block).exists? if block_given?
    platform.h2_for(identifier.clone).exists?
  end
  alias_method "#{name}_h2".to_sym, "#{name}_element".to_sym
end

#h3(name, identifier = {:index => 0}, &block) ⇒ Object

adds three methods - one to retrieve the text of a h3 element, another to return a h3 element, and another to check for it’s existence.

Examples:

h3(:title, :id => 'title')
# will generate 'title', 'title_element', and 'title?' methods

Parameters:

  • the (Symbol)

    name used for the generated methods

  • identifier (Hash) (defaults to: {:index => 0})

    how we find a H3. You can use a multiple paramaters by combining of any of the following except xpath. The valid keys are:

    • :class => Watir and Selenium

    • :id => Watir and Selenium

    • :index => Watir and Selenium

    • :name => Watir and Selenium

    • :xpath => Watir and Selenium

  • optional

    block to be invoked when element method is called



799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
# File 'lib/page-object/accessors.rb', line 799

def h3(name, identifier={:index => 0}, &block)
  define_method(name) do
    return platform.h3_text_for identifier.clone unless block_given?
    self.send("#{name}_element").text
  end
  define_method("#{name}_element") do
    return call_block(&block) if block_given?
    platform.h3_for(identifier.clone)
  end
  define_method("#{name}?") do
    return call_block(&block).exists? if block_given?
    platform.h3_for(identifier.clone).exists?
  end
  alias_method "#{name}_h3".to_sym, "#{name}_element".to_sym
end

#h4(name, identifier = {:index => 0}, &block) ⇒ Object

adds three methods - one to retrieve the text of a h4 element, another to return a h4 element, and another to check for it’s existence.

Examples:

h4(:title, :id => 'title')
# will generate 'title', 'title_element', and 'title?' methods

Parameters:

  • the (Symbol)

    name used for the generated methods

  • identifier (Hash) (defaults to: {:index => 0})

    how we find a H4. You can use a multiple paramaters by combining of any of the following except xpath. The valid keys are:

    • :class => Watir and Selenium

    • :id => Watir and Selenium

    • :index => Watir and Selenium

    • :name => Watir and Selenium

    • :xpath => Watir and Selenium

  • optional

    block to be invoked when element method is called



833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
# File 'lib/page-object/accessors.rb', line 833

def h4(name, identifier={:index => 0}, &block)
  define_method(name) do
    return platform.h4_text_for identifier.clone unless block_given?
    self.send("#{name}_element").text
  end
  define_method("#{name}_element") do
    return call_block(&block) if block_given?
    platform.h4_for(identifier.clone)
  end
  define_method("#{name}?") do
    return call_block(&block).exists? if block_given?
    platform.h4_for(identifier.clone).exists?
  end
  alias_method "#{name}_h4".to_sym, "#{name}_element".to_sym
end

#h5(name, identifier = {:index => 0}, &block) ⇒ Object

adds three methods - one to retrieve the text of a h5 element, another to return a h5 element, and another to check for it’s existence.

Examples:

h5(:title, :id => 'title')
# will generate 'title', 'title_element', and 'title?' methods

Parameters:

  • the (Symbol)

    name used for the generated methods

  • identifier (Hash) (defaults to: {:index => 0})

    how we find a H5. You can use a multiple paramaters by combining of any of the following except xpath. The valid keys are:

    • :class => Watir and Selenium

    • :id => Watir and Selenium

    • :index => Watir and Selenium

    • :name => Watir and Selenium

    • :xpath => Watir and Selenium

  • optional

    block to be invoked when element method is called



867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
# File 'lib/page-object/accessors.rb', line 867

def h5(name, identifier={:index => 0}, &block)
  define_method(name) do
    return platform.h5_text_for identifier.clone unless block_given?
    self.send("#{name}_element").text
  end
  define_method("#{name}_element") do
    return call_block(&block) if block_given?
    platform.h5_for(identifier.clone)
  end
  define_method("#{name}?") do
    return call_block(&block).exists? if block_given?
    platform.h5_for(identifier.clone).exists?
  end
  alias_method "#{name}_h5".to_sym, "#{name}_element".to_sym
end

#h6(name, identifier = {:index => 0}, &block) ⇒ Object

adds three methods - one to retrieve the text of a h6 element, another to return a h6 element, and another to check for it’s existence.

Examples:

h6(:title, :id => 'title')
# will generate 'title', 'title_element', and 'title?' methods

Parameters:

  • the (Symbol)

    name used for the generated methods

  • identifier (Hash) (defaults to: {:index => 0})

    how we find a H6. You can use a multiple paramaters by combining of any of the following except xpath. The valid keys are:

    • :class => Watir and Selenium

    • :id => Watir and Selenium

    • :index => Watir and Selenium

    • :name => Watir and Selenium

    • :xpath => Watir and Selenium

  • optional

    block to be invoked when element method is called



901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
# File 'lib/page-object/accessors.rb', line 901

def h6(name, identifier={:index => 0}, &block)
  define_method(name) do
    return platform.h6_text_for identifier.clone unless block_given?
    self.send("#{name}_element").text
  end
  define_method("#{name}_element") do
    return call_block(&block) if block_given?
    platform.h6_for(identifier.clone)
  end
  define_method("#{name}?") do
    return call_block(&block).exists? if block_given?
    platform.h6_for(identifier.clone).exists?
  end
  alias_method "#{name}_h6".to_sym, "#{name}_element".to_sym
end

#hidden_field(name, identifier = {:index => 0}, &block) ⇒ Object

adds three methods to the page object - one to get the text from a hidden field, another to retrieve the hidden field element, and another to check the hidden field’s existence.

Examples:

hidden_field(:user_id, :id => "user_identity")
# will generate 'user_id', 'user_id_element' and 'user_id?' methods

Parameters:

  • the (String)

    name used for the generated methods

  • identifier (Hash) (defaults to: {:index => 0})

    how we find a hidden field. You can use a multiple paramaters by combining of any of the following except xpath. The valid keys are:

    • :class => Watir and Selenium

    • :id => Watir and Selenium

    • :index => Watir and Selenium

    • :name => Watir and Selenium

    • :text => Watir and Selenium

    • :value => Watir and Selenium

    • :xpath => Watir and Selenium

  • optional

    block to be invoked when element method is called



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/page-object/accessors.rb', line 150

def hidden_field(name, identifier={:index => 0}, &block)
  define_method(name) do
    return platform.hidden_field_value_for identifier.clone unless block_given?
    self.send("#{name}_element").value
  end
  define_method("#{name}_element") do
    return call_block(&block) if block_given?
    platform.hidden_field_for(identifier.clone)
  end
  define_method("#{name}?") do
    return call_block(&block).exists? if block_given?
    platform.hidden_field_for(identifier.clone).exists?
  end
  alias_method "#{name}_hidden_field".to_sym, "#{name}_element".to_sym
end

#image(name, identifier = {:index => 0}, &block) ⇒ Object

adds two methods - one to retrieve the image element, and another to check the image’s existence.

Examples:

image(:logo, :id => 'logo')
# will generate 'logo_element' and 'logo?' methods

Parameters:

  • the (Symbol)

    name used for the generated methods

  • identifier (Hash) (defaults to: {:index => 0})

    how we find an image. You can use a multiple paramaters by combining of any of the following except xpath. The valid keys are:

    • :alt => Watir and Selenium

    • :class => Watir and Selenium

    • :id => Watir and Selenium

    • :index => Watir and Selenium

    • :name => Watir and Selenium

    • :src => Watir and Selenium

    • :xpath => Watir and Selenium

  • optional

    block to be invoked when element method is called



576
577
578
579
580
581
582
583
584
585
586
# File 'lib/page-object/accessors.rb', line 576

def image(name, identifier={:index => 0}, &block)
  define_method("#{name}_element") do
    return call_block(&block) if block_given?
    platform.image_for(identifier.clone)
  end
  define_method("#{name}?") do
    return call_block(&block).exists? if block_given?
    platform.image_for(identifier.clone).exists?
  end
  alias_method "#{name}_image".to_sym, "#{name}_element".to_sym
end

#in_frame(identifier, frame = nil, &block) ⇒ Object

Identify an element as existing within a frame or iframe. A frame parameter is passed to the block and must be passed to the other calls to PageObject. You can nest calls to in_frame by passing the frame to the next level.

Examples:

in_frame(:id => 'frame_id') do |frame|
  text_field(:first_name, :id => 'fname', :frame => frame)
end

Parameters:

  • identifier (Hash)

    how we find the frame. The valid keys are:

    • :id => Watir and Selenium

    • :index => Watir and Selenium

    • :name => Watir and Selenium

  • frame (defaults to: nil)

    passed from a previous call to in_frame. Used to nest calls

  • block

    that contains the calls to elements that exist inside the frame.



80
81
82
83
84
# File 'lib/page-object/accessors.rb', line 80

def in_frame(identifier, frame=nil, &block)
  frame = [] if frame.nil?
  frame << identifier
  block.call(frame)
end

#label(name, identifier = {:index => 0}, &block) ⇒ Object

adds three methods - one to retrieve the text from a label, another to return the label element, and another to check the label’s existence.

Examples:

label(:message, :id => 'message')
# will generate 'message', 'message_element', and 'message?' methods

Parameters:

  • the (Symbol)

    name used for the generated methods

  • identifier (Hash) (defaults to: {:index => 0})

    how we find a label. You can use a multiple paramaters by combining of any of the following except xpath. The valid keys are:

    • :class => Watir and Selenium

    • :id => Watir and Selenium

    • :index => Watir and Selenium

    • :name => Watir and Selenium

    • :text => Watir and Selenium

    • :xpath => Watir and Selenium

  • optional

    block to be invoked when element method is called



1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
# File 'lib/page-object/accessors.rb', line 1004

def label(name, identifier={:index => 0}, &block)
  define_method(name) do
    return platform.label_text_for identifier.clone unless block_given?
    self.send("#{name}_element").text
  end
  define_method("#{name}_element") do
    return call_block(&block) if block_given?
    platform.label_for(identifier.clone)
  end
  define_method("#{name}?") do
    return call_block(&block).exists? if block_given?
    platform.label_for(identifier.clone).exists?
  end
  alias_method "#{name}_label".to_sym, "#{name}_element".to_sym
end

adds three methods - one to select a link, another to return a PageObject::Elements::Link object representing the link, and another that checks the link’s existence.

Examples:

link(:add_to_cart, :text => "Add to Cart")
# will generate 'add_to_cart', 'add_to_cart_element', and 'add_to_cart?' methods

Parameters:

  • the (Symbol)

    name used for the generated methods

  • identifier (Hash) (defaults to: {:index => 0})

    how we find a link. You can use a multiple paramaters by combining of any of the following except xpath. The valid keys are:

    • :class => Watir and Selenium

    • :css => Watir and Selenium

    • :href => Watir and Selenium

    • :id => Watir and Selenium

    • :index => Watir and Selenium

    • :link => Watir and Selenium

    • :link_text => Watir and Selenium

    • :name => Watir and Selenium

    • :text => Watir and Selenium

    • :xpath => Watir and Selenium

  • optional

    block to be invoked when element method is called



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/page-object/accessors.rb', line 272

def link(name, identifier={:index => 0}, &block)
  define_method(name) do
    return platform.click_link_for identifier.clone unless block_given?
    self.send("#{name}_element").click
  end
  define_method("#{name}_element") do
    return call_block(&block) if block_given?
    platform.link_for(identifier.clone)
  end
  define_method("#{name}?") do
    return call_block(&block).exists? if block_given?
    platform.link_for(identifier.clone).exists?
  end
  alias_method "#{name}_link".to_sym, "#{name}_element".to_sym
end

#list_item(name, identifier = {:index => 0}, &block) ⇒ Object

adds three methods - one to retrieve the text from a list item, another to return the list item element, and another to check the list item’s existence.

Examples:

list_item(:item_one, :id => 'one')
# will generate 'item_one', 'item_one_element', and 'item_one?' methods

Parameters:

  • the (Symbol)

    name used for the generated methods

  • identifier (Hash) (defaults to: {:index => 0})

    how we find a list item. You can use a multiple paramaters by combining of any of the following except xpath. The valid keys are:

    • :class => Watir and Selenium

    • :id => Watir and Selenium

    • :index => Watir and Selenium

    • :name => Watir and Selenium

    • :xpath => Watir and Selenium

  • optional

    block to be invoked when element method is called



637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
# File 'lib/page-object/accessors.rb', line 637

def list_item(name, identifier={:index => 0}, &block)
  define_method(name) do
    return platform.list_item_text_for identifier.clone unless block_given?
    self.send("#{name}_element").text
  end
  define_method("#{name}_element") do
    return call_block(&block) if block_given?
    platform.list_item_for(identifier.clone)
  end
  define_method("#{name}?") do
    return call_block(&block).exists? if block_given?
    platform.list_item_for(identifier.clone).exists?
  end
  alias_method "#{name}_list_item".to_sym, "#{name}_element".to_sym
end

#ordered_list(name, identifier = {:index => 0}, &block) ⇒ Object

adds two methods - one to retrieve the ordered list element, and another to test it’s existence.

Examples:

ordered_list(:top_five, :id => 'top')
# will generate 'top_five_element' and 'top_five?' methods

Parameters:

  • the (Symbol)

    name used for the generated methods

  • identifier (Hash) (defaults to: {:index => 0})

    how we find an ordered list. You can use a multiple paramaters by combining of any of the following except xpath. The valid keys are:

    • :class => Watir and Selenium

    • :id => Watir and Selenium

    • :index => Watir and Selenium

    • :name => Watir and Selenium

    • :xpath => Watir and Selenium

  • optional

    block to be invoked when element method is called



701
702
703
704
705
706
707
708
709
710
711
# File 'lib/page-object/accessors.rb', line 701

def ordered_list(name, identifier={:index => 0}, &block)
  define_method("#{name}_element") do
    return call_block(&block) if block_given?
    platform.ordered_list_for(identifier.clone)
  end
  define_method("#{name}?") do
    return call_block(&block).exists? if block_given?
    platform.ordered_list_for(identifier.clone).exists?
  end
  alias_method "#{name}_ordered_list".to_sym, "#{name}_element".to_sym
end

#page_url(url) ⇒ Object Also known as: direct_url

Specify the url for the page. A call to this method will generate a ‘goto’ method to take you to the page.

Parameters:

  • the (String)

    url for the page.

  • a (Symbol)

    method name to call to get the url



19
20
21
22
23
24
# File 'lib/page-object/accessors.rb', line 19

def page_url(url)
  define_method("goto") do
    url = url.kind_of?(Symbol) ? self.send(url) : url
    platform.navigate_to url
  end
end

#paragraph(name, identifier = {:index => 0}, &block) ⇒ Object

adds three methods - one to retrieve the text of a paragraph, another to retrieve a paragraph element, and another to check the paragraph’s existence.

Examples:

paragraph(:title, :id => 'title')
# will generate 'title', 'title_element', and 'title?' methods

Parameters:

  • the (Symbol)

    name used for the generated methods

  • identifier (Hash) (defaults to: {:index => 0})

    how we find a paragraph. You can use a multiple paramaters by combining of any of the following except xpath. The valid keys are:

    • :class => Watir and Selenium

    • :id => Watir and Selenium

    • :index => Watir and Selenium

    • :name => Watir and Selenium

    • :xpath => Watir and Selenium

  • optional

    block to be invoked when element method is called



935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
# File 'lib/page-object/accessors.rb', line 935

def paragraph(name, identifier={:index => 0}, &block)
  define_method(name) do
    return platform.paragraph_text_for identifier.clone unless block_given?
    self.send("#{name}_element").text
  end
  define_method("#{name}_element") do
    return call_block(&block) if block_given?
    platform.paragraph_for(identifier.clone)
  end
  define_method("#{name}?") do
    return call_block(&block).exists? if block_given?
    platform.paragraph_for(identifier.clone).exists?
  end
  alias_method "#{name}_paragraph".to_sym, "#{name}_element".to_sym
end

#radio_button(name, identifier = {:index => 0}, &block) ⇒ Object

adds five methods - one to select, another to clear, another to return if a radio button is selected, another method to return a PageObject::Elements::RadioButton object representing the radio button element, and another to check the radio button’s existence.

Examples:

radio_button(:north, :id => "north")
# will generate 'select_north', 'clear_north', 'north_selected?', 
# 'north_element', and 'north?' methods

Parameters:

  • the (Symbol)

    name used for the generated methods

  • identifier (Hash) (defaults to: {:index => 0})

    how we find a radio button. You can use a multiple paramaters by combining of any of the following except xpath. The valid keys are:

    • :class => Watir and Selenium

    • :id => Watir and Selenium

    • :index => Watir and Selenium

    • :name => Watir and Selenium

    • :value => Watir and Selenium

    • :xpath => Watir and Selenium

  • optional

    block to be invoked when element method is called



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/page-object/accessors.rb', line 357

def radio_button(name, identifier={:index => 0}, &block)
  define_method("select_#{name}") do
    return platform.select_radio(identifier.clone) unless block_given?
    self.send("#{name}_element").select
  end
  define_method("clear_#{name}") do
    return platform.clear_radio(identifier.clone) unless block_given?
    self.send("#{name}_element").clear
  end
  define_method("#{name}_selected?") do
    return platform.radio_selected?(identifier.clone) unless block_given?
    self.send("#{name}_element").selected?
  end
  define_method("#{name}_element") do
    return call_block(&block) if block_given?
    platform.radio_button_for(identifier.clone)
  end
  define_method("#{name}?") do
    return call_block(&block).exists? if block_given?
    platform.radio_button_for(identifier.clone).exists?
  end
  alias_method "#{name}_radio_button".to_sym, "#{name}_element".to_sym
end

#select_list(name, identifier = {:index => 0}, &block) ⇒ Object

adds four methods - one to select an item in a drop-down, another to fetch the currently selected item text, another to retrieve the select list element, and another to check the drop down’s existence.

Examples:

select_list(:state, :id => "state")
# will generate 'state', 'state=', 'state_element', 'state?' methods

Parameters:

  • the (Symbol)

    name used for the generated methods

  • identifier (Hash) (defaults to: {:index => 0})

    how we find a select list. You can use a multiple paramaters by combining of any of the following except xpath. The valid keys are:

    • :class => Watir and Selenium

    • :id => Watir and Selenium

    • :index => Watir and Selenium

    • :name => Watir and Selenium

    • :text => Watir only

    • :value => Watir only

    • :xpath => Watir and Selenium

  • optional

    block to be invoked when element method is called



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/page-object/accessors.rb', line 228

def select_list(name, identifier={:index => 0}, &block)
  define_method(name) do
    return platform.select_list_value_for identifier.clone unless block_given?
    self.send("#{name}_element").value
  end
  define_method("#{name}=") do |value|
    return platform.select_list_value_set(identifier.clone, value) unless block_given?
    self.send("#{name}_element").select(value)
  end
  define_method("#{name}_element") do
    return call_block(&block) if block_given?
    platform.select_list_for(identifier.clone)
  end
  define_method("#{name}?") do
    return call_block(&block).exists? if block_given?
    platform.select_list_for(identifier.clone).exists?
  end
  alias_method "#{name}_select_list".to_sym, "#{name}_element".to_sym
end

#span(name, identifier = {:index => 0}, &block) ⇒ Object

adds three methods - one to retrieve the text from a span, another to return the span element, and another to check the span’s existence.

Examples:

span(:alert, :id => 'alert')
# will generate 'alert', 'alert_element', and 'alert?' methods

Parameters:

  • the (Symbol)

    name used for the generated methods

  • identifier (Hash) (defaults to: {:index => 0})

    how we find a span. You can use a multiple paramaters by combining of any of the following except xpath. The valid keys are:

    • :class => Watir and Selenium

    • :id => Watir and Selenium

    • :index => Watir and Selenium

    • :name => Watir and Selenium

    • :xpath => Watir and Selenium

  • optional

    block to be invoked when element method is called



473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# File 'lib/page-object/accessors.rb', line 473

def span(name, identifier={:index => 0}, &block)
  define_method(name) do
    return platform.span_text_for identifier.clone unless block_given?
    self.send("#{name}_element").text
  end
  define_method("#{name}_element") do
    return call_block(&block) if block_given?
    platform.span_for(identifier.clone)
  end
  define_method("#{name}?") do
    return call_block(&block).exists? if block_given?
    platform.span_for(identifier.clone).exists?
  end
  alias_method "#{name}_span".to_sym, "#{name}_element".to_sym
end

#table(name, identifier = {:index => 0}, &block) ⇒ Object

adds two methods - one to retrieve the table element, and another to check the table’s existence. The existence method does not work on Selenium so it should not be called.

Examples:

table(:cart, :id => 'shopping_cart')
# will generate a 'cart_element' and 'cart?' method

Parameters:

  • the (Symbol)

    name used for the generated methods

  • identifier (Hash) (defaults to: {:index => 0})

    how we find a table. You can use a multiple paramaters by combining of any of the following except xpath. The valid keys are:

    • :class => Watir and Selenium

    • :id => Watir and Selenium

    • :index => Watir and Selenium

    • :name => Watir and Selenium

    • :xpath => Watir and Selenium

  • optional

    block to be invoked when element method is called



508
509
510
511
512
513
514
515
516
517
518
# File 'lib/page-object/accessors.rb', line 508

def table(name, identifier={:index => 0}, &block)
  define_method("#{name}_element") do
    return call_block(&block) if block_given?
    platform.table_for(identifier.clone)
  end
  define_method("#{name}?") do
    return call_block(&block).exists? if block_given?
    platform.table_for(identifier.clone).exists?
  end
  alias_method "#{name}_table".to_sym, "#{name}_element".to_sym
end

#text_area(name, identifier = {:index => 0}, &block) ⇒ Object

adds four methods to the page object - one to set text in a text area, another to retrieve text from a text area, another to return the text area element, and another to check the text area’s existence.

Examples:

text_area(:address, :id => "address")
# will generate 'address', 'address=', 'address_element',
# 'address?' methods

Parameters:

  • the (String)

    name used for the generated methods

  • identifier (Hash) (defaults to: {:index => 0})

    how we find a text area. You can use a multiple paramaters by combining of any of the following except xpath. The valid keys are:

    • :class => Watir and Selenium

    • :id => Watir and Selenium

    • :index => Watir and Selenium

    • :name => Watir and Selenium

    • :xpath => Watir and Selenium

  • optional

    block to be invoked when element method is called



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/page-object/accessors.rb', line 186

def text_area(name, identifier={:index => 0}, &block)
  define_method(name) do
    return platform.text_area_value_for identifier.clone unless block_given?
    self.send("#{name}_element").value
  end
  define_method("#{name}=") do |value|
    return platform.text_area_value_set(identifier.clone, value) unless block_given?
    self.send("#{name}_element").value = value
  end
  define_method("#{name}_element") do
    return call_block(&block) if block_given?
    platform.text_area_for(identifier.clone)
  end
  define_method("#{name}?") do
    return call_block(&block).exists? if block_given?
    platform.text_area_for(identifier.clone).exists?
  end
  alias_method "#{name}_text_area".to_sym, "#{name}_element".to_sym
end

#text_field(name, identifier = {:index => 0}, &block) ⇒ Object

adds four methods to the page object - one to set text in a text field, another to retrieve text from a text field, another to return the text field element, another to check the text field’s existence.

Examples:

text_field(:first_name, :id => "first_name") 
# will generate 'first_name', 'first_name=', 'first_name_element',
# 'first_name?' methods

Parameters:

  • the (String)

    name used for the generated methods

  • identifier (Hash) (defaults to: {:index => 0})

    how we find a text field. You can use a multiple paramaters by combining of any of the following except xpath. The valid keys are:

    • :class => Watir and Selenium

    • :id => Watir and Selenium

    • :index => Watir and Selenium

    • :name => Watir and Selenium

    • :text => Watir only

    • :title => Watir and Selenium

    • :value => Watir only

    • :xpath => Watir and Selenium

  • optional

    block to be invoked when element method is called



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/page-object/accessors.rb', line 109

def text_field(name, identifier={:index => 0}, &block)
  define_method(name) do
    return platform.text_field_value_for identifier.clone unless block_given?
    self.send("#{name}_element").value
  end
  define_method("#{name}=") do |value|
    return platform.text_field_value_set(identifier.clone, value) unless block_given?
    self.send("#{name}_element").value = value
  end
  define_method("#{name}_element") do
    return call_block(&block) if block_given?
    platform.text_field_for(identifier.clone)
  end
  define_method("#{name}?") do
    return call_block(&block).exists? if block_given?
    platform.text_field_for(identifier.clone).exists?
  end
  alias_method "#{name}_text_field".to_sym, "#{name}_element".to_sym
end

#unordered_list(name, identifier = {:index => 0}, &block) ⇒ Object

adds two methods - one to retrieve the unordered list element, and another to check it’s existence.

Examples:

unordered_list(:menu, :id => 'main_menu')
# will generate 'menu_element' and 'menu?' methods

Parameters:

  • the (Symbol)

    name used for the generated methods

  • identifier (Hash) (defaults to: {:index => 0})

    how we find an unordered list. You can use a multiple paramaters by combining of any of the following except xpath. The valid keys are:

    • :class => Watir and Selenium

    • :id => Watir and Selenium

    • :index => Watir and Selenium

    • :name => Watir and Selenium

    • :xpath => Watir and Selenium

  • optional

    block to be invoked when element method is called



671
672
673
674
675
676
677
678
679
680
681
# File 'lib/page-object/accessors.rb', line 671

def unordered_list(name, identifier={:index => 0}, &block)
  define_method("#{name}_element") do
    return call_block(&block) if block_given?
    platform.unordered_list_for(identifier.clone)
  end
  define_method("#{name}?") do
    return call_block(&block).exists? if block_given?
    platform.unordered_list_for(identifier.clone).exists?
  end
  alias_method "#{name}_unordered_list".to_sym, "#{name}_element".to_sym
end