sauce-archives / sauce_ruby

This is the Ruby client adapter for testing with Sauce Labs, a Selenium-based browser testing service (saucelabs.com).
Apache License 2.0
98 stars 115 forks source link

Using Background: in Cucumber scenarios with sauce_ruby starts two SauceLabs sessions per scenario #170

Open smartkiwi opened 11 years ago

smartkiwi commented 11 years ago

Hi

I noticed that when my Cucumber scenarios have Background: section then sauce_ruby/sauce-cucumber starts two Selenium browser sessions per scenario.

This might be related to the way how sauce-cucumber integrates with the Cucumber - using Before hook, and looks like this hook is called twice for every scenario when there is Background tag.

Also I see that Cucumber for runs Background step before starting scenario - it could be related to my problem: https://github.com/cucumber/cucumber/issues/52

PS I'm not using "@selenium" tag in my scenarios but register Suace hooks manually in the features/support/hooks . I'm doing this so there will be no need to modify my features while I'm running them locally. I'm thinking about

Before do |scenario|

  if ENV['SELENIUM_DRIVER'].nil?
    @browser = Selenium::WebDriver.for :firefox
    @local_browser = true
  else
    Sauce::Capybara::Cucumber.before_hook
    @browser = Capybara.current_session.driver.browser.driver
  end

end

if ENV['SELENIUM_DRIVER']
  Around do |scenario, block|
      Sauce::Capybara::Cucumber.around_hook(scenario, block)
  end
end
DylanLacey commented 11 years ago

The Before hook is setting the Cucumber Capybara driver to Sauce. Your "Background" then uses that driver. The driver checks for an existing Sauce session, then creates one.

The 'Around' hook then creates a NEW Sauce session, and sets it as the Sauce session for the next instantiation of the Capybara driver. Because one's already been created, you end up with another session, that then remains empty.

I think the best approach here would be to check if an existing Capybara session is open, and if so, to not create another. That would be an interim fix, until this is patched in Cucumber. I think you're right about it being related to cucumber/cucumber#52

DylanLacey commented 11 years ago

We also have to take into account serial and parallel platform tests. I'll have to have a play around with this. Waiting for Cucumber 2 might be the wisest course, but I'll give it a bit more thought.

geabby commented 9 years ago

Just came across this issue while working on sauce labs. Currently the background scenarios triggers a sauce session and the normal scenario triggers a new one which seems to fail. Is there a way to workaround the background scenarios?

DylanLacey commented 9 years ago

Not currently; The bug in Cucumber was patched for Cuke 2+ but not Cuke 1. There might be a way I'm not sure of, and if so PRs are welcome!