Class: Howitzer::Web::Page

Inherits:
Object
  • Object
show all
Includes:
CapybaraMethodsProxy, ElementDsl, IframeDsl, PageDsl, PageValidator, SectionDsl, RSpec::Matchers, RSpec::Wait, Singleton
Defined in:
lib/howitzer/web/page.rb

Overview

This class represents a single web page. This is a parent class for all web pages

Direct Known Subclasses

BlankPage

Constant Summary collapse

UnknownPage =

:nodoc:

Class.new

Constants included from CapybaraMethodsProxy

CapybaraMethodsProxy::PROXIED_CAPYBARA_METHODS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PageValidator

#check_validations_are_defined!, included, validations

Methods included from SectionDsl

included

Methods included from CapybaraContextHolder

#capybara_context

Methods included from PageDsl

included

Methods included from IframeDsl

included

Methods included from ElementDsl

included

Methods included from CapybaraMethodsProxy

#click_alert_box

Constructor Details

#initializePage

Returns a new instance of Page.



165
166
167
168
169
# File 'lib/howitzer/web/page.rb', line 165

def initialize
  check_validations_are_defined!
  current_window.maximize if Howitzer.maximized_window &&
                             !%w[chrome headless_chrome].include?(Capybara.current_driver)
end

Class Method Details

.current_pageString

Tries to identify current page name or raise the error if ambiguous page matching

Returns:

  • (String)

    a page name

Raises:



64
65
66
67
68
69
# File 'lib/howitzer/web/page.rb', line 64

def self.current_page
  page_list = matched_pages
  return UnknownPage if page_list.count.zero?
  return page_list.first if page_list.count == 1
  raise Howitzer::AmbiguousPageMatchingError, ambiguous_page_msg(page_list)
end

.current_urlString

Returns current page url from browser.

Returns:

  • (String)

    current page url from browser



87
88
89
# File 'lib/howitzer/web/page.rb', line 87

def self.current_url
  Capybara.current_session.current_url
end

.displayed?(timeout = Howitzer.page_load_idle_timeout) ⇒ Boolean

Waits until a web page is opened

Parameters:

  • timeout (Integer) (defaults to: Howitzer.page_load_idle_timeout)

    time in seconds a required web page to be loaded

Returns:

  • (Boolean)

Raises:



76
77
78
79
80
81
82
83
# File 'lib/howitzer/web/page.rb', line 76

def self.displayed?(timeout = Howitzer.page_load_idle_timeout)
  end_time = ::Time.now + timeout
  until ::Time.now > end_time
    return true if opened?
    sleep(0.5)
  end
  raise Howitzer::IncorrectPageError, incorrect_page_msg
end

.expanded_url(params = {}, url_processor = nil) ⇒ String

Returns an expanded page url for the page opening

Parameters:

  • params (Array) (defaults to: {})

    placeholders and their values

  • url_processor (Class) (defaults to: nil)

    custom url processor. For details see Addressable gem

Returns:

  • (String)

Raises:



97
98
99
100
101
102
# File 'lib/howitzer/web/page.rb', line 97

def self.expanded_url(params = {}, url_processor = nil)
  if defined?(path_value)
    return "#{site_value}#{Addressable::Template.new(path_value).expand(params, url_processor)}"
  end
  raise Howitzer::NoPathForPageError, "Please specify path for '#{self}' page. Example: path '/home'"
end

.givenPage

Returns a singleton instance of the web page

Returns:



54
55
56
57
# File 'lib/howitzer/web/page.rb', line 54

def self.given
  displayed?
  instance
end

.inherited(subclass) ⇒ Object

This Ruby callback makes all inherited classes as singleton classes.



29
30
31
# File 'lib/howitzer/web/page.rb', line 29

def self.inherited(subclass)
  subclass.class_eval { include Singleton }
end

.open(validate: true, url_processor: nil, **params) ⇒ Page

Note:

It tries to open the page twice and then raises the error if a validation is failed

Opens a web page in browser

Parameters:

  • validate (Boolean) (defaults to: true)

    if fase will skip current page validation (is opened)

  • url_processor (Class) (defaults to: nil)

    custom url processor. For details see ‘addressable’ gem

  • params (Array)

    placeholder names and their values

Returns:



40
41
42
43
44
45
46
47
48
49
# File 'lib/howitzer/web/page.rb', line 40

def self.open(validate: true, url_processor: nil, **params)
  url = expanded_url(params, url_processor)
  modify_user_agent if Howitzer.user_agent.present?
  Howitzer::Log.info "Open #{name} page by '#{url}' url"
  retryable(tries: 2, logger: Howitzer::Log, trace: true, on: Exception) do |retries|
    Howitzer::Log.info 'Retry...' unless retries.zero?
    Capybara.current_session.visit(url)
  end
  given if validate
end

.path(value) ⇒ Object

DSL to specify an relative path pattern for the page opening

Examples:

class ArticlePage < Howitzer::Web::Page
  url '/articles/:id'
end
ArticlePage.open(id: 10)

Parameters:

  • value (String)

    a path pattern, for details please see Addressable gem

See Also:



117
118
119
120
# File 'lib/howitzer/web/page.rb', line 117

def path(value)
  define_singleton_method(:path_value) { value.to_s }
  private_class_method :path_value
end

.site(value) ⇒ Object

Note:

By default it specifies Howitzer.app_uri.site as a site

DSL to specify a site for the page opening

Examples:

class AuthPage < Howitzer::Web::Page
  site 'https:/example.com'
end

class LoginPage < AuthPage
  path '/login'
end

Parameters:

  • value (String)

    a site as combination of protocol, host and port



135
136
137
138
# File 'lib/howitzer/web/page.rb', line 135

def site(value)
  define_singleton_method(:site_value) { value }
  private_class_method :site_value
end

Instance Method Details

#reloadObject

Reloads current page in a browser



173
174
175
176
# File 'lib/howitzer/web/page.rb', line 173

def reload
  Howitzer::Log.info "Reload '#{current_url}'"
  visit current_url
end