webrat autofilling form fields
Asked Answered
G

3

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?

Geosynclinal answered 14/1, 2011 at 9:40 Comment(4)
+1 For an interesting problem. Can't wait til someone figures this one out.Lemaceon
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"Grossman
@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.Geosynclinal
What adapter are you using? Mechanize?Pickerelweed
A
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.

Anhydrous answered 4/8, 2011 at 13:46 Comment(1)
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.Geosynclinal
A
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>
Alignment answered 3/2, 2011 at 4:2 Comment(4)
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 cacheAlignment
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?Lemaceon
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.Alignment
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.Geosynclinal
H
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.

Hadley answered 4/2, 2011 at 5:18 Comment(1)
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.Geosynclinal

© 2022 - 2024 — McMap. All rights reserved.