我正在使用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'
每种方案后重启浏览器的正确方法是什么?我需要每个测试尽可能无状态。
提前致谢!
答案 0 :(得分:1)
使用Watir-webdriver,它与硒非常相似。
您可以使用钩子来完成此操作。对钩子进行一些研究以获得更好的理解,但是在项目的支持目录中,您需要创建一个钩子文件。我使用ruby和rubymine来运行我的测试,所以在你的环境中可能会有所不同。
你的钩子文件可能看起来像这样。
require 'watir-webdriver'
Before do
@browser = Watir::Browser.new :chrome
end
After do
@browser.close
end
然后,在运行每个方案之前,它将打开浏览器的实例并在方案完成时关闭浏览器。
答案 1 :(得分:1)
清除Cookie并在After hook中刷新浏览器。例如,我在Watir-Webdriver
中使用它After do |scenario|
browser.cookies.clear
browser.refresh
end
答案 2 :(得分:0)
这可能有更优雅的方式,但我会考虑让每个场景都在自己的rake任务中并使用黄瓜中的标签,如下所示:
desc "Run scenario 1"
task :scenario_1 do
cmd = "cucumber --tags @1"
`#{cmd}`
end`
这样做可以完全达到您所追求的目标,因为Cucumber使用--tags参数开箱即用。
答案 3 :(得分:0)
删除了java示例。
Capybara试图通过预测你想做的事来拯救你的工作。 Capybara在每一步都结束会议。如果您明确关闭浏览器会话,那么当capybara尝试清理cookie时,没有要清理的会话。
如果你不想让水豚处理这些事情,那就不用水豚就自己做吧。