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

Resize/get size/close should work for non-current window

This commit is contained in:
Andrey Botalov 2014-04-25 01:38:42 +03:00
parent 397be95d09
commit 5096e90d6a
7 changed files with 107 additions and 61 deletions

View file

@ -25,9 +25,11 @@ before_install:
- sudo apt-get update
- sudo apt-get install google-chrome-stable --force-yes
- sudo chmod 1777 /dev/shm
- sudo apt-get install fluxbox -y --force-yes
- sudo apt-get install awesome -y; mkdir -p ~/.config/awesome
- echo "require('awful'); tags = {}; tags[1] = awful.tag({'name'})" > ~/.config/awesome/rc.lua
before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
- fluxbox > /dev/null 2>&1 &
- sleep 1
- "awesome &"
script: "bundle exec rake travis"

View file

@ -55,19 +55,19 @@ class Capybara::Driver::Base
raise Capybara::NotSupportedByDriverError, 'Capybara::Driver::Base#current_window_handle'
end
def current_window_size
def window_size(handle)
raise Capybara::NotSupportedByDriverError, 'Capybara::Driver::Base#window_size'
end
def resize_current_window_to(width, height)
def resize_window_to(handle, width, height)
raise Capybara::NotSupportedByDriverError, 'Capybara::Driver::Base#resize_window_to'
end
def maximize_current_window
def maximize_window(handle)
raise Capybara::NotSupportedByDriverError, 'Capybara::Driver::Base#maximize_current_window'
end
def close_current_window
def close_window(handle)
raise Capybara::NotSupportedByDriverError, 'Capybara::Driver::Base#close_window'
end

View file

@ -130,21 +130,29 @@ class Capybara::Selenium::Driver < Capybara::Driver::Base
browser.window_handle
end
def current_window_size
size = browser.manage.window.size
[size.width, size.height]
def window_size(handle)
within_given_window(handle) do
size = browser.manage.window.size
[size.width, size.height]
end
end
def resize_current_window_to(width, height)
browser.manage.window.resize_to(width, height)
def resize_window_to(handle, width, height)
within_given_window(handle) do
browser.manage.window.resize_to(width, height)
end
end
def maximize_current_window
browser.manage.window.maximize
def maximize_window(handle)
within_given_window(handle) do
browser.manage.window.maximize
end
end
def close_current_window
browser.close
def close_window(handle)
within_given_window(handle) do
browser.close
end
end
def window_handles
@ -157,6 +165,7 @@ class Capybara::Selenium::Driver < Capybara::Driver::Base
def switch_to_window(handle)
browser.switch_to.window handle
sleep 0.05 # https://code.google.com/p/chromedriver/issues/detail?id=769
end
# @api private
@ -166,11 +175,11 @@ class Capybara::Selenium::Driver < Capybara::Driver::Base
original_handle = browser.window_handle
handles.each do |handle|
browser.switch_to.window handle
switch_to_window(handle)
if (locator == browser.execute_script("return window.name") ||
browser.title.include?(locator) ||
browser.current_url.include?(locator))
browser.switch_to.window original_handle
switch_to_window(original_handle)
return handle
end
end
@ -197,4 +206,18 @@ class Capybara::Selenium::Driver < Capybara::Driver::Base
def no_such_window_error
Selenium::WebDriver::Error::NoSuchWindowError
end
private
def within_given_window(handle)
original_handle = self.current_window_handle
if handle == original_handle
yield
else
switch_to_window(handle)
result = yield
switch_to_window(original_handle)
result
end
end
end

View file

@ -16,9 +16,10 @@ Capybara::SpecHelper.spec '#current_window', requires: [:windows] do
end
it "should be modified by switching to another window" do
window = @session.window_opened_by { @session.find(:css, '#openWindow').click }
expect do
window = @session.window_opened_by { @session.find(:css, '#openWindow').click }
@session.switch_to_window(window)
end.to change { @session.current_window }
end.to change { @session.current_window }.from(@window).to(window)
end
end

View file

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

View file

@ -65,18 +65,22 @@ Capybara::SpecHelper.spec Capybara::Window, requires: [:windows] do
end
end
it 'should change number of windows' do
expect do
@session.within_window(@other_window) do
@other_window.close
end
end.to change { @session.windows.size }.from(2).to(1)
it 'should switch to original window if invoked not for current window' do
expect(@session.windows.size).to eq(2)
expect(@session.current_window).to eq(@window)
@other_window.close
expect(@session.windows.size).to eq(1)
expect(@session.current_window).to eq(@window)
end
it 'should raise error if invoked not for current window' do
it 'should make subsequent invocations of other methods raise no_such_window_error if invoked for current window' do
@session.switch_to_window(@other_window)
expect(@session.current_window).to eq(@other_window)
@other_window.close
expect do
@other_window.close
end.to raise_error(Capybara::WindowError, "Closing not current window is not possible.")
@session.find(:css, '#some_id')
end.to raise_error(@session.driver.no_such_window_error)
@session.switch_to_window(@window)
end
end
@ -85,13 +89,16 @@ Capybara::SpecHelper.spec Capybara::Window, requires: [:windows] do
expect(@session.current_window.size).to eq @session.evaluate_script("[window.outerWidth, window.outerHeight];")
end
it 'should raise error if invoked not for current window' do
it 'should switch to original window if invoked not for current window' do
@other_window = @session.window_opened_by do
@session.find(:css, '#openWindow').click
end
expect do
@other_window.size
end.to raise_error(Capybara::WindowError, "Getting size of not current window is not possible.")
size =
@session.within_window @other_window do
@session.evaluate_script("[window.outerWidth, window.outerHeight];")
end
expect(@other_window.size).to eq(size)
expect(@session.current_window).to eq(@window)
end
end
@ -102,13 +109,13 @@ Capybara::SpecHelper.spec Capybara::Window, requires: [:windows] do
expect(@session.evaluate_script("[window.outerWidth, window.outerHeight];")).to eq([width-10, height-10])
end
it 'should raise error if invoked not for current window' do
it 'should switch to original window if invoked not for current window' do
@other_window = @session.window_opened_by do
@session.find(:css, '#openWindow').click
end
expect do
@other_window.resize_to(1000, 700)
end.to raise_error(Capybara::WindowError, "Resizing not current window is not possible.")
@other_window.resize_to(400, 300)
expect(@session.current_window).to eq(@window)
expect(@other_window.size).to eq([400, 300])
end
end
@ -119,17 +126,17 @@ Capybara::SpecHelper.spec Capybara::Window, requires: [:windows] do
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
sleep 0.1
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
it 'should switch to original window 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.")
@other_window.maximize
expect(@session.current_window).to eq(@window)
expect(@other_window.size).to eq(@session.evaluate_script("[window.screen.availWidth, window.screen.availHeight];"))
end
end
end

View file

@ -9,8 +9,16 @@ module Capybara
# * {Capybara::Session#window_opened_by}
# * {Capybara::Session#switch_to_window}
#
# Note that some drivers (e.g. Selenium) support getting size of/resizing/closing only
# current window. So if you invoke such method for:
#
# * window that is current, Capybara will make 2 Selenium method invocations
# (get handle of current window + get size/resize/close).
# * window that is not current, Capybara will make 4 Selenium method invocations
# (get handle of current window + switch to given handle + get size/resize/close + switch to original handle)
#
class Window
# @return [String] a string that uniquely identifies window
# @return [String] a string that uniquely identifies window within session
attr_reader :handle
# @return [Capybara::Session] session that this window belongs to
@ -44,46 +52,51 @@ module Capybara
end
##
# Close window. Available only for current window.
# After calling this method future invocations of other Capybara methods should raise
# Close window.
#
# If this method was called for window that is current, then 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
#
# @!macro about_current
# If this method was called for window that is not current, then after calling this method
# current window shouldn remain the same as it was before calling this method.
#
def close
raise_unless_current('Closing')
@driver.close_current_window
@driver.close_window(handle)
end
##
# Get window size. Available only for current window.
# Get window size.
#
# @macro about_current
# @return [Array<(Fixnum, Fixnum)>] an array with width and height
# @raise [Capybara::WindowError] if invoked not for current window
#
def size
raise_unless_current('Getting size of')
@driver.current_window_size
@driver.window_size(handle)
end
##
# Resize window. Available only for current window.
# Resize window.
#
# @macro about_current
# @param width [String] the new window width in pixels
# @param height [String] the new window height in pixels
# @raise [Capybara::WindowError] if invoked not for current window
#
def resize_to(width, height)
raise_unless_current('Resizing')
@driver.resize_current_window_to(width, height)
@driver.resize_window_to(handle, width, height)
end
##
# Maximize window. Available only for current window.
# Maximize 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
#
# @macro about_current
#
def maximize
raise_unless_current('Maximizing')
@driver.maximize_current_window
@driver.maximize_window(handle)
end
def eql?(other)