Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restart browser after each test scenario

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!

like image 648
Rui Jiang Avatar asked Nov 04 '22 06:11

Rui Jiang


2 Answers

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.

like image 133
Mayshar Avatar answered Nov 09 '22 12:11

Mayshar


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
like image 20
Jason McI Avatar answered Nov 09 '22 11:11

Jason McI