samayres1992
samayres1992

Reputation: 779

Controlling new tab to operate using Watir? (Ruby)

I'm using watir for automated testing and after running through some tables, a chart then gets opened up in a new tab. But watir doesn't seem to recognise the new current tab and continues to search through the original browser tab.

Is there any way of telling watir which tab you want to be using?

Upvotes: 5

Views: 6861

Answers (4)

Duarte
Duarte

Reputation: 144

@browser.windows[x].use

Replace 'x' with the tab number you'd like to use. @browser.windows is a Watir Collection, which can be treated like an Array for this purpose.

Upvotes: 0

Ivo Dimitrov
Ivo Dimitrov

Reputation: 123

There are 3 primary selectors for windows:

  • :title - typically the easiest

  • :url - often used with a Regexp value

  • :element - a unique element might be the least brittle (new as of Watir 6.18!)

    browser.window(title: 'new window') browser.window(url: /my_page.html/) browser.window(element: browser.div(id: 'my-element'))

Locating by index is no longer supported

More information: Watir Browser Windows

Upvotes: 0

Gaurav Sharma
Gaurav Sharma

Reputation: 349

You can use this code

browser.windows.last.use

this will set watir focus to newly or last opened tab or window, and after completion of use of this new tab/window just tell watir to close that tab/window

browser.windows.last.close

Upvotes: 8

Željko Filipin
Željko Filipin

Reputation: 57262

Watir does not care if a new page opens in a new window or in a new tab, so use window switching API to switch to the new tab:

browser.window(:title => "annoying popup").use do
  browser.button(:id => "close").click
end

More information: http://watirwebdriver.com/browser-popups/

Upvotes: 8

Related Questions