watir webdriver: how to switch to another window open with target
Asked Answered
P

2

5

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?

Physics answered 5/3, 2013 at 20:12 Comment(0)
C
16

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
Cenesthesia answered 5/3, 2013 at 21:1 Comment(1)
@paul, sorry the comment is hard to read without formatting. You should update your newly created question with your specific example (including what errors you are getting). The Watir code is correct. The problem might be that you are also mixing with the Page-Object gem.Cenesthesia
A
7

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
Abhor answered 5/3, 2013 at 20:58 Comment(2)
first line error - syntax error, unexpected ':', expecting ')' browser.window(index: => 1).useAtonic
index: => 1 isn't valid. Try :index => 1 (or index: 1).Abhor

© 2022 - 2024 — McMap. All rights reserved.