Module: SitePrism::ElementContainer
Overview
Instance Method Summary collapse
-
#add_element_name(element_name) ⇒ Object
Adds the element name to the list of known elements.
-
#element(element_name, element_locator = nil) ⇒ Object
Creates two methods; the first method has the same name as the element_name parameter and returns the capybara element located by the element_locator parameter when the method is called.
-
#element_names ⇒ Object
Returns list of known element names.
-
#elements(collection_name, collection_locator = nil) ⇒ Object
(also: #collection)
Works in the same way as Page.element in that it will generate two methods; one to check existence of the element (in the format ‘has_#element_name?’), and another to return not a single element, but an array of elements found by the css locator.
-
#section(section_name, section_class, section_locator) ⇒ Object
Creates a method that returns an instance of a Section.
-
#sections(section_collection_name, section_class, section_collection_locator) ⇒ Object
Works in the same way as Page.section but instead of it returning one section, it returns an array of them.
Instance Method Details
#add_element_name(element_name) ⇒ Object
Adds the element name to the list of known elements
116 117 118 119 |
# File 'lib/site_prism/element_container.rb', line 116 def add_element_name element_name @element_names ||= [] @element_names << element_name end |
#element(element_name, element_locator = nil) ⇒ Object
Creates two methods; the first method has the same name as the element_name parameter and returns the capybara element located by the element_locator parameter when the method is called. The second method generated has a name with a format of: ‘has_#element_name?’ which returns true if the element as located by the element_locator parameter exists, false if it doesn’t
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/site_prism/element_container.rb', line 24 def element element_name, element_locator = nil if element_locator.nil? define_method element_name.to_s do raise SitePrism::NoLocatorForElement.new("#{self.class.name} => :#{element_name} needs a locator") end else add_element_name element_name.to_s define_method element_name.to_s do find_one element_locator end end create_existence_checker element_name, element_locator create_waiter element_name, element_locator end |
#element_names ⇒ Object
Returns list of known element names
122 123 124 |
# File 'lib/site_prism/element_container.rb', line 122 def element_names @element_names end |
#elements(collection_name, collection_locator = nil) ⇒ Object Also known as: collection
Works in the same way as Page.element in that it will generate two methods; one to check existence of the element (in the format ‘has_#element_name?’), and another to return not a single element, but an array of elements found by the css locator
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/site_prism/element_container.rb', line 53 def elements collection_name, collection_locator = nil if collection_locator.nil? define_method collection_name.to_s do raise SitePrism::NoLocatorForElement.new("#{self.class.name} => :#{element_name} needs a locator") end else add_element_name collection_name define_method collection_name.to_s do find_all collection_locator end end create_existence_checker collection_name, collection_locator create_waiter collection_name, collection_locator end |
#section(section_name, section_class, section_locator) ⇒ Object
Creates a method that returns an instance of a Section. If a page contains a common section (eg: a search area) that appears on many pages, create a Section for it and then expose it in each Page that contains the section. Say a search engine website displays the search field and search button on each page and they always have the same IDs, they should be extracted into a Section that would look something like this:
class SearchArea < SitePrism::Section
element :search_field, '.q'
element :search_button, '.btnK'
end
…then that section could be added to any page as follows:
class SearchPage < SitePrism::Page
section :search_area, SearchArea, '.tsf-p'
end
class SearchResultsPage < SitePrism::Page
section :search_again, SearchArea, '.tsf-p table'
end
The SearchArea section appears on both pages, but can be invoked by methods specific to the page (eg: ‘search_area’ and ‘search_again’) and the root element for the section can be different on the page (eg: ‘.tsf-p’ and ‘.tsf-p table’).
94 95 96 97 98 99 100 101 |
# File 'lib/site_prism/element_container.rb', line 94 def section section_name, section_class, section_locator add_element_name section_name create_existence_checker section_name, section_locator create_waiter section_name, section_locator define_method section_name do section_class.new find_one section_locator end end |
#sections(section_collection_name, section_class, section_collection_locator) ⇒ Object
Works in the same way as Page.section but instead of it returning one section, it returns an array of them.
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/site_prism/element_container.rb', line 104 def sections section_collection_name, section_class, section_collection_locator add_element_name section_collection_name create_existence_checker section_collection_name, section_collection_locator create_waiter section_collection_name, section_collection_locator define_method section_collection_name do find_all(section_collection_locator).collect do |element| section_class.new element end end end |