Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

watir webdriver: how to switch to another window open with target

I'm using watir-webdriver, and when clicking on a link with target="_blank" it opens a new window, which I have no control of, but still need to verify that something other than 404 page opened, and if title of that new window contains some keywords. I don't know upfront what title of that window will be (it's not the same all the time), so this solution doesn't help.

Are there any known ways how to handle those target="_blank" windows through watir-webdriver?

like image 918
earlyadopter Avatar asked Mar 05 '13 20:03

earlyadopter


2 Answers

Since you know that the title of the new window should have certain forms, you should be able to locate it via its title using a regex (regular expression denoted with the "/").

browser.window(:title => /known part of title/).use do
  #Whatever you want to do with the popup
end

Alternatively, if you really do not know anything about the popup, you can get the last window created:

browser.windows.last.use do
  #Whatever you want to do with the popup
end
like image 75
Justin Ko Avatar answered Oct 11 '22 13:10

Justin Ko


There's a link to the watir-webdriver window switching spec on http://watirwebdriver.com/browser-popups/. You should be able to find the window by :title, :url, or :index. Examples from the spec:

browser.window(:index => 1).use
browser.window(:url => /closeable\.html/).use
browser.window(:title => "closeable window").use
like image 39
orde Avatar answered Oct 11 '22 13:10

orde