Unable to find buttons of system popup using rautomation

蹲街弑〆低调 提交于 2019-12-24 00:05:14

问题


I'm writing tests using Selenium WebDriver and rautomation to handle system popup. I tried it on irb like following:

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? and cancel_button.exists? are returning false. Hence I can't click on the buttons.

I also tried:

window.buttons.length

to find number of buttons, but it's returning 0.

Could someone please help me why the buttons aren't detected using rautomation? Please correct me if I'm doing something wrong.

Here is a popup:


回答1:


The problem with this dialog is that it does not use native Windows controls. When you use Spy++ or AutoIt Window Info Tool then they do not show you any controls in that window either.

When using RAutomation you can check if it has native controls on it or not like this:

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

The output of this script will be:

true
0
""

In other words - window was present, it had zero controls of any kind and text was an empty string. Also, closing the window really closed it which you can verify visually - this means that we were interacting with the correct window and not accidentally with some other empty window (beware: this might sometimes happen too).

This all means that you cannot interact with the controls directly with AutoIt, RAutomation or many other automation tools. There might be some specific automation tools available for handling these kind of dialogs - i'm not sure.

There is however a workaround how to work with these kind of windows - send needed keystrokes to the window. In this case, sending a return/enter key would do the trick since that is the same as clicking on the "OK" button - you can try that manually.

Here is example code, which works the same as clicking on the "OK" button:

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

I'm not sure why, but for some reason you have to activate the window manually by calling Window#activate and wait a second before sending that enter key.

After doing that a new dialog will pop up, which uses native Windows controls - you can handle that as you would have expected RAutomation to work in the first place.

However, if you would use a :ms_uia adapter instead of the default :win32 then you don't need to activate and sleep.

Here is a fully working example with :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

To click "Cancel" on the first dialog instead of "OK" you can just use Window#close as i was using to test the window above.

I would recommend you to use :ms_uia adapter instead of :win_32 since it is getting more stable every day and will be a new default one in the far future.

To set :ms_uia adapter for default one you can use environment variable RAUTOMATION_ADAPTER before loading RAutomation itself like this:

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



回答2:


For my condition, I have to send two :tab key and then send :enter to save the file. like:

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

I don't know why I can't just save the file with:

window.activate; sleep 1; window.send_keys :enter



回答3:


I do not see any popup when I click that link. Chrome just downloads a file. :) This could help: http://watirwebdriver.com/browser-downloads/




回答4:


This code worked for me:

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)


来源:https://stackoverflow.com/questions/17782546/unable-to-find-buttons-of-system-popup-using-rautomation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!