lib/symbiont/generators.rb in symbiont-0.1.7 vs lib/symbiont/generators.rb in symbiont-0.1.8

- old
+ new

@@ -1,353 +1,144 @@ module Symbiont - - # The Generators module is used to provide groupings of methods that are - # generated dynamically based on object declarations in page definitions. - # The method provided allow for referencing and accessing web objects. - # The platform object acts as a delegate for all method calls. - module Generators - # This method allows for a url_is() method in definitions that will - # define a direct means of accessing a resource. The most common - # example will be a URL for a web page, but any URI is valid. The - # URL provided can be navigated to using the generated view() method - # on a definition instance. - # - # @param [String] url the resource identifier to access - # @return [Nil] def url_is(url) define_method("view") do - @platform.visit(url) + platform.visit(url) end end - # This method allows for a begin_at() method in definitions. The - # URL provided can be navigated to using the generated start() - # method on a definition instance. - # - # @see Symbiont::Generators#url_is - # @param [String] url the resource identifier to access - # @return [Nil] def begin_at(url) define_method("start") do - @platform.visit(url) + platform.visit(url) end end - # This method allows for a title_is() method in definitions. The idea - # is that you can specify the title of a page. This title can then be - # checked on. Note that the assumption here is that the title will - # be static. The argument to the method can, however, be a - # regular expression. - # - # @param [String] title the literal string of the expected title - # @param [Regexp] title the pattern string of the expected title - # @return [Nil] - # @raise exception if the title found does not match the title expected def title_is(title) define_method('has_title?') do - valid_title = title =~ @browser.title if title.kind_of?(Regexp) - valid_title = title == @browser.title if title.kind_of?(String) - raise "Expected title: '#{title}'; Actual title: '#{@browser.title}'" unless valid_title + sleep 1 + valid_title = title =~ browser.title if title.kind_of?(Regexp) + valid_title = title == browser.title if title.kind_of?(String) + raise "\n\nExpected title: '#{title}'; Actual title: '#{browser.title}'" unless valid_title valid_title end end - # This method allows for a look_for() method in definitions. The idea - # is that you can specify an object that should appear on a given page - # when that page appears. - # - # @param [Symbol] widget the friendly name of the object declaration - # @param [optional, Integer] timeout the time to wait for the object to appear - # @return [Boolean] true if object was found def look_for(widget, timeout=5) define_method('has_object?') do if self.respond_to? "#{widget}_object" self.send("#{widget}_object").when_actionable(timeout) else puts "The #{widget} object was not declared and could not be checked." end end end - - # Definition method for links. Methods for the following actions will - # be created: - # * reference a link (identifier_object, identifier_link) - # * check text of link (identifier_text) - # * click a link (identifier) - # * check for existence (identifier?, identifier_exists?) - # * check for visibility (identifier_?, identifier_visible?) - # @param [Symbol] identifier the friendly name of the web object - # @param [optional, Hash] locator the key/values that identify the object - # @return [Object] instance of Symbiont::WebObjects::Link - def link(identifier, locator) - define_method("#{identifier}_object") do - @platform.get_link_for(locator.clone) - end - alias_method "#{identifier}_link".to_sym, "#{identifier}_object".to_sym - - define_method("#{identifier}_text") do - @platform.get_link_text_for(locator.clone) - end - + def paragraph(identifier, locator) define_method(identifier) do - @platform.click_link_for(locator.clone) + platform.paragraph_text_for(locator.clone) end - define_method("#{identifier}_exists?") do - @platform.check_link_for_existence(locator.clone) - end - - alias_method "#{identifier}?".to_sym, "#{identifier}_exists?".to_sym - - define_method("#{identifier}_visible?") do - @platform.check_link_for_visibility(locator.clone) - end - - alias_method "#{identifier}_?".to_sym, "#{identifier}_visible?".to_sym + common_definition_methods(identifier, locator, 'paragraph_element') end - # Definition method for buttons. Methods for the following actions will - # be created: - # * reference a button (identifier_object, identifier_button) - # * get text from button (identifier_text) - # * click a button (identifier) - # * check for existence (identifier?, identifier_exists?) - # * check for visibility (identifier_?, identifier_visible?) - # * check for enabled (identifier!, identifier_enabled?) - # @param [Symbol] identifier the friendly name of the web object - # @param [optional, Hash] locator the key/values that identify the object - # @return [Object] instance of Symbiont::WebObjects::Button - def button(identifier, locator) - define_method("#{identifier}_object") do - @platform.get_button_for(locator.clone) - end + alias_method :p, :paragraph - alias_method "#{identifier}_button".to_sym, "#{identifier}_object".to_sym - - define_method("#{identifier}_text") do - @platform.get_button_text_for(locator.clone) - end - + def link(identifier, locator) define_method(identifier) do - @platform.click_button_for(locator.clone) + platform.click_link_for(locator.clone) end - define_method("#{identifier}_exists?") do - @platform.check_button_for_existence(locator.clone) - end - - alias_method "#{identifier}?".to_sym, "#{identifier}_exists?".to_sym - - define_method("#{identifier}_visible?") do - @platform.check_button_for_visibility(locator.clone) - end - - alias_method "#{identifier}_?".to_sym, "#{identifier}_visible?".to_sym - - define_method("#{identifier}_enabled?") do - @platform.check_button_for_enabled(locator.clone) - end - - alias_method "#{identifier}!".to_sym, "#{identifier}_enabled?".to_sym + common_definition_methods(identifier, locator, 'link_element') end - # Definition method for text fields. Methods for the following actions - # will be created: - # * reference a text field (identifier_object, identifier_text_field) - # * get text from a text field (identifier) - # * set text in a text field (identifier=) - # * check for existence (identifier?, identifier_exists?) - # * check for visibility (identifier_?, identifier_visible?) - # * check for enabled (identifier!, identifier_enabled?) - # @param [Symbol] identifier the friendly name of the web object - # @param [optional, Hash] locator the key/values that identify the object - # @return [Object] instance of Symbiont::WebObjects::TextField - def text_field(identifier, locator) - define_method("#{identifier}_object") do - @platform.get_text_field_for(locator.clone) + alias_method :a, :link + + def button(identifier, locator) + define_method(identifier) do + platform.click_button_for(locator.clone) end - alias_method "#{identifier}_text_field".to_sym, "#{identifier}_object".to_sym + common_definition_methods(identifier, locator, 'button_element') + end + def text_field(identifier, locator) define_method(identifier) do - @platform.get_text_field_value_for(locator.clone) + platform.get_text_field_value_for(locator.clone) end - + define_method("#{identifier}=") do |value| - @platform.set_text_field_value_for(locator.clone, value) + platform.set_text_field_value_for(locator.clone, value) end - - define_method("#{identifier}_exists?") do - @platform.check_text_field_for_existence(locator.clone) - end - - alias_method "#{identifier}?".to_sym, "#{identifier}_exists?".to_sym - - define_method("#{identifier}_visible?") do - @platform.check_text_field_for_visibility(locator.clone) - end - - alias_method "#{identifier}_?".to_sym, "#{identifier}_visible?".to_sym - - define_method("#{identifier}_enabled?") do - @platform.check_text_field_for_enabled(locator.clone) - end - - alias_method "#{identifier}!".to_sym, "#{identifier}_enabled?".to_sym + + common_definition_methods(identifier, locator, 'text_field_element') end - # Definition method for select lists. Methods for the following actions - # will be created: - # * reference a checkbox (identifier_object, identifier_select_list) - # * check for existence (identifier?, identifier_exists?) - # * check for visibility (identifier_?, identifier_visible?) - # * check for enabled (identifier!, identifier_enabled?) - # * check for option (identifier_option?) - # * set selected value (identifier=) - # @param [Symbol] identifier the friendly name of the web object - # @param [optional, Hash] locator the key/values that identify the object - # @return [Object] instance of Symbiont::WebObjects::SelectList def select_list(identifier, locator) - define_method("#{identifier}_object") do - @platform.get_select_list_for(locator.clone) - end - - alias_method "#{identifier}_select_list".to_sym, "#{identifier}_object".to_sym - define_method(identifier) do - @platform.get_select_list_item_for(locator.clone) + platform.get_select_list_item_for(locator.clone) end define_method("#{identifier}_option?") do - @platform.get_select_list_value_for(locator.clone) + platform.get_select_list_value_for(locator.clone) end - + + define_method("#{identifier}_options?") do + object = self.send("#{identifier}_object") + (object && object.options) ? object.options.collect(&:text) : [] + end + define_method("#{identifier}=") do |value| - @platform.set_select_list_value_for(locator.clone, value) + platform.set_select_list_value_for(locator.clone, value) end - - define_method("#{identifier}_exists?") do - @platform.check_select_list_for_existence(locator.clone) - end - - alias_method "#{identifier}?".to_sym, "#{identifier}_exists?".to_sym - - define_method("#{identifier}_visible?") do - @platform.check_select_list_for_visibility(locator.clone) - end - - alias_method "#{identifier}_?".to_sym, "#{identifier}_visible?".to_sym - - define_method("#{identifier}_enabled?") do - @platform.check_select_list_for_enabled(locator.clone) - end - - alias_method "#{identifier}!".to_sym, "#{identifier}_enabled?".to_sym + + common_definition_methods(identifier, locator, 'select_list_element') end + + alias_method :select, :select_list - # Definition method for checkboxes. Methods for the following actions - # will be created: - # * reference a checkbox (identifier_object, identifier_checkbox) - # * check for existence (identifier?, identifier_exists?) - # * check for visibility (identifier_?, identifier_visible?) - # * check for enabled (identifier!, identifier_enabled?) - # * check for checked (identifier_checked?) - # * set checked state (check_identifier) - # * set unchecked state (uncheck_identifier) - # @param [Symbol] identifier the friendly name of the web object - # @param [optional, Hash] locator the key/values that identify the object - # @return [Object] instance of Symbiont::WebObjects::CheckBox def checkbox(identifier, locator) - define_method("#{identifier}_object") do - @platform.get_checkbox_for(locator.clone) - end - - alias_method "#{identifier}_checkbox".to_sym, "#{identifier}_object".to_sym - define_method("check_#{identifier}") do - @platform.check_checkbox_for(locator.clone) + platform.check_checkbox_for(locator.clone) end define_method("uncheck_#{identifier}") do - @platform.uncheck_checkbox_for(locator.clone) + platform.uncheck_checkbox_for(locator.clone) end define_method("#{identifier}_checked?") do - @platform.check_checkbox_for_checked(locator.clone) + platform.check_checkbox_for_checked(locator.clone) end - - define_method("#{identifier}_exists?") do - @platform.check_checkbox_for_existence(locator.clone) - end - - alias_method "#{identifier}?".to_sym, "#{identifier}_exists?".to_sym - - define_method("#{identifier}_visible?") do - @platform.check_checkbox_for_visibility(locator.clone) - end - - alias_method "#{identifier}_?".to_sym, "#{identifier}_visible?".to_sym - - define_method("#{identifier}_enabled?") do - @platform.check_checkbox_for_enabled(locator.clone) - end - - alias_method "#{identifier}!".to_sym, "#{identifier}_enabled?".to_sym + + common_definition_methods(identifier, locator, 'checkbox_element') end - # Definition method for radios. Methods for the following actions will be created: - # * reference a radio (identifier_object, identifier_radio, identifier_radio_button) - # * check for existence (identifier?, identifier_exists?) - # * check for visibility (identifier_?, identifier_visible?) - # * check for enabled (identifier!, identifier_enabled?) - # * check for selected (identifier_set?, identifier_selected?) - # * set selected state (set_identifier, select_identifier) - # @param [Symbol] identifier the friendly name of the web object - # @param [optional, Hash] locator the key/values that identify the object - # @return [Object] instance of Symbiont::WebObjects::Radio def radio(identifier, locator) - define_method("#{identifier}_object") do - @platform.get_radio_for(locator.clone) - end + #alias_method "#{identifier}_radio".to_sym, "#{identifier}_object".to_sym + #alias_method "#{identifier}_radio_button".to_sym, "#{identifier}_object".to_sym - alias_method "#{identifier}_radio".to_sym, "#{identifier}_object".to_sym - alias_method "#{identifier}_radio_button".to_sym, "#{identifier}_object".to_sym - define_method("#{identifier}_selected?") do - @platform.check_radio_for_selected(locator.clone) + platform.check_radio_for_selected(locator.clone) end alias_method "#{identifier}_set?".to_sym, "#{identifier}_selected?".to_sym define_method("select_#{identifier}") do - @platform.select_radio_for(locator.clone) + platform.select_radio_for(locator.clone) end alias_method "set_#{identifier}".to_sym, "select_#{identifier}".to_sym - - define_method("#{identifier}_exists?") do - @platform.check_radio_for_existence(locator.clone) - end - - alias_method "#{identifier}?".to_sym, "#{identifier}_exists?".to_sym - - define_method("#{identifier}_visible?") do - @platform.check_radio_for_visibility(locator.clone) - end - - alias_method "#{identifier}_?".to_sym, "#{identifier}_visible?".to_sym - - define_method("#{identifier}_enabled?") do - @platform.check_radio_for_enabled(locator.clone) - end - - alias_method "#{identifier}!".to_sym, "#{identifier}_enabled?".to_sym + + common_definition_methods(identifier, locator, 'radio_element') end - + + alias_method :radio_button, :radio + + + + + # Definition method for tables. Methods for the following actions will be created: # * reference a table (identifier_object, identifier_table) # * check for existence (identifier?, identifier_exists?) # * check for visibility (identifier_?, identifier_visible?) # @param [Symbol] identifier the friendly name of the web object @@ -403,70 +194,44 @@ define_method("#{identifier}") do @platform.get_table_cell_text_for(locator.clone) end end - # Definition method for div objects. Methods for the following actions will be created: - # * reference a div (identifier_object, identifier_div) - # * check for existence (identifier?, identifier_exists?) - # * check for visibility (identifier_?, identifier_visible?) - # * get text from div (identifier) - # @param [Symbol] identifier the friendly name of the web object - # @param [optional, Hash] locator the key/values that identify the object - # @return [Object] instance of Symbiont::WebObjects::Div def div(identifier, locator) - define_method("#{identifier}_object") do - @platform.get_div_for(locator.clone) - end - - alias_method "#{identifier}_div".to_sym, "#{identifier}_object".to_sym - - define_method("#{identifier}_exists?") do - @platform.check_div_for_existence(locator.clone) - end - - alias_method "#{identifier}?".to_sym, "#{identifier}_exists?".to_sym - - define_method("#{identifier}_visible?") do - @platform.check_div_for_visibility(locator.clone) - end - - alias_method "#{identifier}_?".to_sym, "#{identifier}_visible?".to_sym - - define_method("#{identifier}") do - @platform.get_div_text_for(locator.clone) - end + common_definition_methods(identifier, locator, 'div_element') end - # Definition method for span objects. Methods for the following actions will be created: - # * reference a span (identifier_object, identifier_span) - # * check for existence (identifier?, identifier_exists?) - # * check for visibility (identifier_?, identifier_visible?) - # * get text from span (identifier) - # @param [Symbol] identifier the friendly name of the web object - # @param [optional, Hash] locator the key/values that identify the object - # @return [Object] instance of Symbiont::WebObjects::Span def span(identifier, locator) + common_definition_methods(identifier, locator, 'span_element') + end + + def common_definition_methods(identifier, locator, method) define_method("#{identifier}_object") do - @platform.get_span_for(locator.clone) + platform.send(method, locator.clone) end - - alias_method "#{identifier}_span".to_sym, "#{identifier}_object".to_sym - + define_method("#{identifier}_exists?") do - @platform.check_span_for_existence(locator.clone) + platform.send(method, locator.clone).exists? end - - alias_method "#{identifier}?".to_sym, "#{identifier}_exists?".to_sym - + define_method("#{identifier}_visible?") do - @platform.check_span_for_visibility(locator.clone) + platform.send(method, locator.clone).visible? end - - alias_method "#{identifier}_?".to_sym, "#{identifier}_visible?".to_sym - - define_method("#{identifier}") do - @platform.get_span_text_for(locator.clone) + + define_method("#{identifier}_enabled?") do + platform.send(method, locator.clone).enabled? end + + define_method("#{identifier}_text") do + platform.send(method, locator.clone).text + end + + element = method[0..method.index("_") - 1] + + alias_method "#{identifier}_#{element}".to_sym, "#{identifier}_object".to_sym + alias_method "#{identifier}?".to_sym, "#{identifier}_exists?".to_sym + alias_method "#{identifier}_?".to_sym, "#{identifier}_visible?".to_sym + alias_method "#{identifier}!".to_sym, "#{identifier}_enabled?".to_sym end + end # module: Generators end # module: Symbiont