watir 浏览器对象可以在以后的 Ruby 进程中重用吗? [英] Can a watir browser object be re-used in a later Ruby process?

查看:54
本文介绍了watir 浏览器对象可以在以后的 Ruby 进程中重用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,假设经常运行一个脚本来打开浏览器并执行网络操作:

So let's say pretty often a script runs that opens a browser and does web things:

require 'watir-webdriver'

$browser = Watir::Browser.new(:firefox, :profile => "botmode")
 => #<Watir::Browser:0x7fc97b06f558 url="about:blank" title="about:blank"> 

它可能会以 browser.close 优雅地结束,或者它可能会更快崩溃并留下内存饥渴的 Firefox 进程,直到它们累积并使服务器缓慢爬行时才会被注意到.

It could end gracefully with a browser.close, or it could crash sooner and leave behind the memory-hungry Firefox process, unnoticed until they accumulate and slow the server to a crawl.

我的问题是双重的:

  • 有什么好的做法可以确保即使在任何脚本失败导致立即错误退出的情况下,子进程也将始终得到清理(我已经有很多短的 begin-rescue-end 块用于其他不相关的小测试)
  • 更重要的是,我能不能简单地记住这个 Watir::Browser:0x7fc97b06f558 对象地址或 PID 并以某种方式将它重新分配给一个全新的 Ruby 进程中的另一个 $browser 变量,例如 irb?IE.稍后可以在同一台机器上使用 watir-webdriver 将 webdriver 上的孤立浏览器重新附加到另一个程序中吗?然后我可以从 irb 进入并重新连接到崩溃的 Ruby 脚本留下的浏览器,检查它所在的网站,检查出了什么问题,哪些元素与预期不同,等等.

后者的另一个非常有利的用途是避免每天可能有数百次浏览器启动和关闭的开销......最好让一个像守护进程一样活着.第一次运行将尝试使用我专门准备的 botmode 配置文件重用以前的浏览器对象,否则创建一个.然后我会故意不在我的脚本末尾调用 $browser.close .如果没有别的,我运行一个 at 工作来杀死 Xvfb :99 显示 FF 无论如何都会在一天结束时在里面运行(让 FF 别无选择,只能随它而死,如果仍在运行).是的,我知道 Selenium 独立 jar,但也试图避免 Java 服务足迹.

Another hugely advantageous use of the latter would be to avoid the overhead of potentially hundreds of browser startups and shutdowns per day...best to keep one alive as sort of a daemon. The first run would attempt to reuse a previous browser object using my specially prepared botmode profile, otherwise create one. Then I would deliberately not call $browser.close at the end of my script. If nothing else I run an at job to kill the Xvfb :99 display FF runs inside of at the end of the day anyway (giving FF no choice but to die with it, if still running). Yes I am aware of Selenium standalone jar, but trying to avoid that java service footprint too.

抱歉,如果这更像是一个基本的 Ruby 问题.我只是不知道如何措辞并不断得到不相关的搜索结果.

Apologies if this is more a basic Ruby question. I just wasn't sure how to phrase it and keep getting irrelevant search results.

推荐答案

我想,你不能只记住另一个进程的变量.但是解决方案可能是创建一个主进程并在线程中循环处理您的脚本,定期检查浏览器运行状态.我在 Cucumber + watir 的验收测试中使用了一些类似的东西.所以它会是这样的:

I guess, U cant just remember the variable from another process. But the solution might be creating a master process and process your script in loop in thread, periodically checking the browser running state. I'm using some thing similar in my acceptance tests on Cucumber + watir. So it will be some thing like that:

require 'rubygems'
require 'firewatir' # or watir
@browser = FireWatir::Firefox.new

t = Thread.new do
    @browser.goto "http://google.com"
    #call more browser actions here
end
while not_exit?
  if t.stop?
      # error occurred in thread, restart or exit
  end
  if browser_live?
      # browser was killed for a some reason
      # restart or exit
  end
end
@browser.close

不退出?- 可以超过 ctrl+C 的 TRAP

not_exit? - can be over TRAP for the ctrl+C

浏览器直播?- 您可以检查 firefox 浏览器是否运行进程列表

browser_live? - you can check if firefox browser running with processes listings

这很棘手,但可能对你有用

It is quite tricky but might work for you

这篇关于watir 浏览器对象可以在以后的 Ruby 进程中重用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆