Try adding cucumber to watir
April 17th, 2009

Cucumber is the hot new testing tool that can execute plain-text documents as automated functional tests. Cucumber basically consists of features and steps with some handy-dandy support files. Watir is an open-source library for automating web browsers. I am finding huge productivity gains when combing the two.
Install Cucumber / Watir and execute ruby script/generate cucumber inside your Rails app.
This will generate the below features folder.

Now open the support folder and add the watir configuration code to your env.rb.
Ok now that we are all configured let’s take a look at some code, starting with my sample security.feature.

Notice Line 4 “Given I am on the login page“. Cucumber takes this line and treats it as a regular expression. It will try and find a match in either the step_definitions and/or support folder. “the login page” portion can be found inside paths.rb in my support directory under the “path_to(page_name)” method.


So now any feature steps that end in “the login page” will send the browser to the url “http://deploy.visualcv.com/login”, thanks to watir.
But what about the “I am on” portion? Well, that can be found in the page_steps.rb under the step_definitions directory.


See how “.goto” watir method points to the (page_to(page_name)) method found in paths.rb. That is how “Given I am on the login page” gets executed properly from the security.feature.
Good news? That is as complex as it gets. Now for the easy steps.
Let’s take a look at Line 5 of the security.feature And I click the link “Forgot Your Password?“. Under the step_definitions directory I have a link_steps.rb file.

All I am doing here is passing in the text “Forgot Your Password?” into the watir hash via the block(|text|). I can pass in any link text I wish to this one line of code. Talk about Refactoring your code.
So, I’ve decided to keep the same concept with buttons, assertions, text_fields, etc…



This makes scripting very fast, very organized, very easy and very English.
Enough code talk, let’s see some action in a ScreenCast!
One Response to “Try adding cucumber to watir”
Leave a Reply
|



October 22nd, 2009 at
Other relationships include your faith leader, teacher, or counselor. ,