问题
Another watir issue. This code works until add_task
...
After login, the auth window closes and goes back to main window with login key. But I get this error:
hi there
testing add_task
[remote server] file:///var/folders/3w/b7rcpqfj7kl3wtv56jt99yx00000gn/T/webdriver-profile20120919-9069-1ua1lm9/extensions/fxdriver@googlecode.com/components/command_processor.js:10212:in `unknown': Window not found. The browser window may have been closed. (Selenium::WebDriver::Error::NoSuchWindowError)
require 'rubygems'
require 'test/unit'
require 'watir-webdriver'
class Login
def basic_auth(logintype,usr,pwd)
$browser.goto("http://change.com")
$browser.link(:class,'joinchange').wait_until_present
join_link = $browser.link(:class,'joinchange')
join_link.click
$browser.window(:title, 'Sign In').wait_until_present
$browser.window(:title, 'Sign In').use
if logintype == 'fb'
$browser.link(:name, 'login-facebook id=').click
$browser.text_field(:id,'email').set(usr)
$browser.text_field(:id,'pass').set(pwd)
puts 'form submit'
sleep 3
$browser.form(:id,'login_form').submit
puts 'hello world'
else
$browser.text_field(:id,'lgnId').set('changes')
$browser.text_field(:id,'pwdId').set('letmeinnow')
sleep 3
$browser.form(:name,'LoginActionForm').submit
puts 'hi there'
end
end
def add_task
puts "testing add_task"
$browser.link(:class,'nextStep').click
end
end
$browser = Watir::Browser.new
login = Login.new
login.basic_auth('test','usr','pwd')
sleep 3
login.add_task
回答1:
I believe that the problem is that after switching to the Sign In popup window, the script does not switch out of it. So during add_task when it tries to click the link, it is trying in the Sign In popup, which is closed.
In the basic_auth
method, you want to pass the actions you want performed in the popup as a block to the window.use
method.
So you want:
$browser.window(:title, 'Sign In').use do
if logintype == 'fb'
$browser.link(:name, 'login-facebook id=').click
$browser.text_field(:id,'email').set(usr)
$browser.text_field(:id,'pass').set(pwd)
puts 'form submit'
sleep 3
$browser.form(:id,'login_form').submit
puts 'hello world'
else
$browser.text_field(:id,'lgnId').set('changes')
$browser.text_field(:id,'pwdId').set('letmeinnow')
sleep 3
$browser.form(:name,'LoginActionForm').submit
puts 'hi there'
end
end
(Notice the do
added at the end of the first line and the extra end
at the end.)
来源:https://stackoverflow.com/questions/12503085/watir-webdriver-window-not-found