问题描述
我正在使用 ruby 中的 selenium-webdriver 和 cucumber 为网站自动化测试用例.我需要每个功能以特定的顺序运行并使用相同的浏览器窗口.Atm 每个功能都会创建一个新窗口来运行测试.尽管在某些测试用例中这种行为是需要的——在许多情况下它不是.到目前为止,从我的研究来看,关于是否可以在整个测试用例中使用 selenium 驱动相同的浏览器窗口,似乎有不同的答案.我遇到的大多数答案都是针对其他语言的,并且是特定于浏览器的解决方法(我在测试 IE 时正在开发我的测试,但预计会在其他浏览器中运行这些测试).我在 Ruby 中工作,从我读过的内容来看,好像我必须为页面创建一个类?我很困惑为什么我必须这样做或者这有什么帮助.
I am automating test cases for a website using selenium-webdriver and cucumber in ruby. I need each feature to run in a particular order and using the same browser window. Atm each feature creates a new window to run test in. Though in some test cases this behavior is desired- in many cases it is not. From my research so far it seems there are mixed answers about whether or not it is possible to drive the same browser window with selenium throughout test cases. Most answers I have run into were for other languages and were work arounds specific to a browser (I am developing my test while testing IE but will be expected to run these test in other browsers). I am working in Ruby and from what I have read it seems as though I'd have to make a class for the page? I'm confused as to why I would have to do this or how that helps.
我的 env.rb 文件:
my env.rb file:
require 'selenium-webdriver'
require 'rubygems'
require 'nokogiri'
require 'rspec/expectations'
Before do
@driver ||= Selenium::WebDriver.for :ie
@accept_next_alert = true
@driver.manage.timeouts.implicit_wait = 30
@driver.manage.timeouts.script_timeout = 30
@verification_errors = []
end
After do
#@driver.quit
#@verification_errors.should == []
end
到目前为止,我收集了一些关于有类似问题的人的信息:https://code.google.com/p/selenium/issues/详细信息?id=18有吗有什么方法可以将已经运行的浏览器附加到 java 中的 selenium webdriver?
Some information I've gathered so far of people with similar problems: https://code.google.com/p/selenium/issues/detail?id=18 Is there any way to attach an already running browser to selenium webdriver in java?
如果我的问题不清楚,请向我提问.我有更多的测试要创建,但如果我的基础草率且缺少请求的功能,我不想继续创建测试.(如果您在我的代码中发现任何其他问题,请在评论中指出)
Please ask me questions if anything about my question is not clear. I have many more test to create but I do not want to move on creating test if my foundation is sloppy and missing requested capabilities. (If you notice any other issues within my code please point them out in a comment)
推荐答案
Before
钩子在每个场景之前运行.这就是每次打开新浏览器的原因.
The Before
hook is run before each scenario. This is why a new browser is opened each time.
改为执行以下操作(在 env.rb 中):
Do the following instead (in the env.rb):
require "selenium-webdriver"
driver = Selenium::WebDriver.for :ie
accept_next_alert = true
driver.manage.timeouts.implicit_wait = 30
driver.manage.timeouts.script_timeout = 30
verification_errors = []
Before do
@driver = driver
end
at_exit do
driver.close
end
在这种情况下,浏览器将在开始时打开(在任何测试之前).然后每个测试都会抓取该浏览器并继续使用它.
In this case, a browser will be opened at the start (before any tests). Then each test will grab that browser and continue using it.
注意:虽然在测试中重复使用浏览器通常是可以的.您应该小心需要以特定顺序运行的测试(即变得依赖).依赖测试可能很难调试和维护.
Note: While it is usually okay to re-use the browser across tests. You should be careful about tests that need to be run in a specific order (ie become dependent). Dependent tests can be hard to debug and maintain.
这篇关于如何使用 selenium-webdriver (ruby) 使用相同的浏览器窗口进行自动化测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2