无法使用rautomation找到系统弹出按钮

我正在使用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? 正在返回假。 因此我无法点击按钮。

我也尝试过:

 window.buttons.length 

找到按钮的数量,但它返回0。

有人可以帮助我为什么没有使用rautomation检测到按钮? 如果我做错了,请纠正我。

这是一个弹出窗口:

窗口弹出窗口

此对话框的问题是它不使用本机Windows控件。 当您使用Spy ++或AutoIt窗口信息工具时,它们也不会在该窗口中显示任何控件。

使用RAutomation时,您可以检查它是否具有本机控件,如下所示:

 win = RAutomation::Window.new :title => /Opening rautomation/ p win.present? p win.controls.length p win.text win.close 

该脚本的输出将是:

 true 0 "" 

换句话说 – 窗口存在,它有任何类型的零控件,文本是一个空字符串。 此外,关闭窗口真的关闭它,你可以直观地validation – 这意味着我们正在与正确的窗口进行交互,而不是意外地与其他一些空窗口(请注意:这有时也可能发生)。

这意味着您无法使用AutoIt,RAutomation或许多其他自动化工具直接与控件进行交互。 可能有一些特定的自动化工具可用于处理这些类型的对话 – 我不确定。

然而,有一种解决方法如何使用这些类型的窗口 – 向窗口发送所需的击键。 在这种情况下,发送一个返回/回车键就可以了,因为这与单击“确定”按钮相同 – 您可以手动尝试。

这是示例代码,与单击“确定”按钮的工作方式相同:

 win = RAutomation::Window.new :title => /Opening rautomation/ win.activate sleep 1 win.send_keys :enter 

我不知道为什么,但由于某种原因你必须通过调用Window#activate手动激活窗口并在发送enter密钥之前等待一秒钟。

在执行此操作后,将弹出一个新对话框,该对话框使用本机Windows控件 – 您可以像处理预期的RAutomation一样处理它。

但是,如果您使用:ms_uia适配器而不是默认值:win32那么您不需要激活和hibernate。

以下是一个完整的示例:ms_uia adapter:

 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 

要在第一个对话框上单击“取消”而不是“确定”,您可以使用Window#close因为我用来测试上面的窗口。

我建议你使用:ms_uia adapter而不是:win_32因为它每天都变得更加稳定,并且在未来将是一个新的默认值。

要设置:ms_uia适配器为默认值,您可以在加载RAutomation本身之前使用环境变量RAUTOMATION_ADAPTER ,如下所示:

 ENV["RAUTOMATION_ADAPTER"] ||= :ms_uia require "rautomation" 

对于我的情况,我必须发送两个:tab键然后发送:输入以保存文件。 喜欢:

 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 

当我点击该链接时,我没有看到任何弹出窗口。 Chrome只是下载一个文件。 :)这可能会有所帮助: http : //watirwebdriver.com/browser-downloads/

这段代码对我有用:

 window = RAutomation::Window.new(:title => /Opening rautomation-0.9.2.gem/i) window.activate p window.exists? # => true sleep 2 window.send_keys(:down) window.send_keys(:enter) 
Interesting Posts