Capybara skipping over a click_button - ruby-on-rails-3

I have a test that fills out some fields and then is supposed to click a button. This is all done after loading up a modal window. However, it seems that it just skips over it and doesn't click the button. I have tried debugging it manually and calling it myself and it'll work fine but when I run the test by itself it doesn't click it.
Given /^I login with "(.*?)" and "(.*?)"$/ do |email, password|
within "#signin_fields" do
fill_in("custom_fields_email", :with => email)
fill_in("custom_fields_password", :with => password)
end
click_button("Sign In") if page.should have_selector(".btn-signin")
end
I even added a check to make sure it was on the page but since the removal of wait_until, I'm not sure how I'm supposed to let the page load and then make sure it clicks the button properly. Any ideas would heavily appreciated.

This post may help: How do you get rspec to output what it encountered rather than it "didn't find what it expected"? Basically, for debugging, you can add the line save and open page and inspect what the spec is finding and whether it differs from the results of your own debugging.

The following row from your code is incorrect as have_selector is a RSpec matcher:
click_button("Sign In") if page.should have_selector(".btn-signin")
Instead you should use any of Capybara::Node::Matchers like:
click_button("Sign In") if page.has_selector?(".btn-signin")

page.should have_selector(".btn-signin") isn't going to return true or false and therefor the button will never be clicked. Just do this:
click_button(".btn-signin")
If it should be there then the test will fail if it doesn't appear on the page.

Related

I'm trying to find an invisible field to fill in using Capybara, but nothing I am doing seems to work

I am currently trying to fill in the Google sign in form using Capybara, but I am having the hardest time getting it to find the hidden field to fill in.
Here is the HTML for the field I am trying to fill in.
Here is the test code
require "rails_helper.rb"
RSpec.describe "Sign in page" do
it 'displays the page intention' do
visit('/mars')
expect(page).to have_content 'Sign in with Google'
end
it 'fills in user information' do
find(:xpath, '//*[#id="identifierId"]', visible: false)
end
end
And this is the error I am getting.
First, you shouldn't be attempting to fill in a hidden field, you should be interacting with the visible elements like a user would have to. In this case, it may mean needing to click on another element first to trigger the email input to change to being active. Something like
first('form div[jscontroller]').click
fill_in('identifierId', with: email)
should work for the google login page.
Note: also you should be preferring CSS over XPath for finding elements whenever possible, since it will be faster, easier to read, and can't unintentionally break element scoping (// vs .// in XPath - see https://github.com/teamcapybara/capybara#beware-the-xpath--trap)
Update: Additionally all the it blocks are completely independent, with a browser reset in between, so you need to visit the desired page for each one (if all the it blocks in the describe require visiting the same page you could put the visit in a before block.
Note: this all assumes you are actually using the Selenium driver for this test, as the question tags imply.

Clicking a link using rspec and capybara doesn't load the page

In Rails 3.2 I'm using rspec (2.11.0) and capybara 1.1.2 (and have not installed webrat) and it's doing something strange when I click a link on my web page.
I'm trying to click on link on my home page to go to another page. Below is the basic rspec test.
it "should go to agents page" do
visit 'home'
#puts page.body
#links = page.all('a')
#links.each do |l|
# puts l.text
#end
page.find_by_id('menu_agents').click
puts page.body
#<various content asserts on page currently commented out>
end
I basically go to a page, grab a link and click
The link exists. As you can see from the commented out statements I've confirmed by looking at the page in the console and also looking at the list of links. I've tried clicking not only with the link above, but also using click_link and referencing the id. Looking at both the code and the html of the page when I run the code I see the id of the element.
In all cases I get the following result:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
When running the software with rails s clicking the link does load the correct page. Likewise when I visit the home page in the test it loads correctly as well, since when I print the body to the console I see the right html.
It seems like something is going wrong with the page load when I click the link and the page load has some type of failure (although the test itself does not fail). Any ideas?
Please try this:
Page load will take some time. Use sleep function. It makes some time to wait.
Then use page.find("#menu_agents").click instead of page.find_by_id('menu_agents').click
Verify menu_agents id name is not present any other places in result page.
Note: make sure menu_agents id is unique.
it "should go to agents page" do
visit 'home'
sleep 5
page.find("#menu_agents").click
puts page.body
#<various content asserts on page currently commented out
end

single test failing after changing to capybara-webkit

I have written some RSpec test for my rails 3.2 application and because I was annyoed by the Browser popping up ich tried to change from firefox to capybara-webkit.
After this all tests still run, except one. The line that is failing is:
expect { click_button "Create" }.to change(Answer, :count).by(count)
If I remove the expect and add a method to take a screenshot before and after, I can see that the test is run correctly. But if I step trough with the Debugger the log shows me that the records get created after the second screenshot line. I can wait forever the click_button and corresponding Controller action is run after the line next line is executed.
The "create" button is a standard html button, no JS is involved in the create action. Does sb have a explaination for this strange behaviour?
There is a race condition here between Capybara sending the click action to the server and your test checking the database.
The simplest way to resolve this is to wait before checking:
expect { click_button "Create"; sleep 2 }.to change(Answer, :count).by(count)
I don't like this. A better way to test this would be to check from the end user's perspective.
For example, after clicking 'Create', does the user see the answer on the answers page?
fill_in :title, :with => "My answer"
click_button 'Create'
page.should have_text "My answer"

Rspec Request Spec, fails on form submit

I've got a simple scheduling application. I've got a request spec set up to run through the flow of adding a new workout to the schedule, and the form appears to hang on submit and the script bails after a few seconds. Running through it manually works fine, and my controller and model specs are all passing without complaint.
The only funky thing is I'm using execute_script to interact with the jquery datepicker, which seems to be working okay. Watching the execution shows that no error is happening, it's just hangs waiting for 127.0.0.1 to respond which doesn't seem to happen.
describe "Workouts" do
describe 'Create Workouts', js: true do
it 'Creates a new workout and displays the workout\'s page' do
visit workouts_path
expect {
click_link "New Workout"
fill_in 'workout_name', with: 'Workout!'
fill_in '', with: '05/01/2012'
page.execute_script %Q{ $('#workout_show_date').trigger("focus") }
page.execute_script %Q{ $("a.ui-state-default:contains('15')").trigger("click") }
select('6', from: 'workout_time_4i')
select('15', from: 'workout_time_5i')
click_button 'Create Workout'
}.to change(Workout,:count).by(1)
end
end
end
So after putting a sleep in the spec the error started returning that the database was locked, which google revealed to be a problem related to transactional fixtures being turned on. Turned that off and now all my specs are passing

Rails 3 assert_select undefined method matches?

Trying to get started on view testing in Rails 3. I want to validate that I have a form getting kicked out in the view that has the right URL for the action. So I am using assert_select. I actually got a failing test first, using this syntax in the spec (using Rspec):
response.should assert_select "form[action=#{my_model_path}]"
Looking at the rendered HTML, sure enough, the view was rendering the 'edit' url, not the 'new' url due to the wrong model being passed down. Groovy, start red.
I make the model a 'new' one, and I look at my rendered output, and it's what I'd expect, BUT the test fails, and the error message says:
NoMethodError:
undefined method `matches?' for #<Array:0x0000012a1a5d58>
I've looked all over the web for this, found one guy that mentioned the error, but got no resolution. Any ideas?
Figured out that I could get this to work by nesting my assert_select calls, like so:
response.should assert_select "form" do
assert_select "[action=?]", my_model_path
end