2
votes

Hello StackOverflow members,

I've been searching the site (and the rest of the web) for an answer to this question, but all my search queries returned the awesome features of Watir... I seem to be one of the few people running into this particular problem. I hope someone out there has an easy answer for me :)

I'm working on website test automation. The current test set is written with Cucumber/Ruby/Selenium-Webdriver/Capybara. I'm personally interested in switching to Watir-Webdriver in combination with Cucumber and Ruby, but I'm struggling with the following:

Every time I run my cucumber test, Watir opens not one, but TWO browser screens. It seems to want to initiate a blank screen (which just goes to the site I configurated as default), plus another browser screen in which the actual test steps are executed.

Keep in mind, I'm rather new to this and I'm having this trouble when simply following a beginners tutorial. Nothing fancy as of yet.

In my 'Support/env.rb' file, I have the following:

require "cucumber"
require 'watir-webdriver'

app_host = ENV['apphost']

Before do
  @browser = Watir::Browser.start app_host, :firefox
end


Before  do |scenario|
  @scenario_tag = scenario.source_tag_names
  @browser.cookies.clear
end

at_exit do
  @browser.close
end

The first bit in my steps file (GoogleSearch.rb -- yes it's that basic):

require_relative "../support/env"

Given(/^that I have gone to the Google page$/) do
  @browser.goto 'http://www.google.com'
end

Now, when I run this test, I expect just ONE browser to be initiated. But instead, the automation initiates TWO browser screens. One just stays in the background doing nothing, the other contains the test steps.

Again, I've searched for a while now (which I'm usually pretty good at), but I haven't found the answer to my problem anywhere. The only way I got it to work, is to start with a step in my steps file, initiating a browser (instead of doing this in the env.rb file). But I don't want to start each test with opening a browser..

Any help would be very much appreciated. If any more information from me is required, I'll update as soon as I can.

Thanks in advance!

1
Is the env.rb provided accurate? It is suspicious that you have both @browser as well as browser.Justin Ko
Hi Justin, thanks for your reply. I've updated my starting post with the current env.rb content. I did change 'browser.close' to '@browser.close' after I saw your comment about using @browser and browser. However, I still get two browser instances when starting one scenario...cin

1 Answers

3
votes

The problem is that env.rb is being loaded twice:

  • It is automatically included when running the cucumber command
  • It is being included a second time in GoogleSearch.rb when calling the line require_relative "../support/env".

As a result, each of the hooks is registered twice. In other words, Cucumber is seeing the hooks to run before each scenario as:

Before do
  @browser = Watir::Browser.start app_host, :firefox
end

Before  do |scenario|
  @scenario_tag = scenario.source_tag_names
  @browser.cookies.clear
end

Before do
  @browser = Watir::Browser.start app_host, :firefox
end

Before  do |scenario|
  @scenario_tag = scenario.source_tag_names
  @browser.cookies.clear
end

As you can see, Watir::Browser.start is called twice resulting in the two browsers. The first one is not used since the second call uses the same variable.

To solve the problem, simply remove the require_relative "../support/env" line.

Note that this will only address the issue with opening two browsers for each scenario. You will notice that you will still get a new browser for each scenario and that only the last browser gets closed. If you only want one browser for all scenarios, you should look at the global hooks.