How to restart the browser after each test case

I am testing the Chrome extension with 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

If 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 exits 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 correct way to restart the browser after each script? I need each test to be as stateless as possible.

Thanks in advance!

+5
source share
4 answers

Use Watir-webdriver, which is very similar to selenium.

, . , , . ruby ​​ rubimine , .

.

require 'watir-webdriver'



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


After do
  @browser.close
end

, , , .

+1

, , rake , :

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

, , Cucumber --tags.

0

Delete cookies and refresh your browser after completion. For example, I use this in Watir-Webdriver

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

Remote java example.

Capybara is trying to save you from work, anticipating what you want to do. Capybara closes the session after each step. If you close the browser session explicitly, then when capybara tries to clear the cookies, the session is not cleared.

If you do not want capybara to handle such things, do it yourself without capybara.

0
source

All Articles