Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing if a new window opens with Watir-Webdriver

I'm using Watir-webdriver and I was wondering if there was a good way to check if a new window opens. I've googled around a bit and couldn't find anything though it feels like there should be an easy answer.

I have a printer friendly link and I want to test that the link opens in a new window or tab and I would like to test this with ie, firefox, chrome and safari if possible.

Thanks!

like image 986
Mike Avatar asked Jul 11 '11 15:07

Mike


2 Answers

You can check the number of windows:

browser.windows.size

or check if a specific window exists:

browser.window(:title => "foo").exists?

More examples in the specs.

like image 157
jarib Avatar answered Oct 01 '22 02:10

jarib


You can also use index based browser window checking where you need to worry about index only and it follows zero based index ordering. So, the default window is of index: 0 and if a new window opens it will be of index: 1, the next will be of index: 2 and so on.

To check first child window if you want to test that the link opens in a new window ,

browser.window(index: 1).exists?

Or to work inside this window,

browser.window(index: 1).use do
  # do scripting here
end
like image 34
Nawshad Rehan Rasha Avatar answered Oct 01 '22 02:10

Nawshad Rehan Rasha