Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cucumber Profiles and Global Hooks to select browser

Tags:

cucumber

I'm new to Cucumber and Ruby. I'm a Business Analyst used to writing requirements and test cases, and I've read up on Cucumber and Watir, but I'm having trouble getting what I need to happen to actually work.

I'm going to be testing a set of web pages in IE, Firefox, and Chrome, and as different types of users. I'm writing and tagging features, and making profiles in cucumber.yml so I can run cucumber with -p to run just the appropriate tagged tests for different user types.

I am creating different page objects so that I can easily log in as Basic user, Reports user, Editor user, etc. The feature files will have slightly different scenarios and pass/fail conditions for each type of user, and the step defs will call the appropriate page objects for logging in as those user types.

Rather than duplicate the tests three times each to open the right browser with watir-webdriver, and rather than having the browser open and close with each scenario, I wanted to make a global hook in env.rb or hooks.rb to open the browser once and only once before any features are run, and then close it with and after statement from the global hooks when the features have all run. Again, I am using profiles calling tagged scenarios to pick which tests to run for each user type.

The problem I am having is a step before that, just opening the browser. I know how to user watir-webdriver to open each browser.

What I wanted to was put global hooks in to open a browser before any testing. So in the env.rb I was going to put the browser = Watir::Browser.new call.

What I want to do is have three separate global hooks, one each for IE, Chrome, and Firefox, and call them based on a condition passed from a profile in the cucumber.yml, but I can't get it to work. I tried tagging them, and that didn't work. Can you not tag global hooks? I tried it with an if/then/else setup but that didn't work either.

I would like to be able to pass a parameter or tag from a profile in cucumber.yml to call only one global hook, such as:

basicuser_overviewtests_ie: --tags @basic @overview @ie

basicuser_overviewtests_ff: --tags @basic @overview @firefox

Basicuser_newpagetests_ie: --tags @basic @newpage @ie

Etc... the first two tags would refer to scenarios, the third to the correct env.rb hook.

Or, if I could pass a variable from the profile, that'd work too. I couldn't figure out how I could use an environment variable there though.

Am I missing something? Is this enough information to explain the issue? Thank you!


UPDATE: Part of the problem appears to be that cucumber didn't recognize the [support] folder when it was on the same level as the [features] folder, it needed to be under [features]. The cuke info I had said it would be Ok at the same level rather than inside features. Not the case.

So now I can get my three global hooks for the browsers to run, but tagging doesn't help, they all run at once even if tagged to only run before features tagged @ie or with profiles tagged only to run @ie.

like image 624
Ted Duffy Avatar asked Apr 19 '12 23:04

Ted Duffy


1 Answers

EDIT: pp. 147-148 from The Cucumber Book leads me to believe that this should work:

Before ('@ie') do
    @browser = Watir::Browser.new :ie
end

I haven't tested it, but that would probably get you what you wanted if you wanted to pass the browser as a tag.


I was not able to figure out how to read tags either, though I think there's probably a way to do it. I'll update you if I come across it in The Cucumber Book. I ended up using environment variables. Here's how I did it:

case ENV['BROWSER']
  when 'ie', 'Internet Explorer'
    browser = Watir::Browser.new :ie
  when 'ff', 'Firefox'
    browser = Watir::Browser.new :ff
  when 'chrome'
    browser = Watir::Browser.new :chrome
  when 'opera'
    browser = Watir::Browser.new :opera
  when 'debug'
    debug_profile = Selenium::WebDriver::Firefox::Profile.new
    debug_profile.add_extension 'features/support/firebug.xpi'
    browser = Watir::Browser.new :firefox, :profile => debug_profile
  else
    browser = Watir::Browser.new :ie
end

Before do
  @browser = browser
end

at_exit() do
  browser.close
end

Then I just pass in BROWSER=<browser> on the command line. For a more in-depth discussion of what I did to use ENV variables, you can read the blog post I wrote here.

like image 94
Doug Noel Avatar answered Sep 29 '22 12:09

Doug Noel