Reputation: 63
I run tests using Cucumber in conjunction with Capybara and Selenium-Webdriver. I want restart browser after each scenario. Here is my env.rb. I can add in After section something like this:
After do |scenario|
onError scenario if scenario.failed?
page.driver.browser.close
end
but this kills browser after first scenario passed and all other scenarios failed with reasonable error:
Errno::ECONNREFUSED: Connection refused - connect(2) for "127.0.0.1" port 7055
Is there way to refactor my env.rb to use Before hook to start browser on every scenario?
Upvotes: 4
Views: 2203
Reputation: 18634
For me a simple After
block does the job without manual initialization in a Before
block.
After("@javascript") do
Capybara.page&.driver&.quit
end
Also see https://stackoverflow.com/a/28559244/520567
P.S. as for the reason to do so, sometimes our tests failed being killed for unknown reason. After investigation it turned out the OOM killer did it. So the small 2GB CI runner had to run mysql, redis, dnsmasq, chrome and the actual project and it was too much at times. The biggest users were that MySQL by default uses up ~500MB and Chrome starting several processes each 100-300MB. I added options to reduce number of processes and memory but still running all tests in a single browser made it use more and more resources. So restarting after each tests shows some reasonable savings and for now that did the trick (in addition to the startup options of both Chrome and mysql).
Upvotes: 0
Reputation: 63
Simple:
Before do
Capybara::Selenium::Driver.new(app, :browser => :firefox, :profile => profile)
end
Upvotes: 1