如何在每个测试场景后重新启动浏览器

发布于 2025-01-04 20:16:28 字数 3312 浏览 2 评论 0原文

我正在使用 Cucumber 与 Capybara 和 Selenium-Webdriver 结合测试 Chrome 扩展。

我的测试非常简单:

  @chrome_extension
    @javascript
      Scenario: Test1
      Given I open a browser
      And I close the browser
      When I do nothing
      Then nothing happens

其中步骤定义为:

require 'selenium-webdriver'

Given /^I open a browser$/ do
  visit 'http://google.com'
  STDERR.puts self
end

Given /^I close the browser$/ do
  page.driver.browser.quit
end

Given /^I do nothing$/ do

end

Given /^nothing happens$/ do

end

当我调用 page.driver.browser.quit 时,它确实退出浏览器会话。但随后出现以下错误:

Connection refused - connect(2) (Errno::ECONNREFUSED)
      /Users/rui/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/http.rb:644:in `initialize'
      /Users/rui/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/http.rb:644:in `open'
      /Users/rui/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/http.rb:644:in `block in connect'
      /Users/rui/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/timeout.rb:44:in `timeout'
      /Users/rui/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/timeout.rb:87:in `timeout'
      /Users/rui/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/http.rb:644:in `connect'
      /Users/rui/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/http.rb:637:in `do_start'
      /Users/rui/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/http.rb:626:in `start'
      /Users/rui/.rvm/gems/ruby-1.9.2-p180@yesware/gems/webmock-1.7.8/lib/webmock/http_lib_adapters/net_http.rb:90:in `request_with_webmock'
      /Users/rui/.rvm/gems/ruby-1.9.2-p180@yesware/gems/selenium-webdriver-2.3.2/lib/selenium/webdriver/remote/http/default.rb:73:in `response_for'
      /Users/rui/.rvm/gems/ruby-1.9.2-p180@yesware/gems/selenium-webdriver-2.3.2/lib/selenium/webdriver/remote/http/default.rb:41:in `request'
      /Users/rui/.rvm/gems/ruby-1.9.2-p180@yesware/gems/selenium-webdriver-2.3.2/lib/selenium/webdriver/remote/http/common.rb:34:in `call'
      /Users/rui/.rvm/gems/ruby-1.9.2-p180@yesware/gems/selenium-webdriver-2.3.2/lib/selenium/webdriver/remote/bridge.rb:406:in `raw_execute'
      /Users/rui/.rvm/gems/ruby-1.9.2-p180@yesware/gems/selenium-webdriver-2.3.2/lib/selenium/webdriver/remote/bridge.rb:384:in `execute'
      /Users/rui/.rvm/gems/ruby-1.9.2-p180@yesware/gems/selenium-webdriver-2.3.2/lib/selenium/webdriver/remote/bridge.rb:228:in `deleteAllCookies'
      /Users/rui/.rvm/gems/ruby-1.9.2-p180@yesware/gems/selenium-webdriver-2.3.2/lib/selenium/webdriver/common/options.rb:67:in `delete_all_cookies'
      /Users/rui/.rvm/gems/ruby-1.9.2-p180@yesware/gems/capybara-1.1.2/lib/capybara/selenium/driver.rb:81:in `reset!'
      /Users/rui/.rvm/gems/ruby-1.9.2-p180@yesware/gems/capybara-1.1.2/lib/capybara/session.rb:70:in `reset!'
      /Users/rui/.rvm/gems/ruby-1.9.2-p180@yesware/gems/capybara-1.1.2/lib/capybara/dsl.rb:87:in `block in reset_sessions!'
      /Users/rui/.rvm/gems/ruby-1.9.2-p180@yesware/gems/capybara-1.1.2/lib/capybara/dsl.rb:87:in `each'
      /Users/rui/.rvm/gems/ruby-1.9.2-p180@yesware/gems/capybara-1.1.2/lib/capybara/dsl.rb:87:in `reset_sessions!'
      /Users/rui/.rvm/gems/ruby-1.9.2-p180@yesware/gems/capybara-1.1.2/lib/capybara/cucumber.rb:10:in `After'

每种情况后重新启动浏览器的正确方法是什么?我需要每个测试都尽可能无状态。

提前致谢!

I am testing a Chrome extension using Cucumber in conjunction with Capybara and Selenium-Webdriver.

My test is very simple:

  @chrome_extension
    @javascript
      Scenario: Test1
      Given I open a browser
      And I close the browser
      When I do nothing
      Then nothing happens

Where the steps are defined as:

require 'selenium-webdriver'

Given /^I open a browser$/ do
  visit 'http://google.com'
  STDERR.puts self
end

Given /^I close the browser$/ do
  page.driver.browser.quit
end

Given /^I do nothing$/ do

end

Given /^nothing happens$/ do

end

When I call page.driver.browser.quit, it does quit out of the browser session. But then the following error occurs:

Connection refused - connect(2) (Errno::ECONNREFUSED)
      /Users/rui/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/http.rb:644:in `initialize'
      /Users/rui/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/http.rb:644:in `open'
      /Users/rui/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/http.rb:644:in `block in connect'
      /Users/rui/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/timeout.rb:44:in `timeout'
      /Users/rui/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/timeout.rb:87:in `timeout'
      /Users/rui/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/http.rb:644:in `connect'
      /Users/rui/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/http.rb:637:in `do_start'
      /Users/rui/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/http.rb:626:in `start'
      /Users/rui/.rvm/gems/ruby-1.9.2-p180@yesware/gems/webmock-1.7.8/lib/webmock/http_lib_adapters/net_http.rb:90:in `request_with_webmock'
      /Users/rui/.rvm/gems/ruby-1.9.2-p180@yesware/gems/selenium-webdriver-2.3.2/lib/selenium/webdriver/remote/http/default.rb:73:in `response_for'
      /Users/rui/.rvm/gems/ruby-1.9.2-p180@yesware/gems/selenium-webdriver-2.3.2/lib/selenium/webdriver/remote/http/default.rb:41:in `request'
      /Users/rui/.rvm/gems/ruby-1.9.2-p180@yesware/gems/selenium-webdriver-2.3.2/lib/selenium/webdriver/remote/http/common.rb:34:in `call'
      /Users/rui/.rvm/gems/ruby-1.9.2-p180@yesware/gems/selenium-webdriver-2.3.2/lib/selenium/webdriver/remote/bridge.rb:406:in `raw_execute'
      /Users/rui/.rvm/gems/ruby-1.9.2-p180@yesware/gems/selenium-webdriver-2.3.2/lib/selenium/webdriver/remote/bridge.rb:384:in `execute'
      /Users/rui/.rvm/gems/ruby-1.9.2-p180@yesware/gems/selenium-webdriver-2.3.2/lib/selenium/webdriver/remote/bridge.rb:228:in `deleteAllCookies'
      /Users/rui/.rvm/gems/ruby-1.9.2-p180@yesware/gems/selenium-webdriver-2.3.2/lib/selenium/webdriver/common/options.rb:67:in `delete_all_cookies'
      /Users/rui/.rvm/gems/ruby-1.9.2-p180@yesware/gems/capybara-1.1.2/lib/capybara/selenium/driver.rb:81:in `reset!'
      /Users/rui/.rvm/gems/ruby-1.9.2-p180@yesware/gems/capybara-1.1.2/lib/capybara/session.rb:70:in `reset!'
      /Users/rui/.rvm/gems/ruby-1.9.2-p180@yesware/gems/capybara-1.1.2/lib/capybara/dsl.rb:87:in `block in reset_sessions!'
      /Users/rui/.rvm/gems/ruby-1.9.2-p180@yesware/gems/capybara-1.1.2/lib/capybara/dsl.rb:87:in `each'
      /Users/rui/.rvm/gems/ruby-1.9.2-p180@yesware/gems/capybara-1.1.2/lib/capybara/dsl.rb:87:in `reset_sessions!'
      /Users/rui/.rvm/gems/ruby-1.9.2-p180@yesware/gems/capybara-1.1.2/lib/capybara/cucumber.rb:10:in `After'

What is the proper way of restarting the browser after each scenario? I need every test to be as stateless as possible.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

傻比既视感 2025-01-11 20:16:28

使用 Watir-webdriver,它与 selenium 非常相似。

您可以使用钩子来完成此操作。对钩子进行一些研究以更好地理解,但在项目的支持目录中,您需要创建一个钩子文件。我使用 ruby​​ 和 ruby​​mine 来运行我的测试,因此在您的环境中可能会有所不同。

你的钩子文件可能看起来像这样。

require 'watir-webdriver'



Before do
  @browser = Watir::Browser.new :chrome
end


After do
  @browser.close
end

然后,在运行每个场景之前,它将打开一个浏览器实例,并在场景完成时关闭浏览器。

Using Watir-webdriver, which is very similiar to selenium.

You can do this by using a hook. Do some research on hooks to get a better understanding, but in your support directory for your project, you would want to create a hook file. I am using ruby and rubymine to run my tests, so in your environment it may be different.

Your hook file might look something like this.

require 'watir-webdriver'



Before do
  @browser = Watir::Browser.new :chrome
end


After do
  @browser.close
end

Then, before each scenario is ran, it will open an instance of the browser and close the browser when the scenario is done.

﹎☆浅夏丿初晴 2025-01-11 20:16:28

在 After 挂钩中清除 cookie 并刷新浏览器。例如,我在 Watir-Webdriver 中使用它

After do |scenario|
  browser.cookies.clear
  browser.refresh
end

Clear cookies and refresh browser in an After hook. For example, I use this in Watir-Webdriver

After do |scenario|
  browser.cookies.clear
  browser.refresh
end
瑾夏年华 2025-01-11 20:16:28

可能有一种更优雅的方法来做到这一点,但我会考虑将每个场景放在自己的 rake 任务中,并在 cucumber 中使用标签,如下所示:

desc "Run scenario 1"
task :scenario_1 do
 cmd = "cucumber --tags @1"
 `#{cmd}`
end`

这样做将完全实现您所追求的目标,因为 Cucumber 支持开箱即用的功能与 --tags 参数。

There might be a more elegant way of doing this, but I would consider having each scenario in its own rake task and using tags in cucumber, like so:

desc "Run scenario 1"
task :scenario_1 do
 cmd = "cucumber --tags @1"
 `#{cmd}`
end`

Doing this would accomplish exactly what you're after since Cucumber supports this out of the box with the --tags argument.

手心的温暖 2025-01-11 20:16:28

删除了java示例。

Capybara 试图通过预测您想要做什么来节省您的工作量。 Capybara 在每一步后都会关闭会话。如果您显式关闭浏览器会话,那么当水豚尝试清理 cookie 时,就没有要清理的会话。

如果你不想让水豚处理这些事情,那就自己做,不用水豚。

Deleted java example.

Capybara is trying to save you work by anticipating what you want to do. Capybara closes the session after every step. If you close the browser session explicitly then when capybara tries to clean cookies there is no session to clean.

If you don't want capybara to handle these sorts of things then do it yourself without capybara.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文