diff --git a/.travis.yml b/.travis.yml index 08e98dc1..f0e91138 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,11 +25,12 @@ before_install: - sudo apt-get update - sudo apt-get install google-chrome-stable --force-yes - sudo chmod 1777 /dev/shm - - sudo apt-get install awesome -y; mkdir -p ~/.config/awesome - - echo "require('awful'); tags = {}; tags[1] = awful.tag({'name'})" > ~/.config/awesome/rc.lua + - sudo apt-get install awesome -y before_script: - export DISPLAY=:99.0 - sh -e /etc/init.d/xvfb start - sleep 1 + - mkdir -p ~/.config/awesome + - echo "require('awful'); tags = {}; tags[1] = awful.tag({'name'})" > ~/.config/awesome/rc.lua - "awesome &" script: "bundle exec rake travis" diff --git a/lib/capybara/selenium/driver.rb b/lib/capybara/selenium/driver.rb index 076bf071..a5f43204 100644 --- a/lib/capybara/selenium/driver.rb +++ b/lib/capybara/selenium/driver.rb @@ -147,6 +147,7 @@ class Capybara::Selenium::Driver < Capybara::Driver::Base within_given_window(handle) do browser.manage.window.maximize end + sleep 0.1 # work around for https://code.google.com/p/selenium/issues/detail?id=7405 end def close_window(handle) @@ -165,7 +166,6 @@ 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 diff --git a/lib/capybara/session.rb b/lib/capybara/session.rb index 34c7c985..fe7433be 100644 --- a/lib/capybara/session.rb +++ b/lib/capybara/session.rb @@ -437,12 +437,12 @@ module Capybara def within_window(window_or_handle) if window_or_handle.instance_of?(Capybara::Window) original = current_window + switch_to_window(window_or_handle) unless original == window_or_handle + scopes << nil begin - switch_to_window(window_or_handle) unless original == window_or_handle - scopes << nil yield ensure - @scopes = [nil] + @scopes.pop switch_to_window(original) unless original == window_or_handle end elsif window_or_handle.is_a?(Proc) @@ -452,7 +452,7 @@ module Capybara begin yield ensure - @scopes = [nil] + @scopes.pop switch_to_window(original) end else @@ -464,7 +464,7 @@ module Capybara scopes << nil driver.within_window(window_or_handle) { yield } ensure - @scopes = [nil] + @scopes.pop end end end diff --git a/lib/capybara/spec/session/window/become_closed_spec.rb b/lib/capybara/spec/session/window/become_closed_spec.rb index eb5e36d4..ca054db5 100644 --- a/lib/capybara/spec/session/window/become_closed_spec.rb +++ b/lib/capybara/spec/session/window/become_closed_spec.rb @@ -6,10 +6,10 @@ Capybara::SpecHelper.spec '#become_closed', requires: [:windows, :js] do @session.find(:css, '#openWindow').click end end + after(:each) do - (@session.windows - [@window]).each do |w| - @session.switch_to_window w - w.close + @session.document.synchronize(3, errors: [Capybara::CapybaraError]) do + raise Capybara::CapybaraError if @session.windows.size != 1 end @session.switch_to_window(@window) end @@ -48,7 +48,7 @@ Capybara::SpecHelper.spec '#become_closed', requires: [:windows, :js] 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(); }, 800);') + @session.execute_script('setTimeout(function(){ window.close(); }, 900);') end Capybara.using_wait_time 0.4 do expect do 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 6e9a8eac..3877068a 100644 --- a/lib/capybara/spec/session/window/window_opened_by_spec.rb +++ b/lib/capybara/spec/session/window/window_opened_by_spec.rb @@ -23,6 +23,9 @@ Capybara::SpecHelper.spec '#window_opened_by', requires: [:windows] do end end.to raise_error(Capybara::WindowError, zero_windows_message) end + @session.document.synchronize(2, errors: [Capybara::CapybaraError]) do + raise Capybara::CapybaraError if @session.windows.size != 2 + end end it 'should find window if value of :wait is more than timeout' do @@ -44,6 +47,9 @@ Capybara::SpecHelper.spec '#window_opened_by', requires: [:windows] do end end.to raise_error(Capybara::WindowError, zero_windows_message) end + @session.document.synchronize(2, errors: [Capybara::CapybaraError]) do + raise Capybara::CapybaraError if @session.windows.size != 2 + end end it 'should find window if default_wait_time is more than timeout' do @@ -62,6 +68,9 @@ Capybara::SpecHelper.spec '#window_opened_by', requires: [:windows] do @session.find(:css, '#openTwoWindows').click end end.to raise_error(Capybara::WindowError, two_windows_message) + @session.document.synchronize(2, errors: [Capybara::CapybaraError]) do + raise Capybara::CapybaraError if @session.windows.size != 3 + end end it 'should raise error when no windows were opened by block' do diff --git a/lib/capybara/spec/session/window/window_spec.rb b/lib/capybara/spec/session/window/window_spec.rb index 7e489b1e..3881e343 100644 --- a/lib/capybara/spec/session/window/window_spec.rb +++ b/lib/capybara/spec/session/window/window_spec.rb @@ -126,7 +126,6 @@ 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.1 expect(@session.evaluate_script("[window.outerWidth, window.outerHeight];")).to eq([screen_width, screen_height]) end diff --git a/lib/capybara/spec/session/window/windows_spec.rb b/lib/capybara/spec/session/window/windows_spec.rb index 932898a2..102e6a8f 100644 --- a/lib/capybara/spec/session/window/windows_spec.rb +++ b/lib/capybara/spec/session/window/windows_spec.rb @@ -3,6 +3,10 @@ Capybara::SpecHelper.spec '#windows', requires: [:windows] do @window = @session.current_window @session.visit('/with_windows') @session.find(:css, '#openTwoWindows').click + + @session.document.synchronize(3, errors: [Capybara::CapybaraError]) do + raise Capybara::CapybaraError if @session.windows.size != 3 + end end after(:each) do (@session.windows - [@window]).each do |w| diff --git a/lib/capybara/spec/session/window/within_window_spec.rb b/lib/capybara/spec/session/window/within_window_spec.rb index 3b8cac9a..f2f0eace 100644 --- a/lib/capybara/spec/session/window/within_window_spec.rb +++ b/lib/capybara/spec/session/window/within_window_spec.rb @@ -3,6 +3,10 @@ Capybara::SpecHelper.spec '#within_window', requires: [:windows] do @window = @session.current_window @session.visit('/with_windows') @session.find(:css, '#openTwoWindows').click + + @session.document.synchronize(3, errors: [Capybara::CapybaraError]) do + raise Capybara::CapybaraError if @session.windows.size != 3 + end end after(:each) do (@session.windows - [@window]).each do |w| @@ -52,6 +56,18 @@ Capybara::SpecHelper.spec '#within_window', requires: [:windows] do expect(@session.send(:scopes)).to eq([nil]) end + it "should leave correct scopes after execution in case of error" do + window = (@session.windows - [@window]).first + expect do + @session.within 'html' do + @session.within_window(window) {} + end + end.to raise_error(Capybara::ScopeError) + expect(@session.current_window).to eq(@window) + expect(@session).to have_css('#doesNotOpenWindows') + expect(@session.send(:scopes)).to eq([nil]) + end + it 'should raise error if closed window was passed' do other_window = (@session.windows - [@window]).first @session.within_window other_window do @@ -118,7 +134,7 @@ Capybara::SpecHelper.spec '#within_window', requires: [:windows] do it "should warn" do expect(@session).to receive(:warn).with("DEPRECATION WARNING: Passing string argument "\ "to #within_window is deprecated. Pass window object or lambda. "\ - "(called from #{__FILE__}:122)").and_call_original + "(called from #{__FILE__}:138)").and_call_original @session.within_window('firstPopup') {} end