# Learning Cucumber

**URL:** <https://www.ruby-forum.com/t/learning-cucumber/153230>\
**Category:** RSpec\
**Created:** [November 13, 2008, 5:53pm UTC](https://www.ruby-forum.com/t/learning-cucumber/153230 "2008-11-13T17:53:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [November 13, 2008, 5:53pm UTC](https://www.ruby-forum.com/t/learning-cucumber/153230/1 "2008-11-13T17:53:20Z")

</div>

Here is the scenario:

Scenario: Create a new entity row  
Given I am on the new entity page  
When I fill in a textbox labelled “Common Name” with “My New Entity”  
And I fill in a textbox labelled “Full Legal Name:” with “My New  
Enitity is a CORP”  
And I select from a listbox labelled “Legal Form:” the value “CORP”  
And I press “Create”  
Then I should see “Entity was successfully created.”

Now, the suggestions that cucumber makes about missing step definitions  
are very useful, but I am not yet at the point where I understand enough  
of this to proceed. For the moment I need to sort out the relationship  
of  
cucumber with webrat.

In the case above, what I see when I run rake features is this:

Scenario: Create a new entity row # features/manage\_entities.feature:7  
Given I am on the new entity page # features/steps/entity\_steps.rb:1  
When I fill in a textbox labelled   
“Common Name” with “My New Entity” #  
features/steps/entity\_steps.rb:5  
And I fill in a textbox labelled   
“Full Legal Name:” with “My New Enitity is a CORP”   
# features/steps/entity\_steps.rb:8  
And I select from a listbox labelled   
“Legal Form:” the value “CORP” #  
features/steps/entity\_steps.rb:11  
And I press “Create” #  
features/steps/common\_webrat.rb:4  
Then I should see   
“Entity was successfully created.” #  
features/steps/common\_webrat.rb:36

expected: /Entity was successfully created./m,  
got: "\r\n\<!DOCTYPE html PUBLIC  
\

So, evidently I need some other specification to check the validity of  
the  
action with respect to the web page displayed. Can some one give me a  
hint as to what "I should see “X” should actually say? Should this be a  
regex? In other worlds should the outcome be specified as:

Then I should see “_Entity was successfully created_”

Unfortunately, I have many, many more questions, but I will try to feed  
them in slowly.

Regards,

–  
\*\*\* E-Mail is NOT a SECURE channel \*\*\*  
James B. Byrne [mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Harte & Lyne Limited [http://www.harte-lyne.ca](http://www.harte-lyne.ca)  
9 Brockley Drive vox: +1 905 561 1241  
Hamilton, Ontario fax: +1 905 561 0757  
Canada L8E 3C3

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [November 13, 2008, 6:06pm UTC](https://www.ruby-forum.com/t/learning-cucumber/153230/2 "2008-11-13T18:06:15Z")

</div>

On Thu, November 13, 2008 11:51, James B. Byrne wrote:

> So, evidently I need some other specification to check the validity of the  
> action with respect to the web page displayed. Can some one give me a  
> hint as to what "I should see “X” should actually say? Should this be a  
> regex? In other worlds should the outcome be specified as:
> 
> Then I should see “_Entity was successfully created_”

Actually, I have discovered that what I need to do is to create some  
steps  
to move the data passed by the When clauses into the form variables and  
thus get the expected result instead of an error.

–  
\*\*\* E-Mail is NOT a SECURE channel \*\*\*  
James B. Byrne [mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Harte & Lyne Limited [http://www.harte-lyne.ca](http://www.harte-lyne.ca)  
9 Brockley Drive vox: +1 905 561 1241  
Hamilton, Ontario fax: +1 905 561 0757  
Canada L8E 3C3

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [November 14, 2008, 7:55am UTC](https://www.ruby-forum.com/t/learning-cucumber/153230/3 "2008-11-14T07:55:42Z")

</div>

What you’re doing here is writing imperative features. Writing  
declarative features might be a better alternative. Instead of

Given I am on the new entity page  
When I fill in a textbox labelled “Common Name” with “My New Entity”  
And I fill in a textbox labelled “Full Legal Name:” with “My New  
Enitity is a CORP”  
And I select from a listbox labelled “Legal Form:” the value “CORP”  
And I press “Create”  
Then I should see “Entity was successfully created.”

You could consider

Given I am on the new entity page  
When I correctly fill in the new entity form  
Then I should see a confirmation

Then the feature has much less technical stuff in it that

1. Hide the intent
2. Make the story brittle to cosmetic changes, in particular to small  
rewordings of error messages and confirmations

A side effect of this is you need much less regex magic to match your  
expressions.

HTH

Andrew

2008/11/13 James B. Byrne [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [November 14, 2008, 8:04pm UTC](https://www.ruby-forum.com/t/learning-cucumber/153230/4 "2008-11-14T20:04:19Z")

</div>

Andrew P. wrote:

> What you’re doing here is writing imperative features. Writing  
> declarative features might be a better alternative. Instead of  
> …
> 
> Given I am on the new entity page  
> When I correctly fill in the new entity form  
> Then I should see a confirmation
> 
> Then the feature has much less technical stuff in it that
> 
> 1. Hide the intent
> 2. Make the story brittle to cosmetic changes, in particular to small  
> rewordings of error messages and confirmations
> 
> A side effect of this is you need much less regex magic to match your  
> expressions.

This advice does help. Thank you.

Is there a way to mix webrat and hard coded steps? In other words, if I  
have this in my steps file.

Given /I am on the new entity page/ do  
visits “/entities/new”  
end

When /I fill out the form correctly/ do  
fills\_in(“Common Name”, :with =\> “My Common Name”)  
fills\_in(“Legal Name”, :with =\> “My Legal Name”)  
Entity.entity\_legal\_name = “CORP” # does not work  
end

Is this possible?

How do I set the attribute “entity\_legal\_form” to “CORP” for that  
entity? I cannot figure out how to reference that entity instance.

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [November 15, 2008, 2:35am UTC](https://www.ruby-forum.com/t/learning-cucumber/153230/5 "2008-11-15T02:35:39Z")

</div>

Generally you can do whatever you want in step as its a ruby file. The  
thing to do is use a debugger statement to stop in the step (make sure  
there is a line after debugger) and then explore.

In your step the Entity you are referring to is probably a class not a  
variable

HTH

Andrew

2008/11/14 James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
