1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00

Add Window#fullscreen and support in Selenium driver

This commit is contained in:
Thomas Walpole 2018-06-21 11:30:26 -07:00
parent 3a56e98319
commit 7b4f8ffb15
4 changed files with 61 additions and 2 deletions

View file

@ -90,7 +90,11 @@ class Capybara::Driver::Base
end
def maximize_window(handle)
raise Capybara::NotSupportedByDriverError, 'Capybara::Driver::Base#maximize_current_window'
raise Capybara::NotSupportedByDriverError, 'Capybara::Driver::Base#maximize_window'
end
def fullscreen_window(handle)
raise Capybara::NotSupportedByDriverError, 'Capybara::Driver::Base#fullscreen_window'
end
def close_window(handle)

View file

@ -217,7 +217,18 @@ class Capybara::Selenium::Driver < Capybara::Driver::Base
if marionette? && (window_size(handle) == [width, height])
{}
else
browser.manage.window.resize_to(width, height)
begin
browser.manage.window.resize_to(width, height)
rescue Selenium::WebDriver::Error::UnknownError => e
if chrome? && e.message =~ /failed to change window state/
# Chromedriver doesn't wait long enough for state to change when coming out of fullscreen
# and raises unnecessary error. Wait a bit and try again.
sleep 0.5
browser.manage.window.resize_to(width, height)
else
raise
end
end
end
end
end
@ -229,6 +240,22 @@ class Capybara::Selenium::Driver < Capybara::Driver::Base
sleep 0.1 # work around for https://code.google.com/p/selenium/issues/detail?id=7405
end
def fullscreen_window(handle)
within_given_window(handle) do
window = browser.manage.window
bridge = browser.send(:bridge)
if bridge.respond_to?(:full_screen_window)
window.full_screen
elsif chrome?
result = bridge.http.call(:post, "session/#{bridge.session_id}/window/fullscreen", {})
result['value']
else
raise Capybara::NotSupportedByDriverError, "Fullscreen is not supported by Capybara::Selenium::Driver for #{browser_name}"
end
end
end
def close_window(handle)
raise ArgumentError, "Not allowed to close the primary window" if handle == window_handles.first
within_given_window(handle) do

View file

@ -184,4 +184,21 @@ Capybara::SpecHelper.spec Capybara::Window, requires: [:windows] do
expect(ow_height).to be > 300
end
end
describe '#fullscreen' do
before do
@initial_size = @session.current_window.size
end
after do
@session.current_window.resize_to(*@initial_size)
sleep 0.5
end
it "should be able to fullscreen the window" do
expect do
@session.current_window.fullscreen
end.not_to raise_error
end
end
end

View file

@ -101,6 +101,17 @@ module Capybara
wait_for_stable_size { @driver.maximize_window(handle) }
end
##
# Fullscreen window.
#
# If a particular driver doesn't have concept of fullscreen it may not support this method.
#
# @macro about_current
#
def fullscreen
@driver.fullscreen_window(handle)
end
def eql?(other)
other.is_a?(self.class) && @session == other.session && @handle == other.handle
end