gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 21:22:47 26 4
gpt4 key购买 nike

我正在使用 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 时,它会退出浏览器 session 。但随后出现以下错误:
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'

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

提前致谢!

最佳答案

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

您可以通过使用钩子(Hook)来做到这一点。对钩子(Hook)进行一些研究以获得更好的理解,但是在项目的支持目录中,您需要创建一个钩子(Hook)文件。我正在使用 ruby​​ 和 ruby​​mine 来运行我的测试,所以在您的环境中它可能会有所不同。

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

require 'watir-webdriver'



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


After do
@browser.close
end

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

关于selenium - 如何在每个测试场景后重新启动浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9217114/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com