Class: TestCentricity::Browsers

Inherits:
Object
  • Object
show all
Includes:
Capybara::DSL
Defined in:
lib/testcentricity_web/browser_helper.rb

Class Method Summary collapse

Class Method Details

.browser_size(browser, orientation) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/testcentricity_web/browser_helper.rb', line 170

def self.browser_size(browser, orientation)
  device = get_device(browser)
  if device
    width = device[:css_width]
    height = device[:css_height]
    default_orientation = device[:default_orientation].to_sym
    if orientation
      Environ.device_orientation = orientation
      size = if orientation == default_orientation
               [width, height]
             else
               [height, width]
             end
    else
      Environ.device_orientation = device[:default_orientation]
      size = [width, height]
    end
  else
    size = [1650, 1000]
  end
  size
end

.close_all_browser_instancesObject



104
105
106
107
108
109
# File 'lib/testcentricity_web/browser_helper.rb', line 104

def self.close_all_browser_instances
  Capybara.page.driver.browser.window_handles.each do |handle|
    Capybara.page.driver.browser.switch_to.window(handle)
    Capybara.page.driver.browser.close
  end
end

.close_current_browser_instanceObject



115
116
117
118
# File 'lib/testcentricity_web/browser_helper.rb', line 115

def self.close_current_browser_instance
  Capybara.page.driver.browser.close
  Capybara.page.driver.browser.switch_to.window(Capybara.page.driver.browser.window_handles.last)
end

.close_old_browser_instanceObject



120
121
122
123
124
# File 'lib/testcentricity_web/browser_helper.rb', line 120

def self.close_old_browser_instance
  Capybara.page.driver.browser.switch_to.window(Capybara.page.driver.browser.window_handles.first)
  Capybara.page.driver.browser.close
  Capybara.page.driver.browser.switch_to.window(Capybara.page.driver.browser.window_handles.last)
end

.delete_all_cookiesObject



134
135
136
137
138
139
140
141
142
143
# File 'lib/testcentricity_web/browser_helper.rb', line 134

def self.delete_all_cookies
  if Capybara.current_driver == :selenium
    browser = Capybara.current_session.driver.browser
    if browser.respond_to?(:manage) && browser.manage.respond_to?(:delete_all_cookies)
      browser.manage.delete_all_cookies
    else
      raise 'Could not clear cookies.'
    end
  end
end

.maximize_browserObject

Maximizes the selenium browser window.

Examples:

Browsers.maximize_browser


56
57
58
59
# File 'lib/testcentricity_web/browser_helper.rb', line 56

def self.maximize_browser
  window = Capybara.page.driver.browser.manage.window
  window.maximize
end

.mobile_device_agent(browser) ⇒ Object



153
154
155
156
157
158
# File 'lib/testcentricity_web/browser_helper.rb', line 153

def self.mobile_device_agent(browser)
  device = get_device(browser)
  agent_string = device[:user_agent]
  raise "Device '#{device}' is not defined" unless agent_string
  agent_string
end

.mobile_device_name(browser) ⇒ Object



160
161
162
163
164
165
166
167
168
# File 'lib/testcentricity_web/browser_helper.rb', line 160

def self.mobile_device_name(browser)
  device = get_device(browser)
  raise "Device '#{browser}' is not defined" unless device
  name = device[:name]
  raise "Device '#{device}' is not defined" unless name
  Environ.device_os = device[:os]
  Environ.device_type = device[:type]
  name
end

Emulates clicking the web browser's Back button. Navigates back by one page on the browser’s history.

Examples:

Browsers.navigate_back


87
88
89
# File 'lib/testcentricity_web/browser_helper.rb', line 87

def self.navigate_back
  Capybara.page.driver.browser.navigate.back
end

Emulates clicking the web browser's Forward button. Navigates forward by one page on the browser’s history.

Examples:

Browsers.navigate_forward


96
97
98
# File 'lib/testcentricity_web/browser_helper.rb', line 96

def self.navigate_forward
  Capybara.page.driver.browser.navigate.forward
end

.num_browser_instancesObject



111
112
113
# File 'lib/testcentricity_web/browser_helper.rb', line 111

def self.num_browser_instances
  Capybara.page.driver.browser.window_handles.count
end

.refresh_browserObject

Refreshes the selenium browser.

Examples:

Browsers.refresh_browser


78
79
80
# File 'lib/testcentricity_web/browser_helper.rb', line 78

def self.refresh_browser
  Capybara.page.driver.browser.navigate.refresh
end

.scroll_to(x_loc, y_loc) ⇒ Object

Scroll to location

Examples:

editor_page.scroll_to(200, 400)


34
35
36
# File 'lib/testcentricity_web/browser_helper.rb', line 34

def self.scroll_to(x_loc, y_loc)
  Capybara.page.execute_script "window.scrollBy(#{x_loc},#{y_loc})"
end

.scroll_to_bottomObject

Scroll to bottom of page

Examples:

editor_page.scroll_to_bottom


16
17
18
# File 'lib/testcentricity_web/browser_helper.rb', line 16

def self.scroll_to_bottom
  Capybara.page.execute_script "window.scrollTo(0, document.body.scrollHeight)"
end

.scroll_to_topObject

Scroll to top of page

Examples:

editor_page.scroll_to_top


25
26
27
# File 'lib/testcentricity_web/browser_helper.rb', line 25

def self.scroll_to_top
  Capybara.page.execute_script "window.scrollTo(0, -document.body.scrollHeight)"
end

.set_browser_window_position(x, y) ⇒ Object

Sets the position of the browser window.

Examples:

Browsers.set_browser_window_position([100, 300])

Parameters:

  • x (Integer)

    the desired x coordinate

  • y (Integer)

    the desired y coordinate



68
69
70
71
# File 'lib/testcentricity_web/browser_helper.rb', line 68

def self.set_browser_window_position(x, y)
  window = Capybara.page.driver.browser.manage.window
  window.move_to(x, y)
end

.set_browser_window_size(resolution) ⇒ Object

Sets the size of the browser window.

Examples:

Browsers.set_browser_window_size([1024, 768])

Parameters:

  • resolution (Array)

    the desired [width, height]



44
45
46
47
48
49
# File 'lib/testcentricity_web/browser_helper.rb', line 44

def self.set_browser_window_size(resolution)
  resolution = resolution.split(',') if resolution.is_a?(String)
  window = Capybara.page.driver.browser.manage.window
  window.resize_to(resolution[0], resolution[1])
  Environ.browser_size = [resolution[0].to_i, resolution[1].to_i]
end

.set_device_orientation(orientation) ⇒ Object



145
146
147
148
149
150
151
# File 'lib/testcentricity_web/browser_helper.rb', line 145

def self.set_device_orientation(orientation)
  if Environ.is_mobile? && !Environ.is_device?
    Browsers.set_browser_window_size(Browsers.browser_size(Environ.browser.to_s, orientation))
  else
    puts '***** Cannot change device orientation of desktop web browsers or remote devices *****'
  end
end

.suppress_js_alertsObject



126
127
128
# File 'lib/testcentricity_web/browser_helper.rb', line 126

def self.suppress_js_alerts
  Capybara.page.execute_script('window.alert = function() {}')
end

.suppress_js_leave_page_modalObject



130
131
132
# File 'lib/testcentricity_web/browser_helper.rb', line 130

def self.suppress_js_leave_page_modal
  Capybara.page.execute_script('window.onbeforeunload = null')
end

.switch_to_new_browser_instanceObject



100
101
102
# File 'lib/testcentricity_web/browser_helper.rb', line 100

def self.switch_to_new_browser_instance
  Capybara.page.driver.browser.switch_to.window(Capybara.page.driver.browser.window_handles.last)
end