简体   繁体   中英

How to set WATIR focus on new window

I'm new to WATIR testing (and do I love it!) and have run into a wall on how to refocus my WATIR script to a newly opened window.. Here's my (simplified) script....

#!/usr/bin/ruby
require 'rubygems'
require 'watir-webdriver'
browser=Watir::Browser.new
browser.goto("http://0:3050")

browser.text_field(:name,"userkey300203830").set("OKJHNB")
browser.button(:id, "interview48").click

puts "Expected Result:"
puts "A successful display of cars"

if browser.window(:title=>"300203830").exists?
   puts " Test passed. New window opened!"
else
   puts " Test Failed! No window found"
end

It all works right up to the end. After the key "interview48" is clicked, a new window is opened with the title "300203830". Looks like I find it but I just don't know how to now focus on that window.

browser.window(:title => "300203830").use do
  # do something
end

More information: http://watir.github.io/docs/browser-popups/

Additionally for more than 2 windows you can use the following:

browser.windows[n].use  

#n is variable for which window. n will access them in order of opened or tabs from left to right

browser.windows.last.use 
browser.windows.first.use

You can use the above commands if you open a new window from first browser instance and would like to toggle between the two.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM