Class: SitePrism::Page

Inherits:
Object
  • Object
show all
Extended by:
ElementContainer
Includes:
Capybara::DSL, ElementChecker
Defined in:
lib/site_prism/page.rb

Overview

Subclasses of Page represent pages in your app.

class Home < SitePrism::Page
end

The above is an example of how to make a class representing the home page. There are a number of properties that can be set on a page - here is an example of a more fully spec’ed out page:

class Home < SitePrism::Page
  set_url "/"
  set_url_matcher /\/home.htm$/
end

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ElementContainer

add_element_name, element, element_names, elements, section, sections

Methods included from ElementChecker

#all_there?

Class Method Details

.set_url(page_url) ⇒ Object

Set the url associated with this page

Examples:

class SearchPage < SitePrism::Page
  set_url "/search.htm"
end

Parameters:

  • page_url (String)

    the portion of the url that identifies this page when appended onto Capybara’s app_host. Calling #load causes Capybara to visit this page.



46
47
48
# File 'lib/site_prism/page.rb', line 46

def self.set_url page_url
  @url = page_url
end

.set_url_matcher(page_url_matcher) ⇒ Object

Set the url matcher associated with this page

Examples:

class SearchPage < SitePrism::Page
  set_url_matcher /\/search.htm$/
end

Parameters:

  • page_url_matcher (Regexp)

    a regular expression that when compared to the current browser url will match if we’re on this page or not match if we’re not on this page



56
57
58
# File 'lib/site_prism/page.rb', line 56

def self.set_url_matcher page_url_matcher
  @url_matcher = page_url_matcher
end

.urlString

Get the url associated with this page

Returns:

  • (String)

    the url originally set in set_url

See Also:



63
64
65
# File 'lib/site_prism/page.rb', line 63

def self.url
  @url
end

.url_matcherRegexp

Get the url matcher associated with this page

Returns:

See Also:



70
71
72
# File 'lib/site_prism/page.rb', line 70

def self.url_matcher
  @url_matcher
end

Instance Method Details

#displayed?Boolean

Checks to see if we’re on this page or not

Examples:

class SearchPage < SitePrism::Page
  set_url_matcher /\/search.htm$/
end
search_page = SearchPage.new
search_page.load
puts "We're on the search page" if search_page.displayed?
search_page.should be_displayed

Returns:

  • (Boolean)

    true if the browser’s current url matches the url_matcher that has been set, false if it doesn’t

Raises:



35
36
37
38
# File 'lib/site_prism/page.rb', line 35

def displayed?
  raise SitePrism::NoUrlMatcherForPage if url_matcher.nil?
  !(page.current_url =~ url_matcher).nil?
end

#loadObject

Visits the url associated with this page

Raises:



19
20
21
22
# File 'lib/site_prism/page.rb', line 19

def load
  raise SitePrism::NoUrlForPage if url.nil?
  visit url
end

#titleString?

Gets the title of the current page

Returns:

  • (String, nil)

    the text value of the title element within the page’s head block



88
89
90
91
# File 'lib/site_prism/page.rb', line 88

def title
  title_selector = 'html > head > title'
  using_wait_time(0) { page.find(title_selector).text if page.has_selector?(title_selector) }
end

#urlObject

Get the url associated with this page

See Also:



76
77
78
# File 'lib/site_prism/page.rb', line 76

def url
  self.class.url
end

#url_matcherObject

Get the url matcher associated with this page

See Also:



82
83
84
# File 'lib/site_prism/page.rb', line 82

def url_matcher
  self.class.url_matcher
end