I'm trying to test cloudinary direct uploading through capybara but capybara is saying that it cannot find the image upload field. How do you test direct uploading?
picture_spec.rb
require 'spec_helper'
feature "User Pictures" do
let(:user) { create(:user) }
describe "Uploading an image" do
describe "for profile photo" do
it "raises an error when clicked outside cropping image so image is not cropped" do
visit new_user_picture_path(user)
attach_file('#photo upload', "#{Rails.root}/spec/support/images/PRATIQUE_BIG.png")
click_on "Valider Photo"
expect(page).to have_content "SVP crop votre image et celle-ci doit mesurer au moins 100 x 100px"
end
end
end
end
new picture page
= form_for(#photo, :url => url, role: "form") do |f|
.form_line.form-group
.upload_button_holder
= link_to "Parcourir", "#", class: "btn btn-default upload_button form-control"
#photo-upload= f.cl_image_upload :image, class: "form-control"
%span.status
.form-group
.form_control
.preview
= f.hidden_field :crop_x
= f.hidden_field :crop_y
= f.hidden_field :crop_w
= f.hidden_field :crop_h
.form-group
= f.submit "Valider Photo", class: "btn btn-lg btn-success"
= hidden_field_tag :direct, params[:direct]
%p= link_to "Retour à Mon Profil", profile_path(current_user.user_code)
// Configure Cloudinary jQuery plugin
= cloudinary_js_config
Capybara then gives back Unable to find file field "photo-upload"
The name of the field in case of cl_image_upload is always file. photo-update is the id of the surrounding div. You can assign an id to the file input field by passing html: {id: "my-id"} to cl_image_upload. BTW. class also belongs inside the html hash parameter.
Related
Having this form:
<h3 class="contact-header"><span>Ready to try it</span> GET IN TOUCH</h3>
<%= form_for #contact, url: pages_path, remote: true do |f| %>
<div class= "flex-row">
<div class="form-input-spacing">
<%= f.label :name %>
<%= f.text_field :name, required: true, class: "contact-form-text-area" %>
</div>
<div class="form-input-spacing">
<%= f.label :email %>
<%= f.text_field :email, required: true, class: "contact-form-text-area" %>
</div>
</div>
<div class="area-input">
<%= f.label :message %>
<%= f.text_area :message, rows: 8, cols: 59, required: true, class: "contact-form-text-area",
placeholder: "Send us a message"%>
</div>
<div class="submit-form">
<%= f.submit 'Send Message', class: 'btn btn-primary' %>
</div>
<% end %>
I am trying to test how it would be filled and then sent with Capybara:
scenario "visitor/user sends incomplete contact form" do
visit root_path #form is in root
within(".new_contact") do
# fill_in "#contact_name", with: "Test User"
# fill_in "contact_name", with: "Test User"
find("#contact_name").set("Test User")
end
save_and_open_page
end
I have tried js: true in the scenario, and both fill_in and find but when I do save_and_open_page nothing is filled.
The reason why I used .new_contact is because in the Inspector, thats the class that the form takes, the #contact_nameis the id that the input takes andcontact_nameis the labelfor`.
If I use click_button "Send Message" the button is clicked and a message appears so why is it getting the button but not the input? Thanks!
save_and_open_page saves the HTML with the element attributes, not the element's properties. It is really only useful for when you want to see modified page structure (the HTML structure has been changed). When you change the content of a field it changes the value property of that element but doesn't actually update the page HTML (value attribute), so when you save the HTML it won't have the value set. If you want to see the page as it was use save_and_open_screenshot, or just pause your test and look at the browser.
Also fill_in takes a locator, not CSS, so it would be
fill_in "contact_name", with: "Test User"
from https://rubydoc.info/gems/capybara/Capybara/Node/Actions#fill_in-instance_method - The field can be found via its name, id, test_id attribute, placeholder, or label text. - not CSS
The following Capybara/RSpec test is failing on my Rails app and I can't figure out why. I'm using the simple_form_for Gem to create the form and submit button. The update method seems to be working properly, as when I change
expect(#coin.currency_name).to eq('Updated Name')
to
expect(page).to have_text('Updated Name')
the test passes and the updated name is shown on the new page. However #coin.currency_name doesn't seem to be updated when I use the previously described expect method. When I manually update the Coin model (on the page, not using RSpec) it works fine and the currency_name is updated.
What am I doing wrong on this test?
spec/features/coins/coin_spec
require 'rails_helper'
RSpec.feature 'Coins' do
before(:each) do
#user = FactoryBot.create(:user)
end
context 'update coin' do
scenario 'should succesfully edit name if user=admin' do
#user.update(admin: true)
login_as(#user, :scope => :user)
#coin = Coin.create!(currency_name: "TestName", user_id: #user.id)
visit edit_coin_path(#coin)
fill_in 'Currency Name', with: 'Updated Name'
click_button 'Submit'
expect(#coin.currency_name).to eq('Updated Name')
end
end
end
app/views/coins/edit.html.erb
<div class='form-container'>
<%= simple_form_for #coin, url: coin_path do |f| %>
<h2>Edit Coin</h2>
<div class="form-container__section">
<%= f.input :currency_name, label: "Currency Name", class: 'form-control' %>
<%= f.input :link_name, placeholder: "Link Name", label: false, class: 'form-control' %>
...
<%= f.button :submit, value: "Submit", class: "btn primary-small", style: "margin-top: 20px;" %>
<% end %>
</div>
and the HTML
<div class="form-container">
...
<h2>Edit Coin</h2>
<div class="form-container__section">
<div class="form-group string required coin_currency_name"><label class="control-label string required" for="coin_currency_name"><abbr title="required">*</abbr> Currency Name</label><input class="form-control string required" type="text" value="OldName" name="coin[currency_name]" id="coin_currency_name"></div>
...
<input type="submit" name="commit" value="Submit" class="btn btn-default primary-small" style="margin-top: 20px;" data-disable-with="Update Coin">
</form>
After change of model, use reload method:
click_button 'Submit'
#coin.reload
expect(#coin.currency_name).to eq('Updated Name')
I tried setting the id in various parts of the form, as well as wrapping the form in a div with id set to search. The result is always the same.
spec/features/02_post_spec.rb
scenario 'search for post title' do
fill_post_form(post3)
fill_in "search", with: post3.title
click_button("Search")
expect(page).to_not have_content(post1.title)
expect(page).to_not have_content(post2.title)
expect(page).to have_content(post3.title)
end
spec/spec_helper.rb
def fill_post_form(post)
visit new_post_path
fill_in 'Title', with: post.title
fill_in 'Body', with: post.body
click_button 'Create Post'
end
This will redirect_to post_path(post)
views/posts/index.html.erb
<%= form_tag(posts_path, method: :get, class: "block") do %>
<div class="form-group">
<div class="input-group">
<%= text_field_tag :search, params[:search], class: "form-control", id: "search" %>
<span class="input-group-btn">
<%= submit_tag "Search", name: nil, class: "btn btn-default" %>
</span>
</div>
</div>
<% end %>
Capybara output
Failure/Error: fill_in "search", with: post3.title
Capybara::ElementNotFound:
Unable to find field "search"
The command fill_in "search", with: post3.title you're using should work, assuming you are on the page rendering the shown partial, and the output of that partial is actually visible on the page (not hidden via CSS). Your scenario doesn't show visiting an actual page, and you don't show what fill_post_form is doing so it's tough to know exactly what is going wrong. First step would be to do something like
fill_post_form(post3) # already in your tests
sleep 2 # wait a few seconds to make sure above method has completed whatever actions it does
puts page.html # output the current page and take a look to see if your 'search' element is actually on the page
I have multiple email fields in a form. None of them have an id on them intentionally. How can I differentiate each element in the test if they don't have an id? I'll show code for clarity.
TEST
it "sends an invitation to multiple users" do
click_on "Invite"
click_link "Invite Another Team Member"
fill_in "", with: "user#example.com"
fill_in "", with: "user2#example.com"
expect { click_button "Invite" }
.to change(ActionMailer::Base.deliveries, :count).by(1)
.and change(Invitation, :count).by(2)
expect(current_path).to eq(account_users_path)
expect(page).to have_content("user#example.com")
expect(page).to have_content("user2#example.com")
end
This actually creates one object, the bottom field. How can I give the text fields individuality?
FORM
<%= form_tag account_invitations_url do %>
<label class="email-label">
<%= email_field_tag "emails[]", "", placeholder: "Email Address", data: { invitation_modal_email: "" }, id: "" %>
</label>
<%= link_to '✚ Invite Another Team Member', "#email", data: { invitation_modal_add: "" } %>
<div class="form-actions invitation">
<span class="button-text"><%= link_to 'Cancel', account_users_path %></span>
<%= submit_tag "Invite", class: "button button--invitation" %>
</div>
<% end %>
## Browser HTML
I think you can use Capybara's method all for finding all elements with same name:
emails = ["user#example.com", "user2#example.com"]
all('input[name="emails[]"]').each_with_index do |field, i|
field.set(emails[i])
end
Or:
all('input[name="emails[]"]')[0].set("user#example.com")
all('input[name="emails[]"]')[1].set("user2#example.com")
Trying to test some functionality in a simple page and I am getting this error when using page.check:
Failures:
1) searching for stores serving a taco and a sauce: Adobada tacos with Chile de Arbol sauce
Failure/Error: page.check 'Adobada'
Capybara::ElementNotFound:
Unable to find checkbox "Adobada"
# ./spec/views/store_search_spec.rb:8:in `block (2 levels) in <top (required)>'
Here is my HTML:
<%= simple_form_for :stores, url: stores_path, method: :get do |f| %>
<%= f.label :tacos, 'Select Tacos to Find: ', class: 'label' %>
<div class='taco-select checkboxes'>
<%= f.collection_check_boxes :tacos, Taco.order(:name), :id, :name,
:include_hidden => false, :item_wrapper_class => 'checkbox_container' %>
</div>
<%= f.label :salsa, 'Select Sauces to Find: ', class: 'label' %>
<div class='salsa-select checkboxes'>
<%= f.collection_check_boxes :salsas, Salsa.order(:name), :id, :name,
:include_hidden => false, :item_wrapper_class => 'checkbox_container' %>
</div>
<%= f.submit 'Search!' %>
<% end %>
And this is my test:
require 'rails_helper'
feature 'searching for stores', %(serving a taco and a sauce:) do
scenario 'Adobada tacos with Chile de Arbol sauce' do
visit root_path
page.check 'Adobada'
page.check 'Chile de Arbol'
click_button 'Search!'
expect(page).to have_content "Store"
expect(page).to have_content 'City'
end
end
I would like to test that when some checkboxes are set to true a certain content is rendered in the page.
Don't know how to fix this.
Try with these, they should work for you:
find(:css, "#YOUR_CHECKBOX_ID").set(true)
or
page.check('YOUR_CHECKBOX_ID')
or simply
find('THE CSS SELECTOR OF YOUR CHECKBOX').click
find(:xpath, 'THE XPATH OF YOUR CHECKBOX').click
You don't have to use an ID to check a checkbox. You can just use the label on the checkbox. Say you have a checkbox like this
<label for="...">
<input type="checkbox" name="...">Something</label>
You 'check' it in capybara like so
it "" do
page.check 'Something'
end
Official docs on this can be found here