完成测试后,Watir-Webdriver不会在Firefox中显示对话框

时间:2015-06-26 05:40:14

标签: ruby firefox webdriver watir watir-webdriver

我正在使用带有FF38的webdriver,但在我的脚本完成后离开浏览器打开窗口。如果我在测试后继续,我发现该窗口中的对话框不再打开。

该脚本旨在自动化表单输入而不是手动执行,但网站确实使用对话框来表达选择 - (例如,删除脚本刚刚输入的数据,以便我可以重新运行脚本没有覆盖信息)

有没有办法在我完成后断开webdriver对话框处理?

我感觉有点愚蠢,但我的搜索没有结果,所以我可能在我的搜索中使用了错误的单词,因为我对ruby和webdriver的新见。

示例如下:

require "watir-webdriver"

l_Browser = Watir::Browser.new :firefox
l_Browser.goto "http://www.w3schools.com/js/tryit.asp?filename=tryjs_alert"

# Click the button that opens the dialog
l_Browser.div(:class => "container").div(:class => "iframecontainer"). \
    div(:class => "iframewrapper").iframe(:id => "iframeResult"). \
    button(:onclick => "myFunction()").click

结果是会出现一个弹出窗口,但是一旦脚本完成,就不会再出现弹出窗口进一步尝试点击按钮。

这包括即使在脚本期间没有触发弹出窗口(即:,最后一行被注释掉)......一旦脚本运行完毕,在watir webdriver打开的窗口中不会出现弹出窗口。 (如果我在脚本运行时单击按钮,它们将打开,但不会在之后)

根据以下答案,我正在使用:

begin
    b = Watir::Browser.new :firefox
    File.open('d:\\MARK.TXT', 'w') {|f| f.write(YAML.dump(b)) }
    # Load MessageBox and wait here
    b = YAML.load(File.read('d:\\MARK.TXT'))
ensure
    if !b.nil?
        b.close()
    end
end

...但它目前允许可以忽略的错误......我只是不知道从长远来看忽视它们是多么明智:

    D:/Ruby193/lib/ruby/gems/1.9.1/gems/childprocess-0.5.6/lib/childprocess/windows/handle.rb:50:in `exit_code': The handle is invalid. (6) (ChildProcess::Error)
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/childprocess-0.5.6/lib/childprocess/windows/process.rb:41:in `exited?'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/childprocess-0.5.6/lib/childprocess/abstract_process.rb:147:in `poll_for_exit'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.46.2/lib/selenium/webdriver/firefox/binary.rb:59:in `quit'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.46.2/lib/selenium/webdriver/firefox/launcher.rb:62:in `quit'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.46.2/lib/selenium/webdriver/firefox/bridge.rb:75:in `quit'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.46.2/lib/selenium/webdriver/common/driver.rb:165:in `quit'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.7.0/lib/watir-webdriver/browser.rb:136:in `close'
        from D:/Users/risendevil/Documents/Aptana Studio 3 Workspace/Ruby Test/Default.rb:19:in `<main>'

版本: Firefox 38.0.5 硒(0.2.11) selenium-webdriver(2.46.2,2.45.0) watir-webdriver(0.7.0)

2 个答案:

答案 0 :(得分:0)

我在回答你的问题时学到了新的东西:Turning an object into text is called serialization. Turning text into an object is called deserialization.

And here's a gist of you want to do, specifically.

重要的部分是

Arm myArm = new Arm(5);
Person me = new Person(myArm);

System.out.println(me.getArm().getFingers())

您甚至可以直接在浏览器中执行此操作:

my_object = SomeObject.new
my_object.some_method # => returns your expected result
File.open('path/to/some.file', 'w') {|f| f.write(YAML.dump(my_object)) }
# Do whatever you want
my_object_reloaded = YAML.load(File.read('path/to/some.file'))
my_object_reloaded.some_method # => returns your expected result

答案 1 :(得分:0)

require "watir-webdriver"

l_Browser = Watir::Browser.new :firefox
l_Browser.goto "http://www.w3schools.com/js/tryit.asp?filename=tryjs_alert"

l_Browser.iframe(:id => 'iframeResult').button(:xpath => "//button[text()='Try it']").when_present.click # click on "Try it" button
l_Browser.alert.close # closing popup