Module: SitePrism::ElementContainer

Included in:
Page, Section
Defined in:
lib/site_prism/element_container.rb

Overview

Contains methods applicable to both Pages and Sections. Note that they are mixed into the Page and Section classes so the methods below are used as class methods.

Instance Method Summary collapse

Instance Method Details

#element(element_name, element_locator) ⇒ 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

Examples:

class HomePage < SitePrism::Page
  element :search_link, 'div.search > a'
end
home = HomePage.new

#the element method created 2 methods...
home.search_link #=> returns the capybara element located by the element_locator parameter
home.has_search_link? #=> returns true if the capybara element as located by the element_locator exists, false if it doesn't

#The has_search_link? method allows use of magic matchers in rspec/cucumber:
home.should have_search_link
home.should_not have_search_link

Parameters:

  • element_name (Symbol)

    The name of the element

  • element_locator (String)

    The CSS locator to find the element



24
25
26
27
28
29
30
31
# File 'lib/site_prism/element_container.rb', line 24

def element element_name, element_locator
  add_element_name element_name
  create_existence_checker element_name, element_locator
  create_waiter element_name, element_locator
  define_method element_name.to_s do
    find_one element_locator
  end
end

#elements(collection_name, collection_locator) ⇒ 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

Examples:

class HomePage < SitePrism::Page
  elements :app_links, '.title-links > a'
end
home = HomePage.new

home.should have_app_links
home.app_links #=> [#<Capybara::Element tag="a">, #<Capybara::Element tag="a">, #<Capybara::Element tag="a">]
home.app_links.map {|link| link.text}.should == ['Finance', 'Maps', 'Blogs']

Parameters:

  • collection_name (Symbol)

    The name of the collection

  • collection_locator (String)

    The CSS locator that returns the list of elements in the collection



47
48
49
50
51
52
53
54
# File 'lib/site_prism/element_container.rb', line 47

def elements collection_name, collection_locator
  add_element_name collection_name
  create_existence_checker collection_name, collection_locator
  create_waiter collection_name, collection_locator
  define_method collection_name.to_s do
    find_all collection_locator
  end
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’).

Parameters:

  • the (Symbol)

    method name to be called against this page or section to return an instance of the Section class

  • the (Class)

    class that models this area of the page

  • the (String)

    CSS locator for the root element of the section on this page/section



82
83
84
85
86
87
88
89
# File 'lib/site_prism/element_container.rb', line 82

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.



92
93
94
95
96
97
98
99
100
101
# File 'lib/site_prism/element_container.rb', line 92

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