简体   繁体   English

无法在Ruby Watir中聚焦新窗口

[英]Cannot focus new window in Ruby Watir

I have done a lot of research before asking this questions but unable to find an answer. 在问这个问题之前,我已经做了很多研究,但是找不到答案。

I am trying to automate a website using Ruby Selenium + Webdriver. 我正在尝试使用Ruby Selenium + Webdriver自动化网站。 I am having an issue with the script hanging once new window opened. 打开新窗口时,脚本挂起出现问题。 I am unable to focus on new window or do anything else. 我无法专注于新窗口或执行其他任何操作。

Here is the code that opens up new window: 这是打开新窗口的代码:

<a id="theField" class="RightTextHeading" onclick="javascript:openTheWindow('someInfo.aspx?type=','300','300');if(window.document.RetValue == '5'){window.parent.LoadinIframe('someStuff.aspx?info=N&amp;Stuff=','Some Data here > Full View','false');}; return false;" href="../MoreStuff/#" style="text-decoration: none;">ClickMe <span class="context"> opens an overlay</span></a>

Here is my ruby code: 这是我的红宝石代码:

    require 'rubygems'
require 'watir-webdriver'
require 'selenium-webdriver'

prefs = {
  :download => {
    :prompt_for_download => false,
    :default_directory => '.'
  }
}
client = Selenium::WebDriver::Remote::Http::Default.new
#client.timeout = 600 # seconds - default is 60
client.timeout = 30 # seconds - default is 60
d = Selenium::WebDriver.for :chrome, :prefs => prefs, :http_client => client
b = Watir::Browser.new d

saved_cookies = b.cookies.to_a

b.goto'https://somewebsite.com'
begin
    b.iframe(:id => 'iframeBody').a(:id => "somelink").click
rescue
    puts 'Rescue'
end
sleep 2
puts "before"
#if b.window(:title=>"The new window").exists?
   #puts " Test passed. New window opened!"
#else
   #puts " Test Failed! No window found"
#end
#b.window(:title => /The new window/).use do
#b.window.last.use do
#   puts "here" 
#end
#puts b.window(:title => "The new window").exists?
puts b.windows.size
puts "after"

#b.close

I have commented out different things I attempted to make this work. 我已经注释掉了我尝试进行这项工作的不同内容。 It seems that Ruby just sits and waits after the new window is opened and I do not know how to get it out of the waiting state. 看起来Ruby只是坐着等待,在打开新窗口之后等待,我不知道如何使它脱离等待状态。

If I do not begin/rescue, the code stops execution until it times out. 如果我没有开始/救援,代码将停止执​​行,直到超时。 When I am trying to rescue I can get control back but still unable to focus on the new window. 当我尝试营救时,我可以重新获得控制权,但仍然无法专注于新窗口。

Any ideas are appreciated? 任何想法表示赞赏吗? Please let me know if you have additional questions. 如果您还有其他问题,请告诉我。

Thanks, -Andrey 谢谢,-安德烈

Im not entirely sure what your trying to do with the test, but the answer on how to do something with the new window is already in your code to some extent 我不能完全确定您要如何进行测试,但是在某种程度上,关于如何使用新窗口执行操作的答案已经在您的代码中

#b.window.last.use do

should be 应该

#b.windows.last.use 

or 要么

@browser.windows.last.use

Will have your test start using the new window once it appears. 出现新窗口后,即可开始测试。

Depending on what your trying to do, may change where in your code that line should go. 根据您的尝试,可能会更改该行应在代码中的位置。 But that will let you access and interact with the new window. 但这将使您能够访问新窗口并与之交互。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM