Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webrat autofilling form fields

I am learning how to write tests with cucumber/webrat. One of my test scenarios is set to test form validation (leaving field(s) empty). Strangely enough, fields that I do not fill-in with fill_in are set to the field's name attribute. This only happens when I run cucumber, when using a browser this does not happen.

The step I'm using is straight forward:

When /^I submit the form$/ do
  # Not filling in the 'Name' field here
  fill_in 'Description', :with => 'This is a description'
  click_button 'Save'
end

After running the scenario that uses the step above, I can see that the text field "Name" is set to "name" instead of being empty. This is also the case if I fill in that field with an empty space or nil:

fill_in 'Name', :with => ''

The form I'm testing on is simple enough:

<form action="/item/create" method="post">
  <div>
    <label for="ItemName">Name</label>
    <input type="text" name="name" id="ItemName" />
  </div>
  <div>
    <label for="ItemDescription">Description</label>
    <textarea name="description" id="ItemDescription"></textarea>
  </div>
  <input type="submit" value="Save" />
</form>

Any idea why this is happening?

like image 976
dmondark Avatar asked Jan 14 '11 09:01

dmondark


1 Answers

I'm guessing you're using Webrat with the Mechanize adapter, is that right? If so, I found myself very frustrated by the same issue. It turns out it is a bug in how Webrat passes form field values to Mechanize. You can find details and a patch here: https://webrat.lighthouseapp.com/projects/10503/tickets/384-webrat-does-not-pass-empty-form-fields-correctly-to-mechanize

Alternatively, if you don't want to use a patched version of Webrat, a slightly non-optimal workaround is to instead fill_in with whitespace (' ') and make sure your app's input validation trims or ignores whitespace when considering whether a field has been filled in correctly.

Unfortunately there seem to be a number of issues like this, which have contributed patches that haven't been merged into the "official" Webrat codebase. I e-mailed the author about a month and a half ago to ask whether he was still maintaining it and, if not, to please consider putting a call out for someone who would, as many people still use it. To date, I haven't yet received a response.

like image 176
Michael Avatar answered Nov 08 '22 00:11

Michael