From 397be95d09bf27cb87e8900e8a30394c283c031e Mon Sep 17 00:00:00 2001 From: Andrey Botalov Date: Thu, 10 Apr 2014 10:20:27 +0300 Subject: [PATCH] Add maximize window --- .travis.yml | 4 ++- lib/capybara/driver/base.rb | 4 +++ lib/capybara/selenium/driver.rb | 4 +++ lib/capybara/session.rb | 2 +- .../spec/session/window/become_closed_spec.rb | 4 +-- .../session/window/open_new_window_spec.rb | 2 +- .../session/window/window_opened_by_spec.rb | 2 +- .../spec/session/window/window_spec.rb | 25 +++++++++++++++++-- .../spec/session/window/windows_spec.rb | 2 +- lib/capybara/window.rb | 14 ++++++++++- 10 files changed, 53 insertions(+), 10 deletions(-) diff --git a/.travis.yml b/.travis.yml index b1041aa4..8ba3fb46 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,7 +25,9 @@ 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 before_script: - - sh -e /etc/init.d/xvfb start - export DISPLAY=:99.0 + - sh -e /etc/init.d/xvfb start + - fluxbox > /dev/null 2>&1 & script: "bundle exec rake travis" diff --git a/lib/capybara/driver/base.rb b/lib/capybara/driver/base.rb index e121f08e..8d616b52 100644 --- a/lib/capybara/driver/base.rb +++ b/lib/capybara/driver/base.rb @@ -63,6 +63,10 @@ class Capybara::Driver::Base raise Capybara::NotSupportedByDriverError, 'Capybara::Driver::Base#resize_window_to' end + def maximize_current_window + raise Capybara::NotSupportedByDriverError, 'Capybara::Driver::Base#maximize_current_window' + end + def close_current_window raise Capybara::NotSupportedByDriverError, 'Capybara::Driver::Base#close_window' end diff --git a/lib/capybara/selenium/driver.rb b/lib/capybara/selenium/driver.rb index 5a81dc29..f72cd9f2 100644 --- a/lib/capybara/selenium/driver.rb +++ b/lib/capybara/selenium/driver.rb @@ -139,6 +139,10 @@ class Capybara::Selenium::Driver < Capybara::Driver::Base browser.manage.window.resize_to(width, height) end + def maximize_current_window + browser.manage.window.maximize + end + def close_current_window browser.close end diff --git a/lib/capybara/session.rb b/lib/capybara/session.rb index df7a724d..34c7c985 100644 --- a/lib/capybara/session.rb +++ b/lib/capybara/session.rb @@ -417,7 +417,7 @@ module Capybara # 3. Switches back (this step will be invoked even if exception will happen at second step) # # @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 # @raise [driver#no_such_window_error] if unexistent (e.g. closed) window was passed # @overload within_window(proc_or_lambda) { do_something } diff --git a/lib/capybara/spec/session/window/become_closed_spec.rb b/lib/capybara/spec/session/window/become_closed_spec.rb index 2c974bde..eb5e36d4 100644 --- a/lib/capybara/spec/session/window/become_closed_spec.rb +++ b/lib/capybara/spec/session/window/become_closed_spec.rb @@ -1,4 +1,4 @@ -Capybara::SpecHelper.spec '#become_closed', requires: [:windows] do +Capybara::SpecHelper.spec '#become_closed', requires: [:windows, :js] do before(:each) do @window = @session.current_window @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 @session.within_window @other_window do - @session.execute_script('setTimeout(function(){ window.close(); }, 700);') + @session.execute_script('setTimeout(function(){ window.close(); }, 800);') end Capybara.using_wait_time 0.4 do expect do diff --git a/lib/capybara/spec/session/window/open_new_window_spec.rb b/lib/capybara/spec/session/window/open_new_window_spec.rb index c46755a4..581739ba 100644 --- a/lib/capybara/spec/session/window/open_new_window_spec.rb +++ b/lib/capybara/spec/session/window/open_new_window_spec.rb @@ -18,7 +18,7 @@ Capybara::SpecHelper.spec '#open_new_window', requires: [:windows] do expect(@session.current_url).to eq('about:blank') end - it 'should open window with changable content' do + it 'should open window with changeable content' do window = @session.open_new_window @session.within_window window do @session.visit '/with_html' diff --git a/lib/capybara/spec/session/window/window_opened_by_spec.rb b/lib/capybara/spec/session/window/window_opened_by_spec.rb index 693d1f9d..9d0158d9 100644 --- a/lib/capybara/spec/session/window/window_opened_by_spec.rb +++ b/lib/capybara/spec/session/window/window_opened_by_spec.rb @@ -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 do + Capybara.using_wait_time 1.2 do window = @session.window_opened_by do @session.find(:css, '#openWindowWithTimeout').click end diff --git a/lib/capybara/spec/session/window/window_spec.rb b/lib/capybara/spec/session/window/window_spec.rb index 2e854d80..afe71ec6 100644 --- a/lib/capybara/spec/session/window/window_spec.rb +++ b/lib/capybara/spec/session/window/window_spec.rb @@ -81,7 +81,7 @@ Capybara::SpecHelper.spec Capybara::Window, requires: [:windows] do end 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];") end @@ -96,7 +96,7 @@ Capybara::SpecHelper.spec Capybara::Window, requires: [:windows] do end 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];") @session.current_window.resize_to(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 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 diff --git a/lib/capybara/spec/session/window/windows_spec.rb b/lib/capybara/spec/session/window/windows_spec.rb index ff7728e1..932898a2 100644 --- a/lib/capybara/spec/session/window/windows_spec.rb +++ b/lib/capybara/spec/session/window/windows_spec.rb @@ -21,7 +21,7 @@ Capybara::SpecHelper.spec '#windows', requires: [:windows] do @session.within_window(window) { @session.title } end 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 diff --git a/lib/capybara/window.rb b/lib/capybara/window.rb index 2dd36bbc..1cfaae6d 100644 --- a/lib/capybara/window.rb +++ b/lib/capybara/window.rb @@ -45,7 +45,8 @@ module Capybara ## # 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 # def close @@ -74,6 +75,17 @@ module Capybara @driver.resize_current_window_to(width, height) 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) other.kind_of?(self.class) && @session == other.session && @handle == other.handle end