Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cucumber + Capybara tests to ensure a new window is opened

I have the following lines in my feature file:

Given I have website "www.google.co.uk" 
When I click the website "www.google.co.uk" 
Then "www.google.co.uk" page is opened in a new window

I am struggling to find a way to test that the webpage is definately opened in a new window.

Currently I have been using this in my step def:

Then /url "([^"]*)" is opened in new window/ do |url|
  browser = page.driver.browser
  current_id = browser.window_handle
  tab_id = page.driver.find_window(url)
  browser.switch_to.window tab_id
  page.driver.browser.close
  browser.switch_to.window current_id
end

but this still passes the test if the webpage is loaded on the same page, I want it to fail if the webpage is loaded on the same window/tab.

Any suggestions?

Many thanks

like image 774
Dono Avatar asked Jan 10 '12 16:01

Dono


1 Answers

I see no assertions in your test.

My approach would be to test size of window_handles array after performing clicking action on the link, since before clicking the size should equal 1 and after the clicking window_handles should equal 2.

assert page.driver.browser.window_handles.size == 2

Imho, good enough, since if the webpage is loaded in the same tab, the size will be 1 and the test will fail.

like image 72
socjopata Avatar answered Oct 02 '22 06:10

socjopata