[rspec-users] webrat the way to go?

aslak hellesoy aslak.hellesoy at gmail.com
Mon Sep 29 03:57:15 EDT 2008


On Mon, Sep 29, 2008 at 9:07 AM, Willem van den Ende
<willem at willemvandenende.com> wrote:
> Hi Aslak,
>
> thanks for responding (more below).
>
> On Mon, 2008-09-29 at 08:48 +0200, aslak hellesoy wrote:
>> On Mon, Sep 29, 2008 at 8:46 AM, Willem van den Ende
>> <willem at willemvandenende.com> wrote:
>> > On Sun, 2008-09-28 at 22:24 +0200, Willem van den Ende wrote:
>> > <snip>
>> >> When I go to invitation/new, I see the Subject field, but WebRat doesn't... :
>> > <snip>
>> >>     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.
>> >
>>
>> 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... :) (it is the fields label, maybe the label in the html
> generated by rspec_scaffold should not be <b>Subject</b> 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
> <h1>New invitation</h1>
>
>
>
> <form action="/invitations" class="new_invitation" id="new_invitation"
> method="post">
>  <p>
>    <b>Subject</b><br />
>    <input id="invitation_subject" name="invitation[subject]" size="30"
> type="text" />
>  </p>
>

Try to replace <b>Subject</b> with <label
for="invitation_subject">Subject</label>
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
http://github.com/rails/rails/commit/3dd6027f708a472064e73a50f4afb433f338bf09
Sounds like you're using an older Rails...

Aslak

>  <p>
>    <b>Cc</b><br />
>    <input id="invitation_cc" name="invitation[cc]" size="30"
> type="text" />
>  </p>
>
>  <p>
>    <b>Text</b><br />
>    <textarea cols="40" id="invitation_text" name="invitation[text]"
> rows="20"></textarea>
>  </p>
>
>  <p>
>    <input id="invitation_submit" name="commit" type="submit"
> value="Create" />
>  </p>
> </form>
>
> <a href="/invitations">Back</a>
>    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"'
> <h1>New invitation</h1>
>
>
>
> <form action="/invitations" class="new_invitation" id="new_invitation"
> method="post">
>  <p>
>    <b>Subject</b><br />
>    <input id="invitation_subject" name="invitation[subject]" size="30"
> type="text" />
>  </p>
>
>  <p>
>    <b>Cc</b><br />
>    <input id="invitation_cc" name="invitation[cc]" size="30"
> type="text" />
>  </p>
>
>  <p>
>    <b>Text</b><br />
>    <textarea cols="40" id="invitation_text" name="invitation[text]"
> rows="20"></textarea>
>  </p>
>
>  <p>
>    <input id="invitation_submit" name="commit" type="submit"
> value="Create" />
>  </p>
> </form>
>
> <a href="/invitations">Back</a>
>    And I fill in "CC" with "My invitation cc"
> <h1>New invitation</h1>
>
>
>
> <form action="/invitations" class="new_invitation" id="new_invitation"
> method="post">
>  <p>
>    <b>Subject</b><br />
>    <input id="invitation_subject" name="invitation[subject]" size="30"
> type="text" />
>  </p>
>
>  <p>
>    <b>Cc</b><br />
>    <input id="invitation_cc" name="invitation[cc]" size="30"
> type="text" />
>  </p>
>
>  <p>
>    <b>Text</b><br />
>    <rake aborted!
> Command failed with status (1): [/usr/bin/ruby1.8 -I
> "/home/willem/courses/...]
> /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:974:in `sh'
> /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:987:in `call'
> /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:987:in `sh'
> /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:1084:in `sh'
> /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:1019:in `ruby'
> /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:1084:in `ruby'
> /home/willem/courses/experienceagile/ebay/trunk/excrm/vendor/plugins/cucumber/lib/cucumber/rake/task.rb:47:in `define_tasks'
> /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:621:in `call'
> /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:621:in `execute'
> /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:616:in `each'
> /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:616:in `execute'
> /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:582:in
> `invoke_with_call_chain'
> /usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
> /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:575:in
> `invoke_with_call_chain'
> /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:568:in `invoke'
> /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:2031:in `invoke_task'
> /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:2009:in `top_level'
> /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:2009:in `each'
> /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:2009:in `top_level'
> /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:2048:in
> `standard_exception_handling'
> /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:2003:in `top_level'
> /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:1982:in `run'
> /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:2048:in
> `standard_exception_handling'
> /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:1979:in `run'
> /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/bin/rake:31
> /usr/bin/rake:19:in `load'
> /usr/bin/rake:19
> textarea cols="40" id="invitation_text" name="invitation[text]"
> rows="20"></textarea>
>  </p>
>
>  <p>
>    <input id="invitation_submit" name="commit" type="submit"
> value="Create" />
>  </p>
> </form>
>
> <a href="/invitations">Back</a>
>    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
>>
>> Aslak
>>
>> > 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. :)
>> >
>> > 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
>> >
>> > _______________________________________________
>> > rspec-users mailing list
>> > rspec-users at rubyforge.org
>> > http://rubyforge.org/mailman/listinfo/rspec-users
>> >
>
>


More information about the rspec-users mailing list