Tuesday 9 March 2010

Setting up Cucumber to use the Watir test runner

Recently I was asked to demo Cucumber and how our developers could use it to automate acceptance criteria. I've worked a lot with Cucumber as a way of running Watir commands but I couldn't remember the initial steps you go through to get it installed. After googling a few things I found a couple of links that were really useful - the Cucumber website is good and also a blog post by my friend Mike Wagg helps map out the Watir aspect.

So let's fast forward a bit, I had installed Ruby, Cucumber, Watir and everything was fine no errors at all. I even wrote a quick Cuke test to search Google for the phrase "Cucumber". Yep all worked well so everything looked good for me to start coding the demo.

Now this is where I lost about 2 days OF MY LIFE!!! (Ok maybe not that much but more time than I would've liked). I created a nice new Visual Studio solution with a bare bones HTML form and a couple of textboxes. I typed out a rough scenario with some acceptance criteria and filled out the step definitions. It was going to be around cars (very manly) so my first couple of step definitions looked like this:

Given /^I am on the Cucumber brown bag page$/ do
@browser.goto 'http://localhost/cucumberbrownbag/'
end

And /^I have entered my car details correctly$/ do
@browser.text_field(:name, 'makeTextBox').set("Fiesta")
@browser.text_field(:name, 'modelTextBox').set("1.6 Ghia")
end


Ok looks good so let's run the Cuke test... FAIL! I got this:

Unable to locate element, using :name, "makeTextBox"

Hmmm that's not good so I double checked my naming and step syntax, they all looked correct. Then I tried to rejig the steps, same error. Then I tried to see if Watir knew the textbox existed on that page:

Given /^I am on the Cucumber brown bag page$/ do
@browser.goto 'http://localhost/cucumberbrownbag/'
@browser.text_field(:name, 'makeTextBox').exists?
end

Which gave me this error:

unknown property or method `document'
HRESULT error code:0x800706b5
The interface is unknown. (WIN32OLERuntimeError)

Hmmm now this is strange! I stepped back for a moment to try to work this out. It felt like it was something to do with the browser not being found or Watir being unable to attach to it. What happens if I change the step definition from my localhost domain to my IP address?

Given /^I am on the Cucumber brown bag page$/ do
@browser.goto 'http://10.1.0.185/cucumberbrownbag/'
@browser.text_field(:name, 'makeTextBox').exists?
end

Bingo it worked... so it's permissions! Don't ask me why but I can only use localhost in my Cucumber scripts if I'm running the command line as administrator OR if I use my domain or IP address instead.

On a separate note, I found a link to Scott Hanselman's blog on interactive Watir which allowed me to bring up the Interactive Ruby tool - IRB (get it from the command line by typing "irb"). It's VERY useful as a way of stepping through the Watir process and seeing why things aren't working.

1 Comments:

Blogger Chuck van der Linden said...

It's IE and it's 'protected mode' stuff. we see this from about vista onwards, and especially with IE8.

In IE7, if you changed security contexts, (e.g clicked something on a trusted site that went to a non trusted site, or vise versa) it would open a new IE window.. this made it very obvious what was going on.

it also annoyed the hell out of users.

it appears that what IE8 does is basically under the covers, close the current IE browser 'session' (I'm using that loosely) and establish a new one in the new security context. it happens in the blink of an eye, but the effect is that the browser object in Watir has just been disconnected from the browser.. If you re-attach at that point, you'll be able to see stuff on the page.

The solutions seem to revolve around a combination of turning off protected mode browsing, and/or making sure that every page (including the default) that the browser might access during the duration of your test is in the same security zone (e.g. trusted)

18 January 2011 at 23:25  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home