我正在使用Selenium WebDriver和rautomation编写测试来处理系统弹出窗口。我在irb上尝试过,如下所示:
require 'selenium-webdriver'
require 'rautomation'
driver = Selenium::WebDriver.for :firefox
driver.get "http://rubygems.org/gems/rautomation-0.9.2.gem"
window = RAutomation::Window.new :title => "Opening rautomation-0.9.2.gem"
ok_button = window.button(:text => "&OK")
ok_button.exists?
cancel_button = window.button(:text => "&Cancel")
cancel_button.exists?ok_button.exists?那cancel_button.exists呢?都返回false。因此我不能点击按钮。
我也试过了:
window.buttons.length来查找按钮数,但它返回0。
有没有人能帮我解释一下为什么用rautomation检测不到这些按钮?如果我做错了什么,请纠正我。
下面是一个弹出窗口:

发布于 2013-07-25 03:05:06
此对话框的问题在于它不使用本机Windows控件。当您使用Spy++或AutoIt Window Info Tool时,它们也不会在该窗口中显示任何控件。
使用RAutomation时,您可以检查其上是否有本机控件,如下所示:
win = RAutomation::Window.new :title => /Opening rautomation/
p win.present?
p win.controls.length
p win.text
win.close此脚本的输出将为:
true
0
""换句话说,窗口是存在的,它没有任何类型的控件,文本是一个空字符串。此外,关闭窗口实际上关闭了它,这意味着我们正在与正确的窗口交互,而不是意外地与其他空窗口交互(注意:有时也可能发生这种情况)。
这意味着您不能直接使用AutoIt、RAutomation或许多其他自动化工具与控件进行交互。也许有一些特定的自动化工具可以用来处理这种类型的对话框-我不确定。
然而,有一个变通方法来处理这类窗口--将所需的击键发送到窗口。在这种情况下,发送return/enter键将完成此操作,因为这与单击"OK“按钮相同-您可以手动尝试。
以下是示例代码,它的工作方式与单击"OK“按钮相同:
win = RAutomation::Window.new :title => /Opening rautomation/
win.activate
sleep 1
win.send_keys :enter我不知道为什么,但出于某些原因,您必须通过调用Window#activate手动激活窗口,并在发送enter密钥之前等待一秒钟。
这样做之后,将弹出一个新的对话框,它使用本地窗口控件-你可以像你一开始期望RAutomation工作的那样处理它。
然而,如果你想使用:ms_uia适配器而不是默认的:win32,那么你不需要激活和休眠。
下面是一个使用:ms_uia适配器的完整工作示例:
win = RAutomation::Window.new :title => /Opening rautomation/, :adapter => :ms_uia
win.send_keys :enter
file_dialog = RAutomation::Window.new :title => /Enter name of file/
file_dialog.button(:value => "&Save").click要在第一个对话框中单击"Cancel“而不是"OK”,您可以使用Window#close来测试上面的窗口。
我建议你使用:ms_uia适配器而不是:win_32,因为它每天都在变得越来越稳定,并且在不久的将来会成为一个新的默认适配器。
要将:ms_uia适配器设置为默认适配器,您可以在加载RAutomation本身之前使用环境变量RAUTOMATION_ADAPTER,如下所示:
ENV["RAUTOMATION_ADAPTER"] ||= :ms_uia
require "rautomation"发布于 2013-09-17 11:46:55
对于我的情况,我必须发送两个:Tab键,然后发送:enter来保存文件。像这样:
driver.get "http://rubygems.org/gems/rautomation-0.9.2.gem"
window = RAutomation::Window.new :title => /Opening/i
if window.exist?
window.activate
window.send_keys :tab;
sleep 2;
window.send_keys :tab;
sleep 2;
window.send_keys :enter
end我不知道为什么我不能直接用以下命令保存文件:
window.activate; sleep 1; window.send_keys :enter发布于 2013-07-23 15:57:30
单击该链接时,我看不到任何弹出窗口。Chrome只是下载一个文件。:)这可能会有帮助:http://watirwebdriver.com/browser-downloads/
https://stackoverflow.com/questions/17782546
复制相似问题