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?
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.
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>
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.
精彩评论