Andy Watts
2007-Oct-24 13:56 UTC
[rspec-users] Using Mechanize in Story Step Implementations
Hi, Thought this might be of interest story writers. The mechanize plugin seems to play nice with RSpec. The following mix of methods seems to work just fine. I especially like the helpers for populating forms. agent = WWW::Mechanize.new page = agent.get ''http://www.gmail.com'' page.should have_tag(''form'', :count => 1) form = page.forms.first form.email = ''d at d.com'' page = agent.submit form Could make for some nice step implementations. Almost seems too easy, is there anything that mechanize will break? - Andy -- View this message in context: http://www.nabble.com/Using-Mechanize-in-Story-Step-Implementations-tf4683999.html#a13384581 Sent from the rspec-users mailing list archive at Nabble.com.
James Hughes
2007-Oct-24 20:42 UTC
[rspec-users] Using Mechanize in Story Step Implementations
On 10/24/07, Andy Watts <andywatts at yahoo.com> wrote:> > Hi, > > Thought this might be of interest story writers. > The mechanize plugin seems to play nice with RSpec. > The following mix of methods seems to work just fine. > I especially like the helpers for populating forms. > > agent = WWW::Mechanize.new > page = agent.get ''http://www.gmail.com'' > page.should have_tag(''form'', :count => 1) > form = page.forms.first > form.email = ''d at d.com'' > page = agent.submit form > > Could make for some nice step implementations. > Almost seems too easy, is there anything that mechanize will break?Can''t see why this wouldn''t work, and it might be useful for setting up more complex interactions, but for this simple case why not just do something like: post ''/form_action'', {:email => ''d at d.com''} Cheers, James
David Chelimsky
2007-Oct-24 20:50 UTC
[rspec-users] Using Mechanize in Story Step Implementations
On 10/24/07, James Hughes <hughes.james at gmail.com> wrote:> On 10/24/07, Andy Watts <andywatts at yahoo.com> wrote: > > > > Hi, > > > > Thought this might be of interest story writers. > > The mechanize plugin seems to play nice with RSpec. > > The following mix of methods seems to work just fine. > > I especially like the helpers for populating forms. > > > > agent = WWW::Mechanize.new > > page = agent.get ''http://www.gmail.com'' > > page.should have_tag(''form'', :count => 1) > > form = page.forms.first > > form.email = ''d at d.com'' > > page = agent.submit form > > > > Could make for some nice step implementations. > > Almost seems too easy, is there anything that mechanize will break? > > Can''t see why this wouldn''t work, and it might be useful for setting > up more complex interactions, but for this simple case why not just do > something like: > > post ''/form_action'', {:email => ''d at d.com''}Because even if the form has no email field, as long as the model supports it, your example will pass. Andy''s example specifies that there should be an email field in the form, in addition to specifying that the model should be able to handle it. Cheers, David
Jay Levitt
2007-Oct-25 18:49 UTC
[rspec-users] Using Mechanize in Story Step Implementations
On 10/24/2007 4:42 PM, James Hughes wrote:> On 10/24/07, Andy Watts <andywatts at yahoo.com> wrote: >> Hi, >> >> Thought this might be of interest story writers. >> The mechanize plugin seems to play nice with RSpec. >> The following mix of methods seems to work just fine. >> I especially like the helpers for populating forms. >> >> agent = WWW::Mechanize.new >> page = agent.get ''http://www.gmail.com'' >> page.should have_tag(''form'', :count => 1) >> form = page.forms.first >> form.email = ''d at d.com'' >> page = agent.submit form >> >> Could make for some nice step implementations. >> Almost seems too easy, is there anything that mechanize will break? > > Can''t see why this wouldn''t work, and it might be useful for setting > up more complex interactions, but for this simple case why not just do > something like: > > post ''/form_action'', {:email => ''d at d.com''}Well, one advantage is that the former tests three things: 1. that the get works, 2. that the get and post share the "email" field, and 3. that the post works. I had a recent bug where I actually broke the "get" completely (crashed), and I''m sure we''ve all seen bugs where we change a field name in one case but not the other, so it''s good to test all three. I hadn''t thought of using Mechanize, though - there''s a form_test_helper plugin that does the same thing without the HTTP, and I''ve been meaning to try getting it working on RSpec. Jay Levitt