Capybara returns right URL but ghost page - ruby-on-rails

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...

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 cannot find a link on a modal which changes on button press

I have a rails project using rspec 3.4.0 capybara 2.6.2 and capybara-webkit 1.8.0.
I am writing a feature test for a flow on my site which looks like the following:
scenario "Buyer creates a seller profile", :js => true do
click_link("SELL ON MYSITE",match: :first)
expect(page).to have_text("Reach thousands of customers in your area")
click_link("Create an Activity",match: :first)
expect(current_path).to eql (new_seller_profile_path)
fill_in "seller_profile[business_name]", :with => "Test company"
fill_in "seller_profile[business_email]", :with => "test#email.com"
fill_in "seller_profile[business_phone_number]", :with => "07771330510"
fill_in "seller_profile[business_description]", :with => "This is a test company"
find('label[for="social"]').click
find("#facebook-placeholder").click
fill_in "seller_profile[business_facebook_url]", :with => "https://www.facebook.com/test"
click_button("CREATE AN ACTIVITY")
------this button opens a modal after the page changes --------
fill_in "seller_profile[requested_postcode]", :with => "EH21 8PB"
click_button("Submit")
save_and_open_screenshot
------this is where it goes wrong-------
click_link("Continue")
expect(page).to have_text("Choose the type of activity that you want to create")
end
The click_link continue fails with error:
Failure/Error: click_link("Continue")
Capybara::ElementNotFound:
Unable to find link "Continue"
The link actually does exist - when you click on the submit button some javascript executes which changes the contents of the modal to display some new text and a new button. However for some reason click_link does not wait or look in the modal, it fails straight away.
Having added a save_and_open_screenshot call we can see this situation as the modal javascript has yet to execute as we can still see the submit button :
Interestingly the mouse seems to not be on the Submit button as it should have just clicked it?
How can I make the click_link wait until the continue button appears?!
This is the javascript which executes on press of 'submit' added to the modal which changes it:
$('#gate .modal-body .intro').text('Congratulations, we are available in your location, please continue to create your activity.');
$('#gate .modal-footer').append('<a class="btn btn-lg btn-primary" href="/events/new/">Continue</a>');
Seemingly adding a couple of sleeps in has fixed it. I don't know if this is 'right' solution. If somebody has a better way or potentially a better way I would love you to help me find it :
fill_in "seller_profile[requested_postcode]", :with => "EH21 8PB"
sleep 2
click_button("Submit")
sleep 2
click_link("Continue")

Capybara: Test current path to page created object

After creating new object should redirect to action show. How me check current path?
feature 'add lost pet' do
given(:data) {attributes_for(:lost_pet)}
background do
visit root_path
click_on 'Register new lost pet'
end
scenario 'add new lost pet with valid data' do
within '#new_lost_pet' do
fill_in 'Name', with: data[:name]
fill_in 'Type', with: data[:type]
fill_in 'Breed', with: data[:breed]
fill_in 'Gender', with: data[:gender]
fill_in 'Size', with: data[:size]
fill_in 'Colour', with: data[:colour]
fill_in 'Age', with: data[:age]
fill_in 'Age unit', with: data[:age_unit]
fill_in 'Description', with: data[:description]
fill_in 'Collar description', with: data[:collar_description]
check 'Desexed', :checked
check 'Microchipped', :checked
fill_in 'Microchip number', with: data[:microchipped_number]
select '2015', from: "lost_pet[date_missing(1i)]"
select 'October', from: 'lost_pet[date_missing(2i)]'
select '10', from: 'lost_pet[date_missing(3i)]'
fill_in 'Rewald', with: data[:rewald]
fill_in 'Image', with: data[:image]
fill_in 'Adress lost', with: data[:adress_lost]
click_on 'Create'
end
expect(current_path).to eq lost_pet_path(????)
end
For lost_pet_path i need id, but how me create id? Or how better check path in Capybara?
expect(current_path).to eq ...
doesn't use Capybara's waiting behavior - which means since click_on is asynchronous (isn't waiting for anything on screen, or for the submit to complete) your test may be very flaky. You're much better off using
expect(page).to have_current_path(expected_path)
since that will use Capybara's waiting behavior while checking for the expected path.
On top of that you have the issue that the LostPet object isn't yet created right after the click_on executes (asynchronous) so calling LostPet.last then will most likely return nil. You have a couple of options here
Wait for some text thats going to appear on the page
expect(page).to have_text('Lost Pet created') # shows in a flash message, or header on the show page, etc
# since you know the show page is visible now you can query for the last LostPet created
expect(page).to have_current_path(lost_pet_path(LostPet.last))
or, use the regex option with have_current_path and don't worry about verifying the actual id of the url
expect(page).to have_current_path(/lost_pet\/[0-9]+/) # match the regex to whatever your urls actually are
or something like that
Since the created record is the most recent one in the database, MyActiveRecordModel.last can be used.
lost_pet = LostPet.last
expect(current_path).to eq lost_pet_path(lost_pet)

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

Resources