10

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?

dmondark
  • 1,677
  • 1
  • 12
  • 19
  • +1 For an interesting problem. Can't wait til someone figures this one out. – raidfive Feb 03 '11 at 03:41
  • What happens if you fill in the name, but not description? I'm wondering if you've hit an edge case with the label, attribute, and value all being "name" – Mark Thomas Feb 05 '11 at 01:59
  • @Mark, If I fill in the name but not the description, It would populate the description field with the literal "description" which is the value of the name attrib of that field. – dmondark Feb 22 '11 at 04:59
  • What adapter are you using? Mechanize? – Rimian Sep 20 '11 at 00:36

3 Answers3

2

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.

Michael
  • 548
  • 4
  • 11
  • Miquel, Thanks for your answer and the patch. It fixes the problem (Yep, I am/was using Machanize). I have to mention that we have since switched to Capybarra, which is something most has been doing it seems. Capybarra has been quite stable and the way it handles drivers is pretty neat. I've also heard that Webrat is not being maintained anymore, but I can't confirm that. – dmondark Oct 22 '11 at 15:43
1

One thing you can try is to make sure autocomplete is turned off on that field (autocomplete="off") to see if that affects the outcome.

<form action="/item/create" method="post">
  <div>
    <label for="ItemName">Name</label>
    <input type="text" name="name" id="ItemName" autocomplete="off" />
  </div>
  <div>
    <label for="ItemDescription">Description</label>
    <textarea name="description" id="ItemDescription"></textarea>
  </div>
  <input type="submit" value="Save" />
</form>
Ben
  • 9,725
  • 6
  • 23
  • 28
  • we had to do that because the autocomplete in Firefox was filling in fields in our test. Also if you have a password field it will fill it in with your password in the form-fill cache as well as fill the text field before the password, no matter what the name of that field is, with the username in the form-fill cache – Ben Feb 03 '11 at 21:06
  • Dang, seems like that would be hard to track down :/ Could you launch Firefox in another user profile or incognito to prevent this from happening? – raidfive Feb 04 '11 at 03:14
  • This was in our test suite when it used selenium. Not sure how to tell it to run in a different profile. But it wasn't necessary because the **autocomplete="off"** fixed our issues and it was a field that we didn't want autocompleted anyway. – Ben Feb 04 '11 at 03:43
  • But then I am not using selenium nor (headless) firefox. Webrat is the browser here after all. And it looks like it does not respect autocomplete. I just tried it and it's still populating the field with the value of its name attribute. And even if autocomplete was not turned off, it should not just grab the name and fills in the field with it. – dmondark Feb 04 '11 at 06:08
0

Can you try something like this within the step?

Given %{I fill in "name" with ""}

Or even better, in the feature file use

Given I fill in "name with "".

I would also suggest moving to Capybara, you can do things like this:

https://github.com/jnicklas/capybara/issues/issue/219

Which will let you set up Firefox profiles for your selenium tests.

Tyler K
  • 526
  • 1
  • 4
  • 12
  • Thanks Tyler. I've already tried passing an empty string (and a nil) but it would still fill it up with the field name. I'll certainly look into Capybara, but I am not using firefox/selenium. Only webrat at this point. – dmondark Feb 04 '11 at 05:55