I have a form page with a confirmation field for the email address and the password, both with the label "Confirmation". This fails when running with RSpec, as expected. If I make the labels unique, say by changing the email label to "Email Confirmation", then it will pass. I'd rather keep the labels the same. If I use the email ID generated by form_for, Capybara seems happy but the test fails. I've also tried the name with the same result.
RSpec/Capybara extract:
fill_in "company_contact_email_confirmation", with: "example#example.com"
form_for extract:
<div class="field">
<%= f.label :contact_email_confirmation, "Confirmation" %><br />
<%= f.email_field :contact_email_confirmation %>
</div>
Generated source:
<div class="field">
<label for="company_contact_email_confirmation">Confirmation</label><br>
<input id="company_contact_email_confirmation" name="company[contact_email_confirmation]" size="30" type="email">
</div>
Error message:
1) Company pages signup with valid information should create a company
Failure/Error: expect { click_button submit }.to change(Company, :count).by (1)
count should have been changed by 1, but was changed by 0
If I use Capybara within, as has been suggested elsewhere here, it still does not work:
RSpec/Capybara extract:
within ".email_stuff" do
fill_in "Confirmation", with: "example#example.com"
end
form_for extract:
<div class="email_stuff">
<div class="field">
<%= f.label :contact_email_confirmation, "Confirmation" %><br />
<%= f.email_field :contact_email_confirmation %>
</div>
</div>
Generated source:
<div class="email_stuff">
<div class="field">
<label for="company_contact_email_confirmation">Confirmation</label><br />
<input id="company_contact_email_confirmation" name="company[contact_email_confirmation]" size="30" type="email" />
</div>
</div>
Capybara does not seem to complain but the test fails with the same error. How do I get Capybara to allow me to use the same label twice on the same form?
I added some debugging statements to the ActiveRecord after_validation callback to see the status of the virtual confirmation fields. As shown in my code above, I'd referenced the email confirmation field by name or ID, but not BOTH the password confirmation and email confirmation fields with IDs. Capybara was putting the password data in the wrong confirmation field since the email confirmation field was first on the form. If I'd put the IDs on the password field, it would have worked. IDs on both are safest.
Related
I wrote some tests using capybara for request testing using poltergeist and phantomjs as javascript driver.
The following steps for filling in a login form works great without js:
it "signs in" do
visit new_user_session_path
fill_in "Email", with: user
fill_in "Password", with: password||"foobar"
click_button "Login"
end
If I define my test with js it "signs in", js: true do my test fails with error:
Capybara::ElementNotFound: Unable to find field "Email"
The login form itself is built using simple_form as form generator and bootstrap in frontend.
The fields do not have a label. the search text is only contained in the placeholder attribute.
= simple_form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f|
= f.input :email, :placeholder => User.human_attribute_name(:email), :label => false, :input_html => {:class => 'input-xlarge'}
= f.input :password, :placeholder => User.human_attribute_name(:password), :label => false, :input_html => {:class => 'input-xlarge'}
%div
= f.button :submit, "Login", :class => 'btn btn-large btn-primary'
This code generates the following html code
<form accept-charset="UTF-8" action="/users/login" class="simple_form new_user" id="new_user" method="post" novalidate="novalidate">
<div style="margin:0;padding:0;display:inline">
<input name="utf8" type="hidden" value="✓">
</div>
<div class="control-group email required user_email">
<div class="controls">
<input class="string email required input-xlarge" id="user_email" name="user[email]" placeholder="Email" size="50" type="email" value="">
</div>
</div>
<div class="control-group password required user_password">
<div class="controls">
<input class="password required input-xlarge" id="user_password" name="user[password]" placeholder="Password" size="50" type="password">
</div>
</div>
<div>
<input class="btn btn btn-large btn-primary" name="commit" type="submit" value="Login">
</div>
</form>
Do you have any Idea how to ensure that the fields are found even if js is activated?
I don't know how your Webrat test passed. In my experience Capybara can't find "Email" if there is no matching Label or id.
In your case, since you don't use label, I suggest you to find the field with id
fill_in "user_email", with user.email
# user_email is the id created by simple_form in general case. Verify yours.
# Don't need "#" before id.
Take a screenshot after visit new_user_session_path and verify html is being rendered when :js => true.
visit new_user_session_path
save_and_open_page
If nothing is being rendered, just an empty html document, make sure in your spec_helper.rb
config.use_transactional_fixtures = false
Also try using DatabaseCleaner gem.
RSpec.configure do |config|
config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner.clean_with :truncation
end
config.before(:each) do
if example.metadata[:js]
DatabaseCleaner.strategy = :truncation
else
DatabaseCleaner.strategy = :transaction
end
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
I have a form with checkbox. The form is mapped to a model.
According to rails documentation to prevent a problem when checkbox is unclicked and nothing for this field is sent, rails generate hidden field just before the checkbox with 0 as a value.
That's correct.
What is not correct is that after I click the checkbox and submit the form, I can see in the post request that first is sent the value 1 and then value 0 (from hidden field)
And as a result of this, the checkbox has always value 0.
The documentation is correct when it says that parameters extraction gets the last occurrence of any repeated key in the query string, but the order in the post request is not correct ????
I use rails 3.2.11.
view
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
<%= devise_error_messages! %>
<p><%= f.label :name %><br />
<%= f.text_field :name %></p>
...
<div><%= f.label :send_dayplan_info %><br />
<%= f.check_box :send_dayplan_info %>
<div><%= f.submit "Update" %></div>
<% end %>
controller
...
def update
...
if resource.update_with_password(params[resource_name])
....
end
end
summary
When I debug and evaluate params[resource_name] it contains always value 0 for send_dayplan_info field.
When I click the checkbox, I can see in firebug post request contains values:
user[send_dayplan_info]=1
user[send_dayplan_info]=0
When I unclick the checkbox, I can see in firebug post request contains values:
user[send_dayplan_info]=0
Below there is form data for clicked checkbox
utf8:✓
_method:put
authenticity_token:....
user[name]:....
user[send_dayplan_info]:1
user[send_dayplan_info]:0
And the source code of the generated html is:
...
<div><label for="user_send_dayplan_info">Send dayplan info</label><br />
<input name="user[send_dayplan_info]" type="hidden" value="0" /><input id="user_send_dayplan_info" name="user[send_dayplan_info]" type="checkbox" value="1" />
<div><input name="commit" type="submit" value="Update" /></div>
Rails 3.1.1
Active Admin 0.4.4
formtastic 2.1.1
This is the active admin controller for my Agency model.
ActiveAdmin.register Agency do
form do |f|
f.input :name
f.input :contact_email, :label=>"Email invoices to"
f.input :api_key, :hint=>"Create a key by following these instructions".html_safe
f.actions
end
end
The form should render the three inputs, followed by the submit button, but all I get is:
Just to be clear, the HTML shows no signs of the missing inputs:
<form accept-charset="UTF-8" action="/admin/agencies" class="formtastic agency" id="agency_new" method="post" novalidate="novalidate" name="agency_new">
<div style="margin:0;padding:0;display:inline">
<input name="utf8" type="hidden" value="✓"><input name="authenticity_token" type="hidden" value="...">
</div>
<fieldset class="actions">
<ol>
<li class="action input_action" id="agency_submit_action">
<input name="commit" type="submit" value="Create Agency">
</li>
</ol>
</fieldset>
</form>
I also tried using fieldsets, with the same (faulty) output:
ActiveAdmin.register Agency do
form do |f|
f.inputs "New Agency" do
f.input :name
f.input :contact_email, :label=>"Email invoices to"
f.input :api_key, :hint=>"Create a key by following these instructions".html_safe
end
f.actions
end
end
Update: I've discovered that commenting out the f.actions line gets formtastic to actually print the inputs, but now just without the button.
ActiveAdmin.register Agency do
form do |f|
f.inputs "New Agecny" do
f.input :name
f.input :contact_email, :label=>"Email invoices to"
f.input :api_key, :hint=>"Create a key by following these instructions".html_safe
end
#f.actions
end
end
Form with inputs but no button:
f.buttons should be used, rather than f.actions.
f.buttons, prints the buttons. f.actions expects a block for formatting the buttons, similar to f.inputs.
You are right on track: wrap your inputs in an f.inputs block.
Uncomment the line "f.actions" outside the block.
You should be set.
Try using this: f.input :type => :submit .This worked for me
form do |f|
f.input :starts_at
f.input :ends_at
f.input :type => :submit
end
Try using this: f.buttons :submit
I also think that the block for buttons/actions changed a bit in a version of active admin at some point, so you may be tripped up by old tutorials, etc.
This is so frustrating that after examining all stackoverflow queries on a related subject, having the tests seemingly pass before, I cannot get this simple thing to line up and pass. It cannot find a label "quality" in a form_for (#price).
And I fill in "good" in Quality
cannot fill in, no text field, text area or password field with id, name, or label 'price[quality]' found (Capybara::ElementNotFound)
My feature (abridged)
Scenario: Adding corn price
And I fill in "good" in Quality
My step (abridged)
When /^I fill in "([^"]*)" in Quality$/ do |text|
fill_in('price[quality]', :with => text)
end
My form:
<%= form_for (#price), :url => prices_path do |f| %>
<div class="field">
<%= f.label :quality %><br />
<%= f.text_field :quality %>
</div>
My source:
<div class="field">
<label for="price_quality">Quality</label><br />
<input id="price_quality" name="price[quality]" size="30" type="text" />
</div>
I've tried so many combinations to get it to pass, and I've run out of combinations to try, sam
What about use next:
When /^I fill in "([^"]*)" in Quality$/ do |text|
fill_in('price_quality', :with => text)
end
I have this code in my routes:
controller :active_car do
put 'switch_car' => :update
get 'switch_car' => :edit
end
This is my code in on my edit page.
<% form_tag('switch_car', :method => :put) do%>
<div class="field">
<label for="car_info_id">Car Name:</label>
<%= select("", "car_info_id", #available_cars.collect {|v| [v.name, v.id]})%>
</div>
<div>
<%= submit_tag "Switch Car" %>
</div>
<% end %>
When I click submit I get the following routing error.
No route matches "/switch_car" with the url pointing to http://localhost:3000/switch_car?method=put
The get is working just fine as I end the url with switch_car I get my page to edit. For some reason the put definition is not working.
After changing method to second argument it just doesn't work. It seems to have post as the method still instead of put. Here is generated HTML
<form accept-charset="UTF-8" action="switch_car" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><input name="_method" type="hidden" value="put" />
:method belongs to the "options" hash, which is form_tag's second argument, not the first.
http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-form_tag
Please inspect the html generated by that tag and post it here.