Module: PageObject::PageClassMethods

Defined in:
lib/site-object/page.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#has_fragmentObject (readonly)

Returns the value of attribute has_fragment.



77
78
79
# File 'lib/site-object/page.rb', line 77

def has_fragment
  @has_fragment
end

#page_attributesObject (readonly)

Returns the value of attribute page_attributes.



77
78
79
# File 'lib/site-object/page.rb', line 77

def page_attributes
  @page_attributes
end

#page_elementsObject (readonly)

Returns the value of attribute page_elements.



77
78
79
# File 'lib/site-object/page.rb', line 77

def page_elements
  @page_elements
end

#page_urlObject (readonly)

Returns the value of attribute page_url.



77
78
79
# File 'lib/site-object/page.rb', line 77

def page_url
  @page_url
end

#url_matcherObject (readonly)

Returns the value of attribute url_matcher.



77
78
79
# File 'lib/site-object/page.rb', line 77

def url_matcher
  @url_matcher
end

#url_templateObject (readonly)

Returns the value of attribute url_template.



77
78
79
# File 'lib/site-object/page.rb', line 77

def url_template
  @url_template
end

Instance Method Details

#disable_automatic_navigationObject

DEPRECATED. Use the set_attributes method instead. This method can be used to disable page navigation when defining a page class (it sets an instance variable called @navigation during initialization.) The use case for this is a page that can’t be accessed directly and requires some level of browser interaction to reach. To disable navigation:

class SomePage < SomeSite::Page
  disable_automatic_navigation true
end

When navigation is disabled there will be no automatic navigation when the page is called. If the current page is not the page that you want a SiteObject::WrongPageError will be raised. If the visit method is called on the page a SiteObject::PageNavigationNotAllowedError will be raised.



94
95
96
97
98
99
# File 'lib/site-object/page.rb', line 94

def disable_automatic_navigation
  puts "The disable_automatic_navigation method is deprecated and will be removed in a future release. Use the set_attributes method in place of this one in the class definition. See documentation for more details."
  @page_attributes ||= []
  @page_attributes << :navigation_disabled
  @navigation_disabled = true
end

#element(name, &block) ⇒ Object Also known as: el

Used to define access to a single HTML element on a page. This method takes two arguments:

  • A symbol representing the element you are defining. This symbol is used to create an accessor method on the page object.

  • A block where access to the HTML element gets defined.

Example: The page you are working with has a “First Name” field:

element(:first_name) { |b| b.text_field(:id 'signup-first-name') }

In the example above, the block argument ‘b’ is the browser object that will get passed down from the site to the page and used when the page needs to access the element. You can actually use any label for the block argument but it’s recommended that you use something like ‘b’ or ‘browser’ consistently here because it’s always going to be some sort of browser object.

When page objects get initialized they’ll create an accessor method for the element and you can then work with the element in the same way you’d work with it in Watir or Selenium.

The element method is aliased to ‘el’ and using this alias is recommended as it saves space:

el(:first_name) { |b| b.text_field(:id 'signup-first-name') }


121
122
123
124
125
126
127
# File 'lib/site-object/page.rb', line 121

def element(name, &block)
  @page_elements ||= []
  @page_elements << name.to_sym
  define_method(name) do
    block.call(@browser)
  end
end

#page_template?Boolean



173
174
175
176
# File 'lib/site-object/page.rb', line 173

def page_template?
  @page_attributes ||= []
  @page_attributes.include? :page_template
end

#query_argumentsObject



183
184
185
# File 'lib/site-object/page.rb', line 183

def query_arguments
  required_arguments.find { |x| @url_template.pattern =~ /\?.*#{x}=*/ }
end

#required_argumentsObject

Returns an array of symbols representing the required arguments for the page’s page URL.



179
180
181
# File 'lib/site-object/page.rb', line 179

def required_arguments
  @arguments ||= @url_template.keys.map { |k| k.to_sym }
end

#set_attributes(*args) ⇒ Object

Allows you to set special page attributes that affect page behavior. The two page attributes currently supported are :navigation_disabled and :page_template:

  • When :navigation_disabled is specified as a page attribute, all automatic and manual browser navigation is disabled. If you call the page’s page methods automatic navigation is turned off – it won’t automatically load the page for you. And it the method will raise a PageNavigationNotAllowedError if you call the page’s accessor method while you aren’t actually on the page. And finally, the page’s visit method is disabled. This attribute is useful only when you have a page that can’t be automatically navigated to, in which case all of the navigation features described above wouldn’t work anyway.

  • When :page_template is specified as a page attribute, the site object won’t create an accessor method for the page when initializing and also won’t include the page when calling the site object’s pages method. This allows you to define a page object for inheritance purposes only. The idea behind this is to put common features one or more of these templates, which won’t get used directly. Then your other page objects that you actually do want to use can inherit from one of the templates, gaining all of its features. For example, you can put things like a logout link or common menus into a template and then have all of the page objects that need those features inherit from the template and get those features automatically.

If an unsupported attribute is specified a PageConfigError will be raised.

Usage:

set_attributes :attr1, :attr2


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/site-object/page.rb', line 157

def set_attributes(*args)
  @page_attributes ||= []
  args.each do |arg|
    case arg
    when :navigation_disabled
      @navigation_disabled = true
    when :page_template
      @page_template = true
    else
      raise SiteObject::PageConfigError, "Unsupported page attribute argument: #{arg} for #{self} page definition. Argument class: #{arg.class}. Arguments must be one or more of the following symbols: :navigation_disabled, :template."
    end
  end

  @page_attributes = args
end

#set_url(url) ⇒ Object

Used to define the full or relative URL to the page. Typically, you will almost always want to use this method when defining a page object (but see notes below.) The URL can be defined in a number of different ways. Here are some examples using Google News:

Relative URL

set_url "/nwshp?hl=en"

Relative URLs are most commonly used when defining page objects. The idea here is that you can change the base_url when calling the site object, which allows you to use the same code across multiple test environments by changing the base_url as you initialize a site object.

Relative URL with URL Templating

set_url "/nwshp?hl={language}"

This takes the relative URL example one step further, allowing you to set the page’s parameters. Note that the the language specified in the first relative URL example (‘en’) was replaced by ‘language’ in this one. Siteobject uses the Addressable library, which supports this kind of templating. When you template a value in the URL, the page object will allow you to specify the templated value when it’s being initialized. Here’s an example of how this works using a news site. Here’s the base site object class:

class NewsSite
  include SiteObject
end

Here’s a page object for the news page, templating the language value in the URL:

class NewsPage < NewsSite::Page
  set_url "/news?l={language}"
end

After you’ve initialized the site object you can load the Spanish or French versions of the page by changing the hash argument used to call the page from the site object:

site = NewsSite.new(base_url: "http://news.somesite.com")
site.news_page(language: 'es')
site.news_page(language: 'fr')

In addition to providing a hash of templated values when initializing a page you can also use an object, as long as that object responds to all of the templated arguments in the page’s URL definition. Here’s a simple class that has a language method that we can use for the news page described above:

class Country
  attr_reader :language

  def initialize(lang)
    @language = lang
  end
end

In the example below, the Country class is used to create a new new country object called ‘c’. This object has been initialized with a Spanish language code and the news page will load the spanish version of the page when it’s called with the country object.

site = NewsSite.new(base_url: "http://news.somesite.com")
c = Country.new('es')
=> <Country:0x007fcb0dc67f98 @language="es">
c.language
=> 'es'
site.news_page(c)
=> <NewsPage:0x003434546566>

If one or more URL parameters are missing when the page is getting initialized then the page will look at the hash arguments used to initialize the site. If the argument the page needs is defined in the site’s initialization arguments it will use that. For example, if the site object is initialized with a port, subdomain, or any other argument you can use those values when defining a page URL. Example:

class ConfigPage < MySite::Page
  set_url "/foo/{subdomain}/config"
end

site = MySite.new(subdomain: 'foo')
=> <MySite:0x005434546511>
site.configuration_page # No need to provide a subdomain here as long as the site object has it.
=> <ConfigPage:0x705434546541>

Full URL

set_url "http://news.google.com/nwshp?hl=en"

Every once in a while you may not want to use a base URL that has been defined. This allows you to do that. Just define a complete URL for that page object and that’s what will get used; the base_url will be ignored.

No URL

The set_url method is not mandatory. when defining a page. If you don’t use set_url in the page definition then the page will defined the base_url as the page’s URL.



277
278
279
# File 'lib/site-object/page.rb', line 277

def set_url(url)
  url ? @page_url = url : nil
end

#set_url_matcher(regexp) ⇒ Object

Optional. Allows you to specify a fallback mechanism for checking to see if the correct page is being displayed. This only gets used in cases where the primary mechanism for checking a page (the URL template defined by Page#set_url) fails to match the current browser URL. When that happens the regular expression defined here will be applied and the navigation check will pass if the regular expression matches the current browser URL.

In most cases, you won’t need to define a URL matcher and should just rely on the default page matching that uses the page’s URL template. The default matching should work fine for most cases.



299
300
301
# File 'lib/site-object/page.rb', line 299

def set_url_matcher(regexp)
  regexp ? @url_matcher = regexp : nil
end

#set_url_template(base_url) ⇒ Object



281
282
283
284
285
286
287
288
289
# File 'lib/site-object/page.rb', line 281

def set_url_template(base_url)
  case @page_url
  when /(http:\/\/|https:\/\/)/i
    @url_template = Addressable::Template.new(@page_url)
  else
    @url_template = Addressable::Template.new(Addressable::URI.parse("#{base_url}#{@page_url}"))
  end
  @has_fragment = @url_template.pattern =~ /#/
end

#use_features(*args) ⇒ Object

Used to import page features for use within the page. Example:

class ConfigPage < MySite::Page
  use_features :footer, :sidebar
end

Then, once the page object has been initialized:

site.config_page.footer.about.click

Use the PageFeature class to define page features.



314
315
316
317
318
319
320
# File 'lib/site-object/page.rb', line 314

def use_features(*args)
  if self.page_features
    args.each { |feature| self.page_features << feature }
  else
    self.page_features = args
  end
end