webdriverwatirwatir-webdriver

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?


Solution

  • 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