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

Add maximize window

This commit is contained in:
Andrey Botalov 2014-04-10 10:20:27 +03:00
parent c157ef4e61
commit 397be95d09
10 changed files with 53 additions and 10 deletions

View file

@ -25,7 +25,9 @@ before_install:
- sudo apt-get update - sudo apt-get update
- sudo apt-get install google-chrome-stable --force-yes - sudo apt-get install google-chrome-stable --force-yes
- sudo chmod 1777 /dev/shm - sudo chmod 1777 /dev/shm
- sudo apt-get install fluxbox -y --force-yes
before_script: before_script:
- sh -e /etc/init.d/xvfb start
- export DISPLAY=:99.0 - export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
- fluxbox > /dev/null 2>&1 &
script: "bundle exec rake travis" script: "bundle exec rake travis"

View file

@ -63,6 +63,10 @@ class Capybara::Driver::Base
raise Capybara::NotSupportedByDriverError, 'Capybara::Driver::Base#resize_window_to' raise Capybara::NotSupportedByDriverError, 'Capybara::Driver::Base#resize_window_to'
end end
def maximize_current_window
raise Capybara::NotSupportedByDriverError, 'Capybara::Driver::Base#maximize_current_window'
end
def close_current_window def close_current_window
raise Capybara::NotSupportedByDriverError, 'Capybara::Driver::Base#close_window' raise Capybara::NotSupportedByDriverError, 'Capybara::Driver::Base#close_window'
end end

View file

@ -139,6 +139,10 @@ class Capybara::Selenium::Driver < Capybara::Driver::Base
browser.manage.window.resize_to(width, height) browser.manage.window.resize_to(width, height)
end end
def maximize_current_window
browser.manage.window.maximize
end
def close_current_window def close_current_window
browser.close browser.close
end end

View file

@ -417,7 +417,7 @@ module Capybara
# 3. Switches back (this step will be invoked even if exception will happen at second step) # 3. Switches back (this step will be invoked even if exception will happen at second step)
# #
# @overload within_window(window) { do_something } # @overload within_window(window) { do_something }
# @param [Capybara::Window] instance of Capybara::Window class # @param window [Capybara::Window] instance of `Capybara::Window` class
# that will be switched to # that will be switched to
# @raise [driver#no_such_window_error] if unexistent (e.g. closed) window was passed # @raise [driver#no_such_window_error] if unexistent (e.g. closed) window was passed
# @overload within_window(proc_or_lambda) { do_something } # @overload within_window(proc_or_lambda) { do_something }

View file

@ -1,4 +1,4 @@
Capybara::SpecHelper.spec '#become_closed', requires: [:windows] do Capybara::SpecHelper.spec '#become_closed', requires: [:windows, :js] do
before(:each) do before(:each) do
@window = @session.current_window @window = @session.current_window
@session.visit('/with_windows') @session.visit('/with_windows')
@ -48,7 +48,7 @@ Capybara::SpecHelper.spec '#become_closed', requires: [:windows] do
it 'should raise error if value of default_wait_time is less than timeout' do it 'should raise error if value of default_wait_time is less than timeout' do
@session.within_window @other_window do @session.within_window @other_window do
@session.execute_script('setTimeout(function(){ window.close(); }, 700);') @session.execute_script('setTimeout(function(){ window.close(); }, 800);')
end end
Capybara.using_wait_time 0.4 do Capybara.using_wait_time 0.4 do
expect do expect do

View file

@ -18,7 +18,7 @@ Capybara::SpecHelper.spec '#open_new_window', requires: [:windows] do
expect(@session.current_url).to eq('about:blank') expect(@session.current_url).to eq('about:blank')
end end
it 'should open window with changable content' do it 'should open window with changeable content' do
window = @session.open_new_window window = @session.open_new_window
@session.within_window window do @session.within_window window do
@session.visit '/with_html' @session.visit '/with_html'

View file

@ -47,7 +47,7 @@ Capybara::SpecHelper.spec '#window_opened_by', requires: [:windows] do
end end
it 'should find window if default_wait_time is more than timeout' do it 'should find window if default_wait_time is more than timeout' do
Capybara.using_wait_time 1 do Capybara.using_wait_time 1.2 do
window = @session.window_opened_by do window = @session.window_opened_by do
@session.find(:css, '#openWindowWithTimeout').click @session.find(:css, '#openWindowWithTimeout').click
end end

View file

@ -81,7 +81,7 @@ Capybara::SpecHelper.spec Capybara::Window, requires: [:windows] do
end end
describe '#size' do describe '#size' do
it 'should return size of whole window' do it 'should return size of whole window', requires: [:js] do
expect(@session.current_window.size).to eq @session.evaluate_script("[window.outerWidth, window.outerHeight];") expect(@session.current_window.size).to eq @session.evaluate_script("[window.outerWidth, window.outerHeight];")
end end
@ -96,7 +96,7 @@ Capybara::SpecHelper.spec Capybara::Window, requires: [:windows] do
end end
describe '#resize_to' do describe '#resize_to' do
it 'should be able to resize window' do it 'should be able to resize window', requires: [:js] do
width, height = @session.evaluate_script("[window.outerWidth, window.outerHeight];") width, height = @session.evaluate_script("[window.outerWidth, window.outerHeight];")
@session.current_window.resize_to(width-10, height-10) @session.current_window.resize_to(width-10, height-10)
expect(@session.evaluate_script("[window.outerWidth, window.outerHeight];")).to eq([width-10, height-10]) expect(@session.evaluate_script("[window.outerWidth, window.outerHeight];")).to eq([width-10, height-10])
@ -111,4 +111,25 @@ Capybara::SpecHelper.spec Capybara::Window, requires: [:windows] do
end.to raise_error(Capybara::WindowError, "Resizing not current window is not possible.") end.to raise_error(Capybara::WindowError, "Resizing not current window is not possible.")
end end
end end
describe '#maximize' do
it 'should be able to maximize window', requires: [:js] do
screen_width, screen_height = @session.evaluate_script("[window.screen.availWidth, window.screen.availHeight];")
window = @session.current_window
window.resize_to(screen_width-100, screen_height-100)
expect(@session.evaluate_script("[window.outerWidth, window.outerHeight];")).to eq([screen_width-100, screen_height-100])
window.maximize
sleep 0.6 # Selenium returns when window isn't yet fully resized
expect(@session.evaluate_script("[window.outerWidth, window.outerHeight];")).to eq([screen_width, screen_height])
end
it 'should raise error if invoked not for current window' do
@other_window = @session.window_opened_by do
@session.find(:css, '#openWindow').click
end
expect do
@other_window.maximize
end.to raise_error(Capybara::WindowError, "Maximizing not current window is not possible.")
end
end
end end

View file

@ -21,7 +21,7 @@ Capybara::SpecHelper.spec '#windows', requires: [:windows] do
@session.within_window(window) { @session.title } @session.within_window(window) { @session.title }
end end
expect(titles).to match_array([ expect(titles).to match_array([
'With Windows', 'Title of the first popup', 'Title of popup two' 'With Windows', 'Title of the first popup', 'Title of popup two'
]) ])
end end
end end

View file

@ -45,7 +45,8 @@ module Capybara
## ##
# Close window. Available only for current window. # Close window. Available only for current window.
# After calling this method future invocations of other Capybara methods should raise `session.driver.no_such_window_error` until another window will be switched to. # After calling this method future invocations of other Capybara methods should raise
# `session.driver.no_such_window_error` until another window will be switched to.
# @raise [Capybara::WindowError] if invoked not for current window # @raise [Capybara::WindowError] if invoked not for current window
# #
def close def close
@ -74,6 +75,17 @@ module Capybara
@driver.resize_current_window_to(width, height) @driver.resize_current_window_to(width, height)
end end
##
# Maximize window. Available only for current window.
# If a particular driver (e.g. headless driver) doesn't have concept of maximizing it
# may not support this method.
# @raise [Capybara::WindowError] if invoked not for current window
#
def maximize
raise_unless_current('Maximizing')
@driver.maximize_current_window
end
def eql?(other) def eql?(other)
other.kind_of?(self.class) && @session == other.session && @handle == other.handle other.kind_of?(self.class) && @session == other.session && @handle == other.handle
end end