Class: Gamera::Page

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Capybara::DSL, Visitable
Defined in:
lib/gamera/page.rb

Overview

This is a base class which implements common methods for page object classes.

You can use this to create a Ruby class which wraps a web page, providing an API for automating elements or processes on the page

Examples:

Page Object class for registration page

class NewRegistrationPage < Gamera::Page
  def initialize
    @url = 'http://example.com/registration/new'
    @url_matcher = /registration\/new/
  end

  # page elements
  def name_field
    find_field('Name')
  end

  def email_field
    find_field('Email Address')
  end

  def password_field
    find_field('Password')
  end

  def password_confirmation_field
    find_field('Confirm Password')
  end

  def instructions
  def instructions
    find('#instructions')
  end

  # page processes
  def save
    find_button('Save').click
  end

  def register_user(name:, email:, password:)
    name_field.set(name)
    email_field.set(email)
    password_field.set(password)
    password_confirmation_field.set(password)
    save
  end
end

# This could be used in a test or automation script, e.g.
...
reg_page = NewRegistrationPage.new
reg_page.visit
reg_page.register_user(name: 'Laurence Peltier',
                       email: '[email protected]',
                       password: 'super_secret')
...

Page class for general Rails page with flash messages

class RailsPage < Gamera::Page
  def flash_error_css
    'div.flash.error'
  end

  def flash_notice_css
    'div.flash.notice'
  end

  def flash_error
    find(flash_error_css)
  end

  def flash_notice
    find(flash_notice_css)
  end

  def has_flash_message?(message)
    has_css?(flash_notice_css, text: message)
  end

  def has_flash_error?(error)
    has_css?(flash_error_css, text: error)
  end

  def has_no_flash_error?
    has_no_css?(flash_error_css)
  end

  def has_no_flash_message?
    has_no_css?(flash_notice_css)
  end

  def has_submission_problems?
    has_flash_error?('There were problems with your submission')
  end

  def fail_if_submission_problems
    fail(SubmissionProblemsError, flash_error.text) if has_submission_problems?
  end
end

Constant Summary

Constants included from Visitable

Visitable::CAPYBARA_DEFAULT_MAX_WAIT_TIME

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Visitable

#displayed?, #visit, #with_refreshes

Constructor Details

#initialize(url, url_matcher = nil) ⇒ Page

Returns a new instance of Page.



143
144
145
146
# File 'lib/gamera/page.rb', line 143

def initialize(url, url_matcher = nil)
  @url = url
  @url_matcher = url_matcher || /#{url}/
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



139
140
141
# File 'lib/gamera/page.rb', line 139

def url
  @url
end

#url_matcherObject (readonly)

Returns the value of attribute url_matcher.



139
140
141
# File 'lib/gamera/page.rb', line 139

def url_matcher
  @url_matcher
end