Webrat the way to go?

Hi,

Here is the short version of my question:
For stories, is webrat the way to go? How many of you use webrat? How
many don’t?

Here is the long version:
I have been writing specs for some time now. I have noticed that once
I learned how to write specs (both the syntax and the techniques) it
made my development much faster and more reliable. However, while I
was learning my development was painfully slow. So far it seems to be
the same with stories.

From the recent posts I’ve seen on this mailing list, it looks like I
should focus my efforts on learning cucumber instead of the built-in
story runner. It is mostly compatible with what is already in RSpec,
and it has some good extra features. Do you generally agree with that?

What I am confused about, is whether or not I should learn webrat. It
looks great in theory, and I’ve see it mentioned a few times. But I
haven’t seen a wide-spread move towards it. On top of that, the first
time I looked at it I got rather confused.

The reason I ask (instead of just trying it for myself) is that I’m
the only developer on most of the projects I work on, so I can’t
afford a huge drop in productivity. And if my efforts learning to
write specs are much of a guideline, it will probably take me 3-4
weeks to get comfortable with writing stories.

I know I need to learn this, so I want to focus my efforts where I
will get the most return on my time.

Thanks

PS. I am REALLY looking forward to the RSpec book!

Here’s my take as just a happy user of stories… First, I say
definitely
use WebRat. WebRat is a large part of what convinced me to use stories
in
the first place. I was looking for both something that simulated real
usage, as well as something that wasn’t so fragile/affected by changes
in
implementation (for the latter, this is stories vs. view and controller
specs).
At this point, I have a large set of unit specs, helper specs, and large
number of stories, and then very little controller and view specs. I am
quite sold on this approach. I find writing the stories to be a much
better
way to test my views and controllers, because it’s testing them in the
same
way they’d truly be used by a user, as opposed to what at least feels
like
to me, an insider knowledge way of testing (ya ya, I know, test against
the
API and so on, but go look at most controller tests and such, they just
don’t feel as parallel to reality).

Anyway, all that wouldn’t really be possible without WebRat, IMHO. You
can
also pursue Selenium, but that was just too much for me at this point.
We
are getting into more JavaScript in our app, so I may bring Selenium in
in
the future, but we’ll see.

As for Cucumber, this looks promising, but I haven’t personally tried it
out
yet. Here’s one writeup I saw on it:
http://upstream-berlin.com/2008/08/26/cucumber-next-generation-rspec-story-runner/
I’ve already done all the rake tasks I need, and set it all up to work
quite
nicely with CruiseControl.rb, so some of the advantage is diminished.
Plus,
I haven’t been certain it’s ready for prime time use, or enough of an
advantage to disturb a system that’s already working well, but others
here
can comment better on Cucumber…

On Tue, Sep 9, 2008 at 6:42 AM, DyingToLearn [email protected] wrote:

Hi,

Here is the short version of my question:
For stories, is webrat the way to go? How many of you use webrat? How
many don’t?

I use it and love it. When it doesn’t do what I need (rarely) it’s
trivial to drop down an abstraction level and do request/response
stuff. Webrat doesn’t replace the standard Rails integration API - it
just adds a higher level abstraction level on top. So there is no risk
involved.

and it has some good extra features. Do you generally agree with that?

I would say I agree, but I am obviously biased.

If you already know RSpec I think you’d pick it up faster. Cucumber
has some documentation that might be helpful:

Hopefully the generators should be helpful for newbies.

Cheers,
Aslak

Thank you all for your responses. This has been really helpful!

Matt W. wrote:

I would expect to waste some time splashing around though, yup.

I think I am going to have to book a “day off” next week and just dive
into it all: lots of reading and lots of practice.

Thanks again,
Paul

On 9 Sep 2008, at 05:42, DyingToLearn wrote:

was learning my development was painfully slow. So far it seems to be
the same with stories.

From the recent posts I’ve seen on this mailing list, it looks like I
should focus my efforts on learning cucumber instead of the built-in
story runner. It is mostly compatible with what is already in RSpec,
and it has some good extra features. Do you generally agree with that?

+1

What I am confused about, is whether or not I should learn webrat. It
looks great in theory, and I’ve see it mentioned a few times. But I
haven’t seen a wide-spread move towards it. On top of that, the first
time I looked at it I got rather confused.

Yeah it’s definitely the way to go to give you a nice abstraction
layer between your steps code and the app itself, assuming you’re on
rails. Not tried any of the other adapters (selenium etc) yet.

The reason I ask (instead of just trying it for myself) is that I’m
the only developer on most of the projects I work on, so I can’t
afford a huge drop in productivity. And if my efforts learning to
write specs are much of a guideline, it will probably take me 3-4
weeks to get comfortable with writing stories.

Make sure you read through the other threads on this list about
declarative vs imperative stores etc - there’s a lot of learning been
done even in the short amount of time this stuff has been used.

I would expect to waste some time splashing around though, yup.

I know I need to learn this, so I want to focus my efforts where I
will get the most return on my time.

Thanks

PS. I am REALLY looking forward to the RSpec book!


rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users

cheers,
Matt

http://blog.mattwynne.net

In case you wondered: The opinions expressed in this email are my own
and do not necessarily reflect the views of any former, current or
future employers of mine.

I’d like to know: in a nutshell what’s the difference between Webrat and
Cucumber?

I saw that in RSpec 1.1.5, Story Runner will be replaced by Cucumber, so
isn’t learning Cucumber compulsory?

On Wed, Sep 24, 2008 at 2:43 PM, Fernando P. [email protected]
wrote:

I’d like to know: in a nutshell what’s the difference between Webrat and
Cucumber?

I saw that in RSpec 1.1.5, Story Runner will be replaced by Cucumber, so
isn’t learning Cucumber compulsory?

No you didn’t. You saw that AFTER RSpec 1.1.5, Story Runner will be
replaced by Cucumber. Please be careful when you spread this sort of
information.

As for the different libraries, you can use Webrat in the context of
Story Runner, Cucumber or Rails integration tests. Read more about
each here:

Cheers,
David

They’re separate - Cucumber parallels RSpec Story Runner and replaces
that,
not WebRat.

On Wed, Sep 24, 2008 at 9:43 PM, Fernando P. [email protected]
wrote:

I’d like to know: in a nutshell what’s the difference between Webrat and
Cucumber?

They do completely different things.

Webrat is a library that allows you to interact with a Rails web app
using a high level API similar to Watir and Selenium, but without the
overhead of HTTP and a browser. In essence: go to this URL, fill in
that field, click that button, follow that link. That sort of thing.
It’s a library with a fairly small set of methods that you can call.

Cucumber has nothing to do with Rails, webapps or Webrat for that
matter (although you can use them together). Cucumber lets you write
high level narratives of interactions between a person and a computer
system in plain text, without knowledge of Ruby. Think of it as
executable documentation/requirements. It also provides ruby
programmers with “glue” that sits between these plain text scenarios
and your app. In this “glue” (steps we call them) you can use whatever
ruby libraries you want. For example Webrat. Or something else.

It just happens that Cucumber+Webrat is a real nice blend if you
happen to be writing a Rails app (if you’re writing a ruby app that is
not Rails, Cucumber would still work well, but Webrat wouldn’t). Kind
of like ham and eggs. But you don’t have to have egs with ham or
vice versa. Please - no chunky bacon jokes!

Aslak

Hi,

I just started playing with cucumber
On Wed, 2008-09-17 at 22:44 +0200, aslak hellesoy wrote:

If you already know RSpec I think you’d pick it up faster. Cucumber
has some documentation that might be helpful:
GitHub: Let’s build from here · GitHub

Definitely helpful - after following the steps here It runs (mostly, see
below).

Hopefully the generators should be helpful for newbies.

They clearly are to me. I created a new feature (for a new model
"Invitation) and a rspec_scaffold as indicated on the page.
With the generated Feature it is easy to see the differences with Rspec
story runner and the way webrat should work.
It took me a bit to figure out that the generated code assumes the Model
has a :name field (my Invitation has a Subject that mostly identifies
it).
After fixing that, I get to 10 steps passed, 1 failed (and 6 skipped).
Now I am stuck, because I do not understand the failure webrat seems to
give-
When I go to invitation/new, I see the Subject field, but WebRat
doesn’t… :

Feature: Manage invitations
In order to keep track of invitations
A invitation mechanic
Should be able to manage several invitations

Scenario: Register new invitation
Given I am on the new invitation page
When I fill in “Subject” with “My invitation subject”
Could not find [Webrat::TextField, Webrat::TextareaField,
Webrat::PasswordField]: “Subject” (RuntimeError)
/home/willem/courses/experienceagile/ebay/trunk/excrm/vendor/plugins/webrat/lib/webrat/core/flunk.rb:4:in
flunk' /home/willem/courses/experienceagile/ebay/trunk/excrm/vendor/plugins/webrat/lib/webrat/core/scope.rb:222:in find_field’
/home/willem/courses/experienceagile/ebay/trunk/excrm/vendor/plugins/webrat/lib/webrat/core/scope.rb:25:in
fills_in' /home/willem/courses/experienceagile/ebay/trunk/excrm/vendor/plugins/webrat/lib/webrat/rails/session.rb:24:in send’
/home/willem/courses/experienceagile/ebay/trunk/excrm/vendor/plugins/webrat/lib/webrat/rails/session.rb:24:in
method_missing' /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/integration.rb:448:in send!’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/integration.rb:448:in
method_missing' ./features/steps/common_webrat.rb:13:in When /^I fill in “(.)"
with "(.
)”$/’
features/manage_invitations.feature:8:in `When I fill in “Subject”
with “My invitation subject”’

I also installed the gems as indicated. “Subject” is a TextField, and I
can see it… I would greatly appreciate any help…

Kind regards,

Willem

Willem van den Ende
Living Software B.V.
www.livingsoftware.nl | www.experienceagile.eu | blog: me.andering.com
+31 6 413 06 965 | Spilmanstraat 25 | 5645 JE Eindhoven

On Mon, Sep 29, 2008 at 8:46 AM, Willem van den Ende
[email protected] wrote:

When I fill in “Subject” with “My invitation subject”

instead of

When I fill in “invitation_subject” with “My invitation subject”

which is what I saw in ‘view source’. After changing all generated
whens, I have 17 steps passed.

In the definition of “When I fill in…” you can add this debug
statement on the first line:

puts response.body

That should help you find the reason. Please post the HTML you see for
the failing step.
Also check log/test.log.

Aslak

On Sun, 2008-09-28 at 22:24 +0200, Willem van den Ende wrote:

When I go to invitation/new, I see the Subject field, but WebRat doesn’t… :

When I fill in “Subject” with “My invitation subject”
Could not find [Webrat::TextField, Webrat::TextareaField, Webrat::PasswordField]: “Subject” (RuntimeError)

Ah. Figured it out. The cucumber code generator created (in
manage_invitations.feature):

When I fill in “Subject” with “My invitation subject”

instead of

When I fill in “invitation_subject” with “My invitation subject”

which is what I saw in ‘view source’. After changing all generated
whens, I have 17 steps passed.

It might indicate, as Aslak says in the readme, it is recommended to
create your features by hand. On the other hand, it would also be
convenient if the combination of generate feature and generate
rspec_scaffold works out of the box. :slight_smile:

I’m going to play a bit more with it - too early to send in my own fix
for the feature generator I guess. I am willing to make it if you want
it though.

Willem

Hi Aslak,

thanks for responding (more below).

On Mon, 2008-09-29 at 08:48 +0200, aslak hellesoy wrote:

manage_invitations.feature):

In the definition of “When I fill in…” you can add this debug
statement on the first line:

puts response.body

Ok.

That should help you find the reason. Please post the HTML you see for
the failing step.

Done, see below. It seems WebRat goes looking for the id
(invitation_subject) and can’t find it if you write
When I fill in “Subject” with “My invitation subject”,
but does find
When I fill in “invitation_subject” with “My invitation subject”,

The way it is generated communicates much better with the customer of
course… :slight_smile: (it is the fields label, maybe the label in the html
generated by rspec_scaffold should not be Subject but something
else? ).

trace with HTML:
Feature: Manage invitations
In order to keep track of invitations
A invitation mechanic
Should be able to manage several invitations

Scenario: Register new invitation
Given I am on the new invitation page

New invitation

Subject

Cc

Text

Back
When I fill in “Subject” with “My invitation subject”
Could not find [Webrat::TextField, Webrat::TextareaField,
Webrat::PasswordField]: “Subject” (RuntimeError)
/home/willem/courses/experienceagile/ebay/trunk/excrm/vendor/plugins/webrat/lib/webrat/core/flunk.rb:4:in
flunk' /home/willem/courses/experienceagile/ebay/trunk/excrm/vendor/plugins/webrat/lib/webrat/core/scope.rb:222:in find_field’
/home/willem/courses/experienceagile/ebay/trunk/excrm/vendor/plugins/webrat/lib/webrat/core/scope.rb:25:in
fills_in' /home/willem/courses/experienceagile/ebay/trunk/excrm/vendor/plugins/webrat/lib/webrat/rails/session.rb:24:in send’
/home/willem/courses/experienceagile/ebay/trunk/excrm/vendor/plugins/webrat/lib/webrat/rails/session.rb:24:in
method_missing' /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/integration.rb:448:in send!’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/integration.rb:448:in
method_missing' ./features/steps/common_webrat.rb:14:in When /^I fill in “(.)"
with "(.
)”$/’
features/manage_invitations.feature:8:in `When I fill in “Subject”
with “My invitation subject”’

New invitation

Subject

Cc

Text

Back
And I fill in “CC” with “My invitation cc”

New invitation

Subject

Cc

Text

Back
And I fill in “Text” with “My invitation text”
And I press “Create”
Then I should see “My invitation subject”
And I should see “My invitation cc”
And I should see “My invitation text”

Scenario: Delete invitation
Given there are 4 invitations
When I delete the first invitation
Then there should be 3 invitations left

|initial|after|
|100|99|
|1|0|

10 steps passed
1 steps failed
6 steps skipped

Also check log/test.log.
(apologies for the ESC characters, less doesn’t seem to recognize the
colours…)

Processing InvitationsController#new (for 127.0.0.1 at 2008-09-29
08:55:04) [GET]
Session ID: 56bcf61f1eb75b3e8c973a21bca504d0
Parameters: {“action”=>“new”, “controller”=>“invitations”}
ESC[4;35;1mSQL (0.004048)ESC[0m ESC[0m SELECT a.attname,
format_type(a.atttypid, a.atttypmod), d.
adsrc, a.attnotnull
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = ‘invitations’::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
ESC[0m
Rendering invitations/new
Completed in 0.01889 (52 reqs/sec) | Rendering: 0.00598 (31%) | DB:
0.00550 (29%) | 200 OK [http://ww
w.example.com/invitations/new]
REQUESTING PAGE: POST /invitations with {“commit”=>“Create”,
“invitation”=>{“cc”=>“”, “text”=>“”, “su
bject”=>“”}}

Processing InvitationsController#create (for 127.0.0.1 at 2008-09-29
08:55:04) [POST]
Session ID:
BAh7BiIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNo%
0ASGFzaHsABjoKQHVzZWR7AA%3
D%3D–4a442a5c5f1558d51188e7e418677dedf9dd2717
Parameters: {“commit”=>“Create”, “action”=>“create”,
“controller”=>“invitations”, “invitation”=>{“c
c”=>“”, “text”=>“”, “subject”=>“”}}
ESC[4;36;1mPK and serial sequence (0.005058)ESC[0m ESC[0;1m SELECT
attr.attname, seq.relname
FROM pg_class seq,
pg_attribute attr,
pg_depend dep,
pg_namespace name,
pg_constraint cons
WHERE seq.oid = dep.objid
AND seq.relkind = ‘S’
AND attr.attrelid = dep.refobjid
AND attr.attnum = dep.refobjsubid
AND attr.attrelid = cons.conrelid
AND attr.attnum = cons.conkey[1]
AND cons.contype = ‘p’
AND dep.refobjid = ‘invitations’::regclass
ESC[0m
ESC[4;35;1mInvitation Create (0.001065)ESC[0m ESC[0mINSERT INTO
invitations (“cc”, “updated_at”,
“text”, “subject”, “created_at”) VALUES(E’‘, ‘2008-09-29
08:55:04.858034’, E’‘, E’', ‘2008-09-29 08:55:04.858034’)ESC[0m
ESC[4;36;1mSQL (0.000500)ESC[0m ESC[0;1mSELECT
currval(‘invitations_id_seq’)ESC[0m
Redirected to http://www.example.com/invitations/1
Completed in 0.01049 (95 reqs/sec) | DB: 0.00662 (63%) | 302 Found
[http://www.example.com/invitations]

Willem

On Mon, 2008-09-29 at 09:57 +0200, aslak hellesoy wrote:

Try to replace Subject with Subject
Now both vision impaired people and Webrat know that the text
“Subject” is linked to the input field. This is much better semantic
HTML.

Ah, thanks. I’ll do that. I had seen the labels in one of my other rails
projects. This also makes the Feature a lot more readable.

Rails generates this as of
Update scaffold to use labels instead of bold tags. Closes #10757 [z… · rails/rails@3dd6027 · GitHub
Sounds like you’re using an older Rails…

yes… 2.0.2. Normally I keep my rails apps up to date, but for this
one I’ve use ActiveScaffold quite a bit, and it was broken with 2.1…

ActiveScaffold looks pretty inactive, so I’m going to add specs for
everything, do the UI manually and then I can let ActiveScaffold go
after a while…

(It also turned out that the work it saved me in the beginning -
spec/test and code Models, declare controllers and views, so it can go
without speccing, bit me later… because of the ‘clever bastard code’
in active scaffold with lots of reflection, stacktraces are pretty
incomprehensible - e.g. when I forget to spec something in the model and
there is a nullpointer somewhere the same stacktrace appears for every
model).

Thanks again,

Willem

On Mon, Sep 29, 2008 at 9:07 AM, Willem van den Ende
[email protected] wrote:

When I fill in "invitation_subject" with "My invitation subject"

Ok.
The way it is generated communicates much better with the customer of
Scenario: Register new invitation
type=“text” />

Try to replace Subject with Subject
Now both vision impaired people and Webrat know that the text
“Subject” is linked to the input field. This is much better semantic
HTML.

Rails generates this as of

Sounds like you’re using an older Rails…

Aslak