Showing posts with label cucumber. Show all posts
Showing posts with label cucumber. Show all posts

Sunday, January 2, 2011

Cucumber: Running a single feature / scenario


cucumber path/to/file.feature --line 33
or
rake features FEATURE=path/to/file.feature CUCUMBER_OPTS="--line 33"
or in vim
:! cucumber %:33

Thursday, December 23, 2010

Diffing tables in Cucumber


Then /^I should see the following within "([^"]*)" area:$/ do |selector, table|
actual_table = tableish("table#{selector} tr", "td,th" ).to_a
table.diff!(actual_table)
end


references:
https://github.com/aslakhellesoy/cucumber/wiki/multiline-step-arguments
http://railscasts.com/episodes/186-pickle-with-cucumber

Friday, December 17, 2010

Debugging: Show the webpage after a cucumber step


# Example
#
Scenario: Signing in
When I go to the home page
And I fill in "username" with "john"
And I fill in "password" with "john"
Then show me the page

# Actual implementation
# Uses webrat's method
#
Then /^show me the page$/ do
save_and_open_page
end

Suppress NOTICE messages from Postgresql when running rake cucumber


# Notice messages spewed
#
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index ...

# Fix
# Add to database config in config/database.yml
#
test:
...
min_messages: WARNING;;

Wednesday, January 27, 2010

rake cucumber:tags in verbose mode


rake cucumber:ok CUCUMBER_OPTS="--verbose"

Wednesday, September 30, 2009

Clicking on links in Webrat using XPath

If for some reason you wanna use XPath instead of CSS Selector to click on a link

# Cucumber step
And I follow "Edit" for "John Woo"

# Using Css Selector
within(%\...\) do
click_link(link)
end

# Using XPath
response.should have_xpath(%\//td[normalize-space(text())="#{name}"]/parent::tr//a[normalize-space(text())="#{link}"]/@id\) do |link_id|
click_link(link_id)
end

Tuesday, September 8, 2009

Better testing of form elements in Cucumber using form fieldsets


# Scenario:
# Sometimes too form elements --> messy!
# Also elements have same label names --> webrat won't know how to distinguish them
#
# Solution:
# Separate logical groupings of elements into fieldsets
# Cucumber test the fieldsets

# In feature file
When /../ do
Given I focus on XXX section
...
end

# In step definition file
Given /^I focus on "([^\"]*)" section do |section|
set_fielset_context(section)
end

Wednesday, September 2, 2009

Cucumber feature template

Feature:title

In order to [Business Value]
As a [Role]
I want to [Some Action]

Tuesday, August 18, 2009

Breaking Cucumber feature tests into separate smaller files

Previous I had the following Cucumber feature tests in a single file

# features/webrat/edit_enrollment_table

@edit_enrollment_details
Feature: Editing of enrollment details from enrollment table
In order to edit enrollment details easily
As an admin
I want to edit enrollment details from enrollment table

Scenario: Admin should be able to enter entry test date
....
Scenario: Admin should be able to edit entry test date
...
Scenario: Admin should be able to enter entry test status
...
Scenario: Admin should be able to edit entry test status
...
Scenario: Admin should be able to enter interview test date
...
Scenario: Admin should be able to edit interview test date
...
Scenario: Admin should be able to enter interview test status
...
Scenario: Admin should be able to edit interview test status
...

Pretty massive. Why not split them up into manageable bits?

# features/webrat/edit_entry_test_details

@edit_enrollment_details
@edit_entry_test_details
Feature: Editing of entry test details from enrollment table
In order to edit entry test details easily
As an admin
I want to edit entry test details from enrollment table

Scenario: Admin should be able to enter entry test date
...
Scenario: Admin should be able to edit entry test date
...
Scenario: Admin should be able to enter entry test status
...
Scenario: Admin should be able to edit entry test status
...

# features/webrat/edit_interview_test_details

@edit_enrollment_details
@edit_interview_test_details
Feature: Editing of interview test details from enrollment table
In order to edit interview test details easily
As an admin
I want to edit interview test details from enrollment table

Scenario: Admin should be able to enter interview test date
...
Scenario: Admin should be able to edit interview test date
...
Scenario: Admin should be able to enter interview test status
...
Scenario: Admin should be able to edit interview test status
...

So here's what I think the advantages are:

- Cleaner code in a single file. Well, at least not so chunky!
- Running small number of tests is much FASTER,
- Can still run full quite with proper tagging. ie. @edit_enrollment_details

Thursday, August 13, 2009

Cucumber > 0.3.7 freezes hash table and changes loading of shoulda


diff --git a/config/environments/test.rb b/config/environments/test.rb

-config.gem "thoughtbot-shoulda", :lib => "shoulda/rails", :source => "http://gems.github.com"
+config.gem "thoughtbot-shoulda", :lib => "shoulda", :source => "http://gems.github.com"


diff --git a/features/step_definitions/enrollment_steps.rb b/features/step_definitions/enrollment_steps.rb

- enrollments.hashes.each do |hash|
+ enrollments.hashes.each do |h|
+ hash = h.dup # Hash is frozen by cucumber. Not sure why.

Monday, July 27, 2009

Webrat's fill_in method on cucumber

Consider the following:

In feature:

And I fill in "Telephone (Home)" with "61234567"

In webrat steps:

When /^I fill in "([^\"]*)" with "([^\"]*)"$/ do |field, value|
fill_in
(field, :with => value)
end

The error encountered:

Could not find field: "Telephone (Home)" (Webrat::NotFoundError)

At first I thought the regex couldn't parse "(Home)" properly as "(" & ")" are control characters in regex. Turns out it's because the way webrat's fill_in method looks for the field. I had to use the following instead for the nested form field.


And I fill in "enrollment_addresses_attributes_0_telephone_home" with "61234567"

Monday, July 20, 2009

Error using cucumber, selenium and firefox 3

Cucumber and Webrat works great for Behaviour Driven Development (BDD). In certain situations it's better to couple Cucumber with Selenium especially when we need to test javascript or have a look at the test visually.

When I first started Cucumber and Selenium on my Ubuntu. Selenium-server throws up saying that firefox 3 path isn't defined. Thankfully a blog article by Tze Yang presents a solution.However I still get the annoying error every now and then when firefox 3.0 was upgraded. This is because of the firefox executable is a symlink to a shell script.



ls -l /usr/bin/ | grep firefox

lrwxrwxrwx 1 root root 11 2009-06-13 13:32 abrowser -> firefox-3.0
lrwxrwxrwx 1 root root 11 2009-06-13 13:32 abrowser-3.0 -> firefox-3.0
lrwxrwxrwx 1 root root 33 2009-07-21 00:34 firefox -> firefox-3.0
lrwxrwxrwx 1 root root 32 2009-06-13 13:32 firefox-3.0 -> ../lib/firefox-3.0.11/firefox.sh

lrwxrwxrwx 1 root root 31 2009-07-01 00:42 firefox-3.5 -> /home/jasonong/.firefox/firefox


The trick then is to change the firefox symlink to the actual firefox 3 executable.


sudo rm /usr/bin/firefox
sudo ln -s /usr/lib/firefox-3.0.11/firefox-3.0 firefox


That should get you some nice cucumber + selenium action.