How to set WATIR focus on new window

Jane WIlkie picture Jane WIlkie · May 22, 2012 · Viewed 11.6k times · Source

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.

Answer

Željko Filipin picture Željko Filipin · May 22, 2012
browser.window(:title => "300203830").use do
  # do something
end

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