lib/page-object/accessors.rb in page-object-0.0.4 vs lib/page-object/accessors.rb in page-object-0.0.5
- old
+ new
@@ -7,11 +7,24 @@
#
# @see PageObject::WatirPageObject for the watir implementation of the platform delegate
# @see PageObject::SeleniumPageObject for the selenium implementation of the platform delegate
#
module Accessors
+
#
+ # Specify the url for the page. A call to this method will generate a
+ # 'goto' method to take you to the page.
+ #
+ # @param [String] the url for the page.
+ #
+ def page_url(url)
+ define_method("goto") do
+ platform.navigate_to url
+ end
+ end
+
+ #
# adds three methods to the page object - one to set text in a text field,
# another to retrieve text from a text field and another to return the text
# field element.
#
# @example
@@ -27,20 +40,21 @@
# * :name => Watir and Selenium
# * :tag_name => Watir and Selenium
# * :text => Watir only
# * :value => Watir only
# * :xpath => Watir and Selenium
+ # @param optional block to be invoked when element method is called
#
- def text_field(name, identifier)
+ def text_field(name, identifier=nil, &block)
define_method(name) do
platform.text_field_value_for identifier
end
define_method("#{name}=") do |value|
platform.text_field_value_set(identifier, value)
end
define_method("#{name}_text_field") do
- platform.text_field_for identifier
+ block ? block.call(browser) : platform.text_field_for(identifier)
end
end
#
# adds two methods to the page object - one to get the text from a hidden field
@@ -58,17 +72,18 @@
# * :index => Watir only
# * :name => Watir and Selenium
# * :tag_name => Watir and Selenium
# * :text => Watir only
# * :xpath => Watir and Selenium
+ # @param optional block to be invoked when element method is called
#
- def hidden_field(name, identifier)
+ def hidden_field(name, identifier=nil, &block)
define_method(name) do
platform.hidden_field_value_for identifier
end
define_method("#{name}_hidden_field") do
- platform.hidden_field_for identifier
+ block ? block.call(browser) : platform.hidden_field_for(identifier)
end
end
#
# adds three methods to the page object - one to set text in a text area,
@@ -86,20 +101,21 @@
# * :id => Watir and Selenium
# * :index => Watir only
# * :name => Watir and Selenium
# * :tag_name => Watir and Selenium
# * :xpath => Watir and Selenium
+ # @param optional block to be invoked when element method is called
#
- def text_area(name, identifier)
+ def text_area(name, identifier=nil, &block)
define_method(name) do
platform.text_area_value_for identifier
end
define_method("#{name}=") do |value|
platform.text_area_value_set(identifier, value)
end
define_method("#{name}_text_area") do
- platform.text_area_for identifier
+ block ? block.call(browser) : platform.text_area_for(identifier)
end
end
#
# adds three methods - one to select an item in a drop-down,
@@ -117,20 +133,21 @@
# * :index => Watir only
# * :name => Watir and Selenium
# * :text => Watir only
# * :value => Watir only
# * :xpath => Watir and Selenium
+ # @param optional block to be invoked when element method is called
#
- def select_list(name, identifier)
+ def select_list(name, identifier=nil, &block)
define_method(name) do
platform.select_list_value_for identifier
end
define_method("#{name}=") do |value|
platform.select_list_value_set(identifier, value)
end
define_method("#{name}_select_list") do
- platform.select_list_for identifier
+ block ? block.call(browser) : platform.select_list_for(identifier)
end
end
#
# adds two methods - one to select a link and another
@@ -150,17 +167,18 @@
# * :link => Watir and Selenium
# * :link_text => Watir and Selenium
# * :name => Watir and Selenium
# * :text => Watir and Selenium
# * :xpath => Watir and Selenium
+ # @param optional block to be invoked when element method is called
#
- def link(name, identifier)
+ def link(name, identifier=nil, &block)
define_method(name) do
platform.click_link_for identifier
end
define_method("#{name}_link") do
- platform.link_for identifier
+ block ? block.call(browser) : platform.link_for(identifier)
end
end
#
# adds four methods - one to check, another to uncheck, another
@@ -176,23 +194,24 @@
# * :class => Watir and Selenium
# * :id => Watir and Selenium
# * :index => Watir only
# * :name => Watir and Selenium
# * :xpath => Watir and Selenium
+ # @param optional block to be invoked when element method is called
#
- def checkbox(name, identifier)
+ def checkbox(name, identifier=nil, &block)
define_method("check_#{name}") do
platform.check_checkbox(identifier)
end
define_method("uncheck_#{name}") do
platform.uncheck_checkbox(identifier)
end
define_method("#{name}_checked?") do
platform.checkbox_checked?(identifier)
end
define_method("#{name}_checkbox") do
- platform.checkbox_for identifier
+ block ? block.call(browser) : platform.checkbox_for(identifier)
end
end
#
# adds four methods - one to select, another to clear,
@@ -209,23 +228,24 @@
# * :class => Watir and Selenium
# * :id => Watir and Selenium
# * :index => Watir only
# * :name => Watir and Selenium
# * :xpath => Watir and Selenium
+ # @param optional block to be invoked when element method is called
#
- def radio_button(name, identifier)
+ def radio_button(name, identifier=nil, &block)
define_method("select_#{name}") do
platform.select_radio(identifier)
end
define_method("clear_#{name}") do
platform.clear_radio(identifier)
end
define_method("#{name}_selected?") do
platform.radio_selected?(identifier)
end
define_method("#{name}_radio_button") do
- platform.radio_button_for identifier
+ block ? block.call(browser) : platform.radio_button_for(identifier)
end
end
#
# adds two methods - one to click a button and another to
@@ -241,17 +261,18 @@
# * :id => Watir and Selenium
# * :index => Watir only
# * :name => Watir and Selenium
# * :text => Watir only
# * :xpath => Watir and Selenium
+ # @param optional block to be invoked when element method is called
#
- def button(name, identifier)
+ def button(name, identifier=nil, &block)
define_method(name) do
platform.click_button_for identifier
end
define_method("#{name}_button") do
- platform.button_for identifier
+ block ? block.call(browser) : platform.button_for(identifier)
end
end
#
# adds two methods - one to retrieve the text from a div
@@ -266,17 +287,18 @@
# * :class => Watir and Selenium
# * :id => Watir and Selenium
# * :index => Watir only
# * :name => Selenium only
# * :xpath => Watir and Selenium
+ # @param optional block to be invoked when element method is called
#
- def div(name, identifier)
+ def div(name, identifier=nil, &block)
define_method(name) do
platform.div_text_for identifier
end
define_method("#{name}_div") do
- platform.div_for identifier
+ block ? block.call(browser) : platform.div_for(identifier)
end
end
#
# adds two methods - one to retrieve the text from a span
@@ -291,17 +313,18 @@
# * :class => Watir and Selenium
# * :id => Watir and Selenium
# * :index => Watir only
# * :name => Selenium only
# * :xpath => Watir and Selenium
+ # @param optional block to be invoked when element method is called
#
- def span(name, identifier)
+ def span(name, identifier=nil, &block)
define_method(name) do
platform.span_text_for identifier
end
define_method("#{name}_span") do
- platform.span_for identifier
+ block ? block.call(browser) : platform.span_for(identifier)
end
end
#
# adds a method to retrieve the table element
@@ -315,14 +338,15 @@
# * :class => Watir and Selenium
# * :id => Watir and Selenium
# * :index => Watir only
# * :name => Selenium only
# * :xpath => Watir and Selenium
+ # @param optional block to be invoked when element method is called
#
- def table(name, identifier)
+ def table(name, identifier=nil, &block)
define_method("#{name}_table") do
- platform.table_for identifier
+ block ? block.call(browser) : platform.table_for(identifier)
end
end
#
# adds two methods one to retrieve the text from a table cell
@@ -337,17 +361,18 @@
# * :class => Watir and Selenium
# * :id => Watir and Selenium
# * :index => Watir only
# * :name => Selenium only
# * :xpath => Watir and Selenium
+ # @param optional block to be invoked when element method is called
#
- def cell(name, identifier)
+ def cell(name, identifier=nil, &block)
define_method("#{name}") do
platform.cell_text_for identifier
end
define_method("#{name}_cell") do
- platform.cell_for identifier
+ block ? block.call(browser) : platform.cell_for(identifier)
end
end
#
# adds a method to retrieve the image element
@@ -361,14 +386,15 @@
# * :class => Watir and Selenium
# * :id => Watir and Selenium
# * :index => Watir only
# * :name => Watir and Selenium
# * :xpath => Watir and Selenium
+ # @param optional block to be invoked when element method is called
#
- def image(name, identifier)
+ def image(name, identifier=nil, &block)
define_method("#{name}_image") do
- platform.image_for identifier
+ block ? block.call(browser) : platform.image_for(identifier)
end
end
#
# adds a method to retrieve the form element
@@ -381,14 +407,15 @@
# @param [Hash] identifier how we find a form. The valid keys are:
# * :class => Watir and Selenium
# * :id => Watir and Selenium
# * :index => Watir only
# * :xpath => Watir and Selenium
+ # @param optional block to be invoked when element method is called
#
- def form(name, identifier)
+ def form(name, identifier=nil, &block)
define_method("#{name}_form") do
- platform.form_for identifier
+ block ? block.call(browser) : platform.form_for(identifier)
end
end
#
# adds two methods - one to retrieve the text from a list item
@@ -403,17 +430,18 @@
# * :class => Watir and Selenium
# * :id => Watir and Selenium
# * :index => Watir only
# * :name => Selenium only
# * :xpath => Watir and Selenium
+ # @param optional block to be invoked when element method is called
#
- def list_item(name, identifier)
+ def list_item(name, identifier=nil, &block)
define_method(name) do
platform.list_item_text_for identifier
end
define_method("#{name}_list_item") do
- platform.list_item_for identifier
+ block ? block.call(browser) : platform.list_item_for(identifier)
end
end
#
# adds a method to retrieve the unordered list element
@@ -427,14 +455,15 @@
# * :class => Watir and Selenium
# * :id => Watir and Selenium
# * :index => Watir only
# * :name => Selenium only
# * :xpath => Watir and Selenium
+ # @param optional block to be invoked when element method is called
#
- def unordered_list(name, identifier)
+ def unordered_list(name, identifier=nil, &block)
define_method("#{name}_unordered_list") do
- platform.unordered_list_for identifier
+ block ? block.call(browser) : platform.unordered_list_for(identifier)
end
end
#
# adds a method to retrieve the ordered list element
@@ -448,13 +477,14 @@
# * :class => Watir and Selenium
# * :id => Watir and Selenium
# * :index => Watir only
# * :name => Selenium only
# * :xpath => Watir and Selenium
+ # @param optional block to be invoked when element method is called
#
- def ordered_list(name, identifier)
+ def ordered_list(name, identifier=nil, &block)
define_method("#{name}_ordered_list") do
- platform.ordered_list_for identifier
+ block ? block.call(browser) : platform.ordered_list_for(identifier)
end
end
end
end