lib/page-object/accessors.rb in page-object-0.7.6 vs lib/page-object/accessors.rb in page-object-0.8
- old
+ new
@@ -526,17 +526,18 @@
end
alias_method "#{name}_span".to_sym, "#{name}_element".to_sym
end
#
- # adds two methods - one to retrieve the table element, and another to
+ # adds three methods - one to return the text for the table, 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.
#
# @example
# table(:cart, :id => 'shopping_cart')
- # # will generate a 'cart_element' and 'cart?' method
+ # # will generate a 'cart', 'cart_element' and 'cart?' method
#
# @param [Symbol] the name used for the generated methods
# @param [Hash] identifier 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
@@ -546,10 +547,14 @@
# * :name => Watir and Selenium
# * :xpath => Watir and Selenium
# @param optional block to be invoked when element method is called
#
def table(name, identifier={:index => 0}, &block)
+ define_method(name) do
+ return platform.table_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.table_for(identifier.clone)
end
define_method("#{name}?") do
@@ -699,16 +704,17 @@
alias_method "#{name}_list_item".to_sym, "#{name}_element".to_sym
end
alias_method :li, :list_item
#
- # adds two methods - one to retrieve the unordered list element, and another to
+ # adds three methods - one to return the text within the unorderd
+ # list, one to retrieve the unordered list element, and another to
# check it's existence.
#
# @example
# unordered_list(:menu, :id => 'main_menu')
- # # will generate 'menu_element' and 'menu?' methods
+ # # will generate 'menu', 'menu_element' and 'menu?' methods
#
# @param [Symbol] the name used for the generated methods
# @param [Hash] identifier 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
@@ -718,10 +724,14 @@
# * :name => Watir and Selenium
# * :xpath => Watir and Selenium
# @param optional block to be invoked when element method is called
#
def unordered_list(name, identifier={:index => 0}, &block)
+ define_method(name) do
+ return platform.unordered_list_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.unordered_list_for(identifier.clone)
end
define_method("#{name}?") do
@@ -731,16 +741,17 @@
alias_method "#{name}_unordered_list".to_sym, "#{name}_element".to_sym
end
alias_method :ul, :unordered_list
#
- # adds two methods - one to retrieve the ordered list element, and another to
+ # adds three methods - one to return the text withing the ordered
+ # list, one to retrieve the ordered list element, and another to
# test it's existence.
#
# @example
# ordered_list(:top_five, :id => 'top')
- # # will generate 'top_five_element' and 'top_five?' methods
+ # # will generate 'top_five', 'top_five_element' and 'top_five?' methods
#
# @param [Symbol] the name used for the generated methods
# @param [Hash] identifier 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
@@ -750,10 +761,14 @@
# * :name => Watir and Selenium
# * :xpath => Watir and Selenium
# @param optional block to be invoked when element method is called
#
def ordered_list(name, identifier={:index => 0}, &block)
+ define_method(name) do
+ return platform.ordered_list_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.ordered_list_for(identifier.clone)
end
define_method("#{name}?") do
@@ -1979,7 +1994,57 @@
def indexed_property (name, identifier_list)
define_method("#{name}") do
IndexedProperties::TableOfElements.new(@browser, identifier_list)
end
end
+
+ #
+ # methods to fetch multiple elements of the same type
+ #
+ # adds a method to the page object to retrun all of the matching elements
+ #
+ # @example
+ # text_fields(:first_name, :id => "first_name")
+ # # will generate 'first_name_elements'
+ #
+ # @param [String] the name used for the generated methods
+ # @param [Hash] identifier 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 the same ones supported by the standard methods.
+ # @param optional block to be invoked when element method is called
+ #
+ [:text_fields,
+ :hidden_fields,
+ :text_areas,
+ :select_lists,
+ :links,
+ :checkboxes,
+ :radio_buttons,
+ :buttons,
+ :divs,
+ :spans,
+ :tables,
+ :cells,
+ :images,
+ :forms,
+ :list_items,
+ :unordered_lists,
+ :ordered_lists,
+ :h1s,
+ :h2s,
+ :h3s,
+ :h4s,
+ :h5s,
+ :h6s,
+ :paragraphs,
+ :labels,
+ :file_fields].each do |method_name|
+ define_method(method_name) do |name, identifier, &block|
+ define_method("#{name}_elements") do
+ return call_block(&block) if block_given?
+ platform.send "#{method_name.to_s}_for", identifier.clone
+ end
+ end
+ end
+
end
end