Rspec/Capybara not finding inputs - ruby-on-rails

Capybara is acting really strange.
It wasn't finding any input fields at all, ID, name or label. So I changed the test to just check what was rendering and get a huge sass error.
feature "new users are not admin" do
scenario "user signs up" do
visit new_user_registration_path
#fill_in "Username", :with => "newuser"
#fill_in "Email", with: "newuser#example.com"
#fill_in "Password", with: "newuserpassword"
#fill_in "Password Confirmation", with: "newuserpassword"
#click_button "Sign up"
expect(page).to have_content("Sign Up")
end
end
And then this:
Failure/Error: expect(page).to have_content("Sign Up")
expected to find text "Sign Up" in "Sass::SyntaxError at /users/sign_up =================================== > Invalid CSS after \"\": expected selector, was \"===============...\" (in app/assets/stylesheets/application.css) app/views/layouts/application.html.erb, line 5 ---------------------------------------------- ``` ruby 1 2 3 4 > 5 true %> 6 true %> 7 8 9 10 ```
Which is actually a lot more, and seems to be the contents of the error page rails produces (better_errors), but only in tests, not when visiting the actual application. This is also only when the fill_in/click_button is commented out, otherwise it will be "could not find field 'username' etc.
The sass error has "============" in it which is part of how I split my css in sections with a large comment block e.g.
/*=============
Styles
==============*/
But this is all commented out. I don't see what it would cause a problem.
I have no idea what's going on. Any help is appreciated.

Fixed this, it was, for some reason, an un-commented section in bootstrap.css (that for some reason doesn't render an error except via tests).
Works as intended now

Related

Rails + Capybara + Braintree—how to feature test BT's Hosted Fields?

TL;DR—How do I access fields within Braintree's Hosted Fields' iframes?
I want to test a UX flow of paying a donation through Braintree. This is my code so far:
require "rails_helper"
RSpec.feature "Donation Module", type: :feature do
scenario "Public visitor creates a new donation" do
#load page
website = create(:website)
Capybara.current_session.driver.header 'Referer', website.website
visit "/donate?t=#{website.public_token}&frame=1"
#verify page loaded
expect(page).not_to have_content("Are you sure you're installing this on the correct website?")
#fill page 1
find("input[value='20']").click
#go to page 2
find("#credit-details").click
#verify page 2 content is loaded
expect(find(".total-cost-text")).to be_visible
#fill page 2
fill_in 'First Name', with: 'Leeroy'
fill_in 'Last Name', with: 'Jenkins'
fill_in 'Email for receipt', with: 'new_donor#email.com'
within_frame('#braintree-hosted-field-number') do
fill_in '#credit-card-number', with: '4111-1111-1111-1111'
end
within_frame('#braintree-hosted-field-expirationDate') do
fill_in '#expiration', with: '09/19'
end
within_frame('#braintree-hosted-field-cvv') do
fill_in '#cvv', with: '123'
end
find('Make payment').click
# expect to make a new user, new donation, new receipt, email receipt
end
end
Currently, it's breaking at the first within_frame saying Capybara::NotSupportedByDriverError:
Capybara::Driver::Base#within_frame
How do I access fields inside BT's iframes?
Well, I am writing here not exactly an answer to this question, but rather corrections to the question, as I was in the similar situation and was facing similar errors such as Selenium::WebDriver::Error::NoSuchFrameError: Unable to locate frame: #braintree-hosted-field-number and Test::Unit::Capybara::ElementNotFound: Unable to find field "#credit-card-number".
The within_frame should have the following format (the #-sign for ID should be removed from both):
within_frame('braintree-hosted-field-number') do
fill_in 'credit-card-number', :with => number
end
And in order to use the selenium driver in Test::Unit, I used the following helper:
def js
Capybara.current_driver = Capybara.javascript_driver
yield
Capybara.current_driver = Capybara.use_default_driver
end
And then wrapped my tests in it:
class SomeTest < ActionDispatch::IntegrationTest
test "should ..." do
js do
within_frame('braintree-hosted-field-number') do
fill_in 'credit-card-number', :with => number
end
# ...
end
end
Hopefully, someone will find it useful while using Unit Tests.
Seems like you're using the rack_test driver? That doesn't support JS or frames so braintree isn't going to work with that. You need to switch to one of the real browser drivers like selenium, capybara-webkit, or poltergeist.

Capybara/Rspec not catching ActionController::UnpermittedParameters error

I was in the processing in adding a field to an ActiveRecord model and the corresponding view. Before I started, I had a full green test run. I started with writing the following test:
scenario "with all correct info" do
create_vendor "vendor.email#example.com", "vendor_passowrd"
create_consumer "consumer.email#example.com", "password"
sign_in_with "consumer.email#example.com", "password"
click_link "New Order"
select 'vendor.email#example.com', from: "Vendor"
fill_in "Order text", with: "my normal order"
click_button "Submit"
expect(page).to have_content "my normal order"
end
I added the field to the model using the proper rails migration and the route entry.
I would have expected the test to fail with a ActionController::UnpermittedParameters because I have not yet updated the controller. However, this is the failure I am getting:
"Failure/Error: expect(page).to have_content "my normal order"
When I test in the browser, I do get the ActionController::UnpermittedParameters, but not in the automated test.
My question is should the test get the ActionController::UnpermittedParameters error?
Capybara simulates a web browser request, so it will get back an error page from Rails. The UnpermittedParameters error will not propagate up to the spec code itself.
This sounds like it's working correctly.

Capybara/Poltergeist causing unexpected results in request spec in Rails 3.2 app

Is there an alternative I can use to Capybara's save_and_open_page? I have written a request spec that test the user signup form. Upon successful signup, which this test should yield, this test should be passing, but it's not.
Yet when I do save_and_open_page at the end, all the form fields are blank. I can mirror this in the development environment doing the data entry myself and everything passes, so I am struggling to understand what's wrong. I can't afford to let this go because obviously this is a critical test.
I've only been doing TDD for a little while, so any tips or tricks about this stack (Capybara and Poltergeist) would be helpful. I am not having similar difficulties in other request specs using the same stack.
Here is the request spec:
it 'allows a user to subscribe', js: true do
visit root_url
click_link "Pricing"
click_button "Subscribe", match: :first
fill_in "First name", with: "Marky"
fill_in "Last name", with: "Mark"
fill_in "Company", with: "The Funky Bunch"
fill_in "subscription_email", with: "marky.mark#thefunkybunch.com"
fill_in "Password", with: "MyString"
fill_in "Password confirmation", with: "MyString"
fill_in "Credit Card Number", with: "4242424242424242"
fill_in "Security Code on Card (CVV)", with: "123"
select "July", from: "Card Expiration"
select "2014", from: "card_year"
check "chkACCEPT"
click_button "Subscribe to myapp"
# The line below fails, but a save_and_open page just shows a blank form
current_path.should_not eq(new_subscription_path)
page.should have_content("Thank You")
end
Poltergeist provides screenshots which can be used instead of save_and_open_page:
save_screenshot('/path/to/file.png', :full => true)
However your comment "# The line below fails" seems very consistent to "save_and_open page just shows a blank form".

How to test a form Reset button

Googling around and not find a working solution...
Using Rails 3.2.11, RSpec 2.12.2 Capybara 2.0.2 I'm trying to test the "reset button" on a form (which works fine when testing with a browser)
I tried this code
scenario "Fill form then reset it" do
visit contact_path
fill_in 'message_name', :with => 'abc'
fill_in 'message_email', :with => 'abc'
fill_in 'message_subject', :with => 'abc'
click_on 'Reset form'
expect(page).to find_field('message_name').value.should == ''
end
The test fails with this error
expected: ""
got: "abc"
(compared using ==)
It seems the fields are not resetted at all.. (but in a browser they are)
What am I missing ? Something new in this version of Capybara or Rspec ?
Thanks for your help
My fault,
This type of feature/scenario requires js driver.
Selenium is included in the capybara gem but I forgot to activate it in my feature. To have it to work I just had to change the line
scenario "Fill form then reset it" do
to this one
scenario "Fill form then reset it", :js => true do
according the doc https://github.com/jnicklas/capybara#using-capybara-with-rspec. Now all works fine...
Hope it can help someone else ...
Cheers

Capybara returns right URL but ghost page

Here is what I have:
visit "..."
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
p current_url
p page.body
save_and_open_page
fill_in "Email", with: "lol#lol.com"
click_button "Update"
current_url is correct
But page.body gives an empty page with only DOCTYPE, and it's an old HTML4 DOCTYPE that does not exist anywhere in the app!
save_and_open_page also gives empty page.
Any clues?
Don't know what visit "..." Does but, when trying to visualize where the test is I like to add as an argument to the test method :js => true and then sleep 10 or however long wherever u want to pause execution, :js => true will load selenium and you can watch the tests being physically run and inspect the page if u sleep it.
Did you try visiting a specific page rather than using "..."? So try visit '/'
Also, is capybara's visit working for you in other tests? It's possibly not loaded/working properly in general.
Looks like this will be on-hold....the architecture changed and this is no longer needed...thanks for the help though...

Resources