Class: WebPage

Inherits:
Object
  • Object
show all
Extended by:
Howitzer::Capybara::DslEx
Includes:
Howitzer::Capybara::DslEx, Howitzer::Utils::PageValidator, LocatorStore, RSpec::Matchers, Singleton
Defined in:
lib/howitzer/web_page.rb

Direct Known Subclasses

BlankPage

Constant Summary collapse

BLANK_PAGE =
Deprecated.

, use BlankPage instead

'about:blank'
UnknownPage =
Class.new

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Howitzer::Utils::PageValidator

#check_validations_are_defined!, included, pages, validations

Methods included from LocatorStore

included

Constructor Details

#initializeWebPage

Returns a new instance of WebPage


118
119
120
121
# File 'lib/howitzer/web_page.rb', line 118

def initialize
  check_validations_are_defined!
  page.driver.browser.manage.window.maximize if settings.maximized_window
end

Class Method Details

.current_pageObject

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

Returns:

  • string - page name


90
91
92
93
94
95
96
97
98
99
100
# File 'lib/howitzer/web_page.rb', line 90

def self.current_page
  page_list = matched_pages
  if page_list.count.zero?
    UnknownPage
  elsif page_list.count > 1
    log.error Howitzer::AmbiguousPageMatchingError,
              "Current page matches more that one page class (#{page_list.join(', ')}).\n\tCurrent url: #{current_url}\n\tCurrent title: #{title}"
  elsif page_list.count == 1
    page_list.first
  end
end

.givenObject

Returns singleton instance of current web page

Returns:

  • WebPage - Singleton instance


53
54
55
56
# File 'lib/howitzer/web_page.rb', line 53

def self.given
  wait_for_opened
  self.instance
end

.inherited(subclass) ⇒ Object


20
21
22
23
# File 'lib/howitzer/web_page.rb', line 20

def self.inherited(subclass)
  subclass.class_eval { include Singleton }
  Howitzer::Utils::PageValidator.pages << subclass
end

.open(url = "#{app_url unless self == BlankPage}#{self::URL}") ⇒ Object

Opens web-site by given url

Parameters:

  • url - Url string that will be opened

Returns:

  • WebPage - New instance of current class


36
37
38
39
40
41
42
43
# File 'lib/howitzer/web_page.rb', line 36

def self.open(url = "#{app_url unless self == BlankPage}#{self::URL}")
  log.info "Open #{self.name} page by '#{url}' url"
  retryable(tries: 2, logger: log, trace: true, on: Exception) do |retries|
    log.info 'Retry...' unless retries.zero?
    visit url
  end
  given
end

.textObject

Returns body text of html page

Returns:

  • string - Body text


78
79
80
# File 'lib/howitzer/web_page.rb', line 78

def self.text
  page.find('body').text
end

.urlObject

Returns current url

Returns:

  • string - Current url


66
67
68
# File 'lib/howitzer/web_page.rb', line 66

def self.url
  self.current_url
end

.wait_for_opened(timeout = settings.timeout_small) ⇒ Object

Waits until web page is not opened, or raise error after timeout

Parameters:

  • time_out - Seconds that will be waiting for web page to be loaded


110
111
112
113
114
115
116
# File 'lib/howitzer/web_page.rb', line 110

def self.wait_for_opened(timeout=settings.timeout_small)
  end_time = ::Time.now + timeout
  until ::Time.now > end_time
    self.opened? ? return : sleep(0.5)
  end
  log.error Howitzer::IncorrectPageError, "Current page: #{self.current_page}, expected: #{self}.\n\tCurrent url: #{current_url}\n\tCurrent title: #{title}"
end

Instance Method Details

#click_alert_box(flag) ⇒ Object

Accepts or declines JS alert box by given flag

Parameters:

  • flag [TrueClass,FalseClass] - Determines accept or decline alert box


150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/howitzer/web_page.rb', line 150

def click_alert_box(flag)
  if %w[selenium selenium_dev sauce].include? settings.driver
    if flag
      page.driver.browser.switch_to.alert.accept
    else
      page.driver.browser.switch_to.alert.dismiss
    end
  else
    if flag
      page.evaluate_script('window.confirm = function() { return true; }')
    else
      page.evaluate_script('window.confirm = function() { return false; }')
    end
  end
end

#js_click(css_locator) ⇒ Object

Clicks on button or link using JS event call

Parameters:

  • css_locator - Css locator of link or button


174
175
176
177
# File 'lib/howitzer/web_page.rb', line 174

def js_click(css_locator)
  page.execute_script("$('#{css_locator}').trigger('click')")
  sleep settings.timeout_tiny
end

#reloadObject

Reloads current page


237
238
239
240
# File 'lib/howitzer/web_page.rb', line 237

def reload
  log.info "Reload '#{current_url}'"
  visit current_url
end

#tinymce_fill_in(name, options = {}) ⇒ Object

Fills in field that using Tinymce API

Parameters:

  • name - Frame name that contains Tinymce field

  • Hash - Not required options


132
133
134
135
136
137
138
139
140
# File 'lib/howitzer/web_page.rb', line 132

def tinymce_fill_in(name, options = {})
  if %w[selenium selenium_dev sauce].include? settings.driver
    page.driver.browser.switch_to.frame("#{name}_ifr")
    page.find(:css, '#tinymce').native.send_keys(options[:with])
    page.driver.browser.switch_to.default_content
  else
    page.execute_script("tinyMCE.get('#{name}').setContent('#{options[:with]}')")
  end
end

#titleObject

Returns Page title

Returns:

  • string - Page title


250
251
252
# File 'lib/howitzer/web_page.rb', line 250

def title
  page.title
end

#wait_for_ajax(timeout = settings.timeout_small, message = nil) ⇒ Object

Deprecated.

With Capybara 2.x it is extra :nocov:


182
183
184
185
186
187
188
189
# File 'lib/howitzer/web_page.rb', line 182

def wait_for_ajax(timeout=settings.timeout_small, message=nil)
  end_time = ::Time.now + timeout
  until ::Time.now > end_time
    return true if page.evaluate_script('$.active') == 0
    sleep 0.25
  end
  log.error message || 'Timed out waiting for ajax requests to complete'
end

#wait_for_title(expected_title, timeout = settings.timeout_small) ⇒ Object

Deprecated.

Waits until web is loaded with expected title

Parameters:

  • expected_title - Page title that will be waited for

  • time_out - Seconds that will be waiting for web-site to be loaded until raise error


222
223
224
225
226
227
228
229
230
# File 'lib/howitzer/web_page.rb', line 222

def wait_for_title(expected_title, timeout=settings.timeout_small)
  warn '[Deprecated] This method is deprecated, and will be removed in next version of Howitzer'
  end_time = ::Time.now + timeout
  until ::Time.now > end_time
    operator = expected_title.is_a?(Regexp) ? :=~ : :==
    return true if title.send(operator, expected_title).tap{|res| sleep 1 unless res}
  end
  log.error Howitzer::IncorrectPageError, "Current title: #{title}, expected:  #{expected_title}"
end

#wait_for_url(expected_url, timeout = settings.timeout_small) ⇒ Object

Deprecated.

Waits until web page is loaded

Parameters:

  • expected_url - Url that will be waiting for

  • time_out - Seconds that will be waiting for web-site to be loaded until raise error


202
203
204
205
206
207
208
209
210
# File 'lib/howitzer/web_page.rb', line 202

def wait_for_url(expected_url, timeout=settings.timeout_small)
  warn '[Deprecated] This method is deprecated, and will be removed in next version of Howitzer'
  end_time = ::Time.now + timeout
  until ::Time.now > end_time
    operator = expected_url.is_a?(Regexp) ? :=~ : :==
    return true if current_url.send(operator, expected_url).tap{|res| sleep 1 unless res}
  end
  log.error Howitzer::IncorrectPageError, "Current url: #{current_url}, expected:  #{expected_url}"
end