lib/page-object/accessors.rb in page-object-0.6.9 vs lib/page-object/accessors.rb in page-object-0.7.0
- old
+ new
@@ -32,14 +32,14 @@
# @raise An exception if expected_title does not match actual title
#
# @example Specify 'Google' as the expected title of a page
# expected_title "Google"
# page.has_expected_title?
- #
+ #
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
+ has_expected_title = expected_title === title
raise "Expected title '#{expected_title}' instead of '#{title}'" unless has_expected_title
has_expected_title
end
end
@@ -57,11 +57,11 @@
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
-
+
#
# 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.
#
@@ -86,12 +86,12 @@
#
# 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.
#
- # @example
- # text_field(:first_name, :id => "first_name")
+ # @example
+ # text_field(:first_name, :id => "first_name")
# # will generate 'first_name', 'first_name=', 'first_name_element',
# # 'first_name?' methods
#
# @param [String] the name used for the generated methods
# @param [Hash] identifier how we find a text field. You can use a multiple paramaters
@@ -124,11 +124,11 @@
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
-
+
#
# 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.
#
@@ -205,11 +205,11 @@
end
#
# 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
+ # to retrieve the select list element, and another to check the
# drop down's existence.
#
# @example
# select_list(:state, :id => "state")
# # will generate 'state', 'state=', 'state_element', 'state?' methods
@@ -293,11 +293,11 @@
# a PageObject::Elements::CheckBox object representing the checkbox, and
# a final method to check the checkbox's existence.
#
# @example
# checkbox(:active, :name => "is_active")
- # # will generate 'check_active', 'uncheck_active', 'active_checked?',
+ # # will generate 'check_active', 'uncheck_active', 'active_checked?',
# # 'active_element', and 'active?' methods
#
# @param [Symbol] the name used for the generated methods
# @param [Hash] identifier how we find a checkbox. You can use a multiple paramaters
# by combining of any of the following except xpath. The valid keys are:
@@ -333,18 +333,18 @@
alias_method "#{name}_checkbox".to_sym, "#{name}_element".to_sym
end
#
# adds five methods - one to select, another to clear,
- # another to return if a radio button is selected,
+ # 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.
#
# @example
# radio_button(:north, :id => "north")
- # # will generate 'select_north', 'clear_north', 'north_selected?',
+ # # will generate 'select_north', 'clear_north', 'north_selected?',
# # 'north_element', and 'north?' methods
#
# @param [Symbol] the name used for the generated methods
# @param [Hash] identifier 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:
@@ -710,13 +710,13 @@
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
-
+
#
- # adds three methods - one to retrieve the text of a h1 element, another to
+ # 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.
#
# @example
# h1(:title, :id => 'title')
# # will generate 'title', 'title_element', and 'title?' methods
@@ -744,11 +744,11 @@
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
-
+
#
# 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.
#
# @example
@@ -1047,10 +1047,39 @@
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
end
+ #
+ # adds a method that will return an indexed property. The property will respond to
+ # the [] method with an object that has a set of normal page_object properties that
+ # correspond to the definitions included in the identifier_list parameter, with the
+ # "what" of the "how and what" substituted based on the index provided to the []
+ # method.
+ #
+ # @example
+ # indexed_property(:title, [
+ # [:text_field, :field_1, :id => 'table[%s].field_1'],
+ # [:button, :button_1, :id => 'table[%s].button_1'],
+ # [:text_field, :field_2, :name => 'table[%s].field_2']
+ # ])
+ # # will generate a title method that responds to []. title['foo'] will return an object
+ # # that responds to the normal methods expected for two text_fields and a button with the
+ # # given names, using the given how and what with 'foo' substituted for the %s. title[123]
+ # # will do the same, using the integer 123 instead.
+ #
+ # @param [Symbol] the name used for the generated method
+ # @param [Array] definitions an array of definitions to define on the indexed property. Each
+ # entry in the array should contain two symbols and a hash, corresponding to one of the standard
+ # page_object properties with a single substitution marker in each value in the hash,
+ # e.g. [:text_field, :field_1, :id => 'table[%s].field_1']
+ #
+ def indexed_property (name, identifier_list)
+ define_method("#{name}") do
+ IndexedProperties::TableOfElements.new(@browser, identifier_list)
+ end
+ end
end
end