Module: Druid::Accessors

Defined in:
lib/druid/accessors.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

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

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

Examples:

area(: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 an area. You can use a multiple parameters

  • optional

    block to be invoked when element method is called



809
810
811
812
813
814
815
# File 'lib/druid/accessors.rb', line 809

def area(name, identifier={:index => 0}, &block)
  standard_methods(name, identifier, 'area_for', &block)
  define_method(name) do
    return click_area_for identifier.clone unless block_given?
    self.send("#{name}_element").click
  end
end

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

adds two methods - one to return the audio element and another to check the audio’s existence

Examples:

audio(:acdc, :id => 'audio_id')
# will generate 'acdc_element' and 'acdc?' methods

Parameters:

  • the (Symbol)

    name used for the generated methods

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

    how we find an audio element. You can use a multiple parameters

  • optional

    block to be invoked when element method is called



845
846
847
# File 'lib/druid/accessors.rb', line 845

def audio(name, identifier={:index => 0}, &block)
  standard_methods(name, identifier, 'audio_for', &block)
end

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

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

Examples:

b(:blod, :id => 'title')
# will generate 'bold', 'bold_element', 'bold?' methods

Parameters:

  • the (Symbol)

    name used for the generated methods

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

    how we find a b, You can use a multiple parameters

  • optional

    block to be invoked when element method is called



877
878
879
880
881
882
883
# File 'lib/druid/accessors.rb', line 877

def b(name, identifier={:index => 0}, &block)
  standard_methods(name, identifier, 'b_for', &block)
  define_method(name) do
    return b_text_for identifier.clone unless block_given?
    self.send("#{name}_element").text
  end
end

#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 parameters

  • optional

    block to be invoked when element method is called



354
355
356
357
358
359
360
# File 'lib/druid/accessors.rb', line 354

def button(name, identifier={:index => 0}, &block)
  standard_methods(name, identifier, 'button_for', &block)
  define_method(name) do
    return click_button_for identifier.clone unless block_given?
    self.send("#{name}_element").click
  end
end

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

adds two method - one to return the canvas element and another to check the canvas’s existence.

Examples:

canvas(:my_canvas, :id => 'canvas_id')
#  will generate 'my_canvas_element' and 'my_canvas?' methods

Parameters:

  • the (Symbol)

    name used for the generated methods

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

    how we find a canvas. You can use a multiple parameters

  • optional

    block to be invoked when element method is called



829
830
831
# File 'lib/druid/accessors.rb', line 829

def canvas(name, identifier={:index => 0}, &block)
  standard_methods(name, identifier, 'canvas_for', &block)
end

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

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 parameters

  • optional

    block to be invoked when element method is called



416
417
418
419
420
421
422
# File 'lib/druid/accessors.rb', line 416

def cell(name, identifier={:index => 0}, &block)
  standard_methods(name, identifier, 'cell_for', &block)
  define_method(name) do
    return cell_text_for identifier.clone unless block_given?
    self.send("#{name}_element").text
  end
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 parameters

  • optional

    block to be invoked when element method is called



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/druid/accessors.rb', line 244

def checkbox(name, identifier={:index => 0}, &block)
  standard_methods(name, identifier, 'checkbox_for', &block)
  define_method("check_#{name}") do
    return check_checkbox identifier.clone unless block_given?
    self.send("#{name}_element").check
  end
  define_method("uncheck_#{name}") do
    return uncheck_checkbox identifier.clone unless block_given?
    self.send("#{name}_element").uncheck
  end
  define_method("#{name}_checked?") do
    return checkbox_checked? identifier.clone unless block_given?
    self.send("#{name}_element").checked?
  end
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 parameters

  • optional

    block to be invoked when element method is called



374
375
376
377
378
379
380
# File 'lib/druid/accessors.rb', line 374

def div(name, identifier={:index => 0}, &block)
  standard_methods(name, identifier, 'div_for', &block)
  define_method(name) do
    return div_text_for identifier.clone unless block_given?
    self.send("#{name}_element").text
  end
end

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

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

Examples:

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

Parameters:

  • the (Symbol)

    name used for the generated methods

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

    how we find an element. You can use a multiple parameters

  • optional

    block to be invoked when element method is called



934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
# File 'lib/druid/accessors.rb', line 934

def element(name, tag=:element, identifier={:index => 0}, &block)
  #
  # sets tag as element if not defined
  #
  if tag.is_a? Hash
    identifier = tag
    tag = :element
  end

  define_method("#{name}") do
    self.send("#{name}_element").text
  end
  define_method("#{name}_element") do
    return call_block(&block) if block_given?
    element_for(tag, identifier.clone)
  end
  define_method("#{name}?") do
    self.send("#{name}_element").exist?
  end
end

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

adds a method to return a collection of generic Element objects for a specific tag

Examples:

elements(:title, :header, :id => 'title')
 # will generate 'title_elements'

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 parameters

  • optional

    block to be invoked when element method is called



968
969
970
971
972
973
974
975
976
977
978
979
980
981
# File 'lib/druid/accessors.rb', line 968

def elements(name, tag=:element, identifier={:index => 0}, &block)
  #
  # sets tag as element if not defined
  #
  if tag.is_a? Hash
    identifier = tag
    tag = :element
  end

  define_method("#{name}_elements") do
    return call_block(&block) if block_given?
    elements_for(tag, identifier.clone)
  end
end

#expected_element(element_name, timeout = Druid.default_element_wait) ⇒ 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, Interger) (defaults to: Druid.default_element_wait)

    default value is 5 seconds

Returns:

  • (boolean)


108
109
110
111
112
# File 'lib/druid/accessors.rb', line 108

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

#expected_element_visible(element_name, timeout = Druid.default_element_wait) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/druid/accessors.rb', line 114

def expected_element_visible(element_name, timeout=Druid.default_element_wait)
  define_method("has_expected_element_visible?") do
    self.respond_to? "#{element_name}_element" and self.send("#{element_name}_element").when_present timeout
    self.respond_to? "#{element_name}_element" and self.send("#{element_name}_element").when_visible 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



88
89
90
91
92
93
94
95
# File 'lib/druid/accessors.rb', line 88

def expected_title(expected_title)
  define_method("has_expected_title?") do
    page_title = title
    has_expected_title = (expected_title === page_title)
    raise "Expected title '#{expected_title}' instead of '#{page_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

  • optional

    block to be invoked when element method is called



769
770
771
772
773
774
775
# File 'lib/druid/accessors.rb', line 769

def file_field(name, identifier={:index => 0}, &block)
  standard_methods(name, identifier, 'file_field_for', &block)
  define_method("#{name}=") do |value|
    return file_field_value_set(identifier.clone, value) unless block_given?
    self.send("#{name}_element").value = value
  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 parameters

  • optional

    block to be invoked when element method is called



499
500
501
# File 'lib/druid/accessors.rb', line 499

def form(name, identifier={:index => 0}, &block)
  standard_methods(name, identifier, 'form_for', &block)
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

  • optional

    block to be invoked when element method is called



628
629
630
631
632
633
634
# File 'lib/druid/accessors.rb', line 628

def h1(name, identifier={:index => 0}, &block)
  standard_methods(name, identifier, 'h1_for', &block)
  define_method(name) do
    return h1_text_for identifier.clone unless block_given?
    self.send("#{name}_element").text
  end
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

  • optional

    block to be invoked when element method is called



648
649
650
651
652
653
654
# File 'lib/druid/accessors.rb', line 648

def h2(name, identifier={:index => 0}, &block)
  standard_methods(name, identifier, 'h2_for', &block)
  define_method(name) do
    return h2_text_for identifier.clone unless block_given?
    self.send("#{name}_element").text
  end
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

  • optional

    block to be invoked when element method is called



668
669
670
671
672
673
674
# File 'lib/druid/accessors.rb', line 668

def h3(name, identifier={:index => 0}, &block)
  standard_methods(name, identifier, 'h3_for', &block)
  define_method(name) do
    return h3_text_for identifier.clone unless block_given?
    self.send("#{name}_element").text
  end
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

  • optional

    block to be invoked when element method is called



688
689
690
691
692
693
694
# File 'lib/druid/accessors.rb', line 688

def h4(name, identifier={:index => 0}, &block)
  standard_methods(name, identifier, 'h4_for', &block)
  define_method(name) do
    return h4_text_for identifier.clone unless block_given?
    self.send("#{name}_element").text
  end
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

  • optional

    block to be invoked when element method is called



708
709
710
711
712
713
714
# File 'lib/druid/accessors.rb', line 708

def h5(name, identifier={:index => 0}, &block)
  standard_methods(name, identifier, 'h5_for', &block)
  define_method(name) do
    return h5_text_for identifier.clone unless block_given?
    self.send("#{name}_element").text
  end
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

  • optional

    block to be invoked when element method is called



728
729
730
731
732
733
734
# File 'lib/druid/accessors.rb', line 728

def h6(name, identifier={:index => 0}, &block)
  standard_methods(name, identifier, 'h6_for', &block)
  define_method(name) do
    return h6_text_for identifier.clone unless block_given?
    self.send("#{name}_element").text
  end
end

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

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 (Symbol)

    name used for the generated methods

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

    how we find a hidden field. You can use a multiple parameters

  • optional

    block to be invoked when element method is called



516
517
518
519
520
521
522
# File 'lib/druid/accessors.rb', line 516

def hidden_field(name, identifier={:index => 0}, &block)
  standard_methods(name, identifier, 'hidden_field_for', &block)
  define_method(name) do
    return hidden_field_value_for identifier.clone unless block_given?
    self.send("#{name}_element").value
  end
end

#i(name, identifier = {:index => 0}, &block) ⇒ Object Also known as: icon

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

Examples:

i(:italic, :id => 'title')
# will generate 'italic', 'italic_element', 'italic?' methods

Parameters:

  • the (Symbol)

    name used for the generated methods

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

    how we find a i, You can use a multiple parameters

  • optional

    block to be invoked when element method is called



897
898
899
900
901
902
903
# File 'lib/druid/accessors.rb', line 897

def i(name, identifier={:index => 0}, &block)
  standard_methods(name, identifier, 'i_for', &block)
  define_method(name) do
    return i_text_for identifier.clone unless block_given?
    self.send("#{name}_element").text
  end
end

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

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', 'logo_loaded?' and 'logo?' methods

Parameters:

  • the (Symbol)

    name used for the generated methods

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

    how we find a image. You can use a multiple parameters

  • optional

    block to be invoked when element method is called



478
479
480
481
482
483
484
# File 'lib/druid/accessors.rb', line 478

def image(name, identifier={:index => 0}, &block)
  standard_methods(name, identifier, 'image_for', &block)
  define_method("#{name}_loaded?") do
    return image_loaded_for identifier.clone unless block_given?
    self.send("#{name}_element").loaded?
  end
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 Druid. 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

    • :index

    • :name

  • 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.



139
140
141
142
143
# File 'lib/druid/accessors.rb', line 139

def in_frame(identifier, frame=nil, &block)
  frame = frame.nil? ? [] : frame.dup
  frame << {frame: identifier}
  block.call(frame)
end

#in_iframe(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 Druid. You can nest calls to in_iframe by passing the frame to the next level.

Examples:

in_iframe(: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

    • :index

    • :name

  • frame (defaults to: nil)

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

  • block

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



161
162
163
164
165
# File 'lib/druid/accessors.rb', line 161

def in_iframe(identifier, frame=nil, &block)
  frame = frame.nil? ? [] : frame.dup
  frame << {iframe: 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 parameters

  • optional

    block to be invoked when element method is called



789
790
791
792
793
794
795
# File 'lib/druid/accessors.rb', line 789

def label(name, identifier={:index => 0}, &block)
  standard_methods(name, identifier, 'label_for', &block)
  define_method(name) do
    return label_text_for identifier.clone unless block_given?
    self.send("#{name}_element").text
  end
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 parameters by combining of any of the following except xpath. The valid values are:

    • :class

    • :href

    • :id

    • :index

    • :name

    • :text

    • :xpath

    • :link

    • :link_text

    • :css

    • :title

  • optional

    block to be invoked when element method is called



192
193
194
195
196
197
198
# File 'lib/druid/accessors.rb', line 192

def link(name, identifier={:index => 0}, &block)
  standard_methods(name, identifier, 'link_for', &block)
  define_method(name) do
    return click_link_for identifier.clone unless block_given?
    self.send("#{name}_element").click
  end
end

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

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 parameters

  • optional

    block to be invoked when element method is called



538
539
540
541
542
543
544
# File 'lib/druid/accessors.rb', line 538

def list_item(name, identifier={:index => 0}, &block)
  standard_methods(name, identifier, 'list_item_for', &block)
  define_method(name) do
    return list_item_text_for identifier.clone unless block_given?
    self.send("#{name}_element").text
  end
end

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

adds three methods - one to return the text within the ordered list, 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' '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 parameters

  • optional

    block to be invoked when element method is called



560
561
562
563
564
565
566
# File 'lib/druid/accessors.rb', line 560

def ordered_list(name, identifier={:index => 0}, &block)
  standard_methods(name, identifier, 'ordered_list_for', &block)
  define_method(name) do
    return ordered_list_text_for identifier.clone unless block_given?
    self.send("#{name}_element").text
  end
end

#page_section(name, section_class, identifier) ⇒ Object

adds a method to return a page object rooted at the element

Examples:

page_section(:navigation_bar, NavigationBar, :id => 'nav-bar')
# will generate 'navigation_bar'

Parameters:

  • the (Symbol)

    name used for the generated methods

  • the (Class)

    class to instantiate for the element

  • identifier (Hash)

    how we find an element. You can use multiple parameters



1005
1006
1007
1008
1009
# File 'lib/druid/accessors.rb', line 1005

def page_section(name, section_class, identifier)
  define_method(name) do
    page_for(identifier, section_class)
  end
end

#page_sections(name, section_class, identifier) ⇒ Object

adds a method to return a collection of page objects rooted at elements

Examples:

page_sections(:articles, Article, :class => 'article')
# will generate 'articles'

Parameters:

  • the (Symbol)

    name used for the generated method

  • the (Class)

    class to instantiate for each element

  • identifier (Hash)

    how we find an element. You can use a multiple parameters



1022
1023
1024
1025
1026
# File 'lib/druid/accessors.rb', line 1022

def page_sections(name, section_class, identifier)
  define_method(name) do
    pages_for(identifier, section_class)
  end
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



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/druid/accessors.rb', line 38

def page_url(url)
  define_method("goto") do
    driver.goto self.page_url_value
  end

  define_method("page_url_value") do
    lookup = url.kind_of?(Symbol) ? self.send(url) : url
    erb = ERB.new(%Q{#{lookup}})
    merged_params = self.class.instance_variable_get("@merged_params")
    params = merged_params ? merged_params : self.class.params
    erb.result(binding)
  end
end

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

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

  • optional

    block to be invoked when element method is called



748
749
750
751
752
753
754
# File 'lib/druid/accessors.rb', line 748

def paragraph(name, identifier={:index => 0}, &block)
  standard_methods(name, identifier, 'paragraph_for', &block)
  define_method(name) do
    return paragraph_text_for identifier.clone unless block_given?
    self.send("#{name}_element").text
  end
end

#paramsObject

Return the params that exist on this page class



27
28
29
# File 'lib/druid/accessors.rb', line 27

def params
  @params ||= {}
end

#params=(the_params) ⇒ Object

Set some values that can be used withing the class. This is typically used to provided values that help build dynamic urls in the page_url method

Parameters:

  • the (Hash)

    value to set the params



20
21
22
# File 'lib/druid/accessors.rb', line 20

def params=(the_params)
  @params = the_params
end

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

adds four methods - one to select, 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', '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 parameters

  • optional

    block to be invoked when element method is called



307
308
309
310
311
312
313
314
315
316
317
# File 'lib/druid/accessors.rb', line 307

def radio_button(name, identifier={:index => 0}, &block)
  standard_methods(name, identifier, 'radio_button_for', &block)
  define_method("select_#{name}") do
    return select_radio identifier.clone unless block_given?
    self.send("#{name}_element").select
  end
  define_method("#{name}_selected?") do
    return radio_selected? identifier.clone unless block_given?
    self.send("#{name}_element").selected?
  end
end

#radio_button_group(name, identifier) ⇒ Object



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/druid/accessors.rb', line 320

def radio_button_group(name, identifier)
  define_method("select_#{name}") do |value|
    radio_buttons_for(identifier.clone).each do |radio_elem|
      return radio_elem.select if radio_elem.value == value
    end
  end
  define_method("#{name}_values") do
    radio_buttons_for(identifier.clone).collect { |e| e.value}
  end
  define_method("#{name}_selected?") do
    radio_buttons_for(identifier.clone).each do |radio_elem|
      return radio_elem.value if radio_elem.selected?
    end
    return false
  end
  define_method("#{name}_elements") do
    radio_buttons_for(identifier.clone)
  end
  define_method("#{name}?") do
    radio_buttons_for(identifier.clone).any?
  end
end

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

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

Examples:

row(:sums, :id => 'sum_row')
# will generate 'sums', 'sums_element', and 'sums?' 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 parameters

  • optional

    block to be invoked when element method is called



438
439
440
441
442
443
444
# File 'lib/druid/accessors.rb', line 438

def row(name, identifier={:index => 0}, &block)
  standard_methods(name, identifier, 'row_for', &block)
  define_method(name) do
    return row_text_for identifier.clone unless block_given?
    self.send("#{name}_element").text
  end
end

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

adds five 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 and another to get all the available options to select from

Examples:

select_list(:state, :id => "state")
# will generate 'state', 'state=', 'state_element', 'state?', 'state_options' 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 parameters

  • optional

    block to be invoked when element method is called



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/druid/accessors.rb', line 275

def select_list(name, identifier={:index => 0}, &block)
  standard_methods(name, identifier, 'select_list_for', &block)
  define_method(name) do
    return select_list_value_for identifier.clone unless block_given?
    self.send("#{name}_element").options.each {|o| return o.text if o.selected?}
  end
  define_method("#{name}=") do |value|
    return select_list_value_set(identifier.clone, value) unless block_given?
    self.send("#{name}_element").select(value)
  end
  define_method("#{name}_options") do
    element = self.send("#{name}_element")
    (element && element.options) ? element.options.collect(&:text) : []
  end
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 parameters

  • optional

    block to be invoked when element method is called



458
459
460
461
462
463
464
# File 'lib/druid/accessors.rb', line 458

def span(name, identifier={:index => 0}, &block)
  standard_methods(name, identifier, 'span_for', &block)
  define_method(name) do
    return span_text_for identifier.clone unless block_given?
    self.send("#{name}_element").text
  end
end

#standard_methods(name, identifier, method, &block) ⇒ Object



983
984
985
986
987
988
989
990
991
992
# File 'lib/druid/accessors.rb', line 983

def standard_methods(name, identifier, method, &block)
  define_method("#{name}_element") do
    return call_block(&block) if block_given?
    self.send(method, identifier.clone)
  end
  define_method("#{name}?") do
    return call_block(&block).exist? if block_given?
    self.send(method, identifier.clone).exist?
  end
end

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

adds two methods - one to retrieve a svg, and another to check svg’s existence.

Examples:

svg(:circle, :id => 'circle')
# will generate 'circle_element', and 'circle?' methods

Parameters:

  • the (Symbol)

    name used for the generated methods

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

    how we find a svg. You can use a multiple parameters

  • optional

    block to be invoked when element method is called



918
919
920
# File 'lib/druid/accessors.rb', line 918

def svg(name, identifier={:index => 0}, &block)
  standard_methods(name, identifier, 'svg_for', &block)
end

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

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

Examples:

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

Parameters:

  • the

    name used for the generated methods

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

    how we find a table. You can use a multiple parameters

  • optional

    block to be invoked when element method is called



395
396
397
398
399
400
401
# File 'lib/druid/accessors.rb', line 395

def table(name, identifier={:index => 0}, &block)
  standard_methods(name, identifier, 'table_for', &block)
  define_method(name) do
    return table_text_for identifier.clone unless block_given?
    self.send("#{name}_element").text
  end
end

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

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 (Symbol)

    name used for the generated methods

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

    how we find a text area. You can use a multiple parameters

  • optional

    block to be invoked when element method is called



583
584
585
586
587
588
589
590
591
592
593
# File 'lib/druid/accessors.rb', line 583

def text_area(name, identifier={:index => 0}, &block)
  standard_methods(name, identifier, 'text_area_for', &block)
  define_method("#{name}=") do |value|
    return text_area_value_set(identifier.clone, value) unless block_given?
    self.send("#{name}_element").value = value
  end
  define_method(name) do
    return text_area_value_for identifier.clone unless block_given?
    self.send("#{name}_element").value
  end
end

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

adds four methods to the page objec - 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 (Symbol)

    name used for the generated methods

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

    how we find a text_field. You can use a multiple parameters

  • optional

    block to be invoked when element method is called



217
218
219
220
221
222
223
224
225
226
227
# File 'lib/druid/accessors.rb', line 217

def text_field(name, identifier={:index => 0}, &block)
  standard_methods(name, identifier, 'text_field_for', &block)
  define_method(name) do
    return text_field_value_for identifier.clone unless block_given?
    self.send("#{name}_element").value
  end
  define_method("#{name}=") do |value|
    return text_field_value_set(identifier.clone, value) unless block_given?
    self.send("#{name}_element").value = value
  end
end

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

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

Examples:

unordered_list(:menu, :id => 'main_menu')
# will generate 'menu' '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 parameters

  • optional

    block to be invoked when element method is called



607
608
609
610
611
612
613
# File 'lib/druid/accessors.rb', line 607

def unordered_list(name, identifier={:index => 0}, &block)
  standard_methods(name, identifier, 'unordered_list_for', &block)
  define_method(name) do
    return unordered_list_text_for identifier.clone unless block_given?
    self.send("#{name}_element").text
  end
end

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

adds two methods - one to return the video element and another to check the video’s existence

Examples:

video(:movie, :id => 'video_id')
# will generate 'movie_element' and 'movie?' methods

Parameters:

  • the (Symbol)

    name used for the generated methods

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

    how we find a video element. You can use a multiple parameters

  • optional

    block to be invoked when element method is called



861
862
863
# File 'lib/druid/accessors.rb', line 861

def video(name, identifier={:index => 0}, &block)
  standard_methods(name, identifier, 'video_for', &block)
end

#wait_for_expected_title(expected_title, timeout = Druid.default_element_wait) ⇒ boolean

Creates a method that waits the expected_title of a page to match the actual.

Examples:

Specify ‘Google’ as the expected title of a page

wait_for_expected_title "Google"
page.wait_for_expected_title?

Parameters:

  • expected_title (String)

    the literal expected title for the page

  • expected_title (Regexp)

    the expected title pattern for the page

  • timeout (optional, Integer) (defaults to: Druid.default_element_wait)

    default value is nil - do not wait

Returns:

  • (boolean)

Raises:

  • An exception if expected_title does not match actual title



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/druid/accessors.rb', line 65

def wait_for_expected_title(expected_title, timeout=Druid.default_element_wait)
  define_method("wait_for_expected_title?") do
    error_message = lambda { "Expected title '#{expected_title}' instead of '#{title}'" }
    has_expected_title = (expected_title === title)
    wait_until(timeout, error_message.call) do
      has_expected_title = (expected_title === title)
    end unless has_expected_title
    raise error_message.call unless has_expected_title
    has_expected_title
  end
end