the page content is incomplete after visit a page - ruby-on-rails

I am new to Capybara. I try to use "visit root_path" and then check if the spicified tag is rendered correctly. Firstly there is "ElementNotFound" error. And i use "puts page.html" to see the response page content. And i found that only the static parts of the page are rendered. And all the other parts dynamically created by rails(<%= %>) are NOT. BTW, i put this test in requests folder. All Capybara config are default.
Did anyone come with similar problem like it?

Try to use lanchy gem and save_and_open_page in your tests. Check this post

Related

Capybara - NameError: uninitialized constant Capybara::TimeoutError

I am creating integration tests for my rails application.
The application I am working to is a little slow. In my test, I execute a certain action within the website (a "saving" - which reloads in the end the page) and the following capybara action runs before the page is actually reloaded.
I cannot use "sleep (seconds)" as this freezes the "reloading" itself.
So I wanted to give a try to this github idea: https://gist.github.com/metaskills/1172519
but I get the following error:
NameError: uninitialized constant Capybara::TimeoutError
Can someone tell me why I get this error and what does it mean?
As you posted, you're trying to make a method which waits for the ajax requests to finish.
But there's a better way to do this:
You have a view, which loads a modal (remote, with ajax). You should not do something like the wait_until method. Or even though not with using while true.
The best way of doing this, is to set an unique html element on the modals content:
<!-- in your modal view/partial -->
<span id="modal"></span>
... modal code
When you then use Capybara like this:
find("#modal")
The find method automatically waits for all ajax requests to finish.
See https://www.varvet.com/blog/why-wait_until-was-removed-from-capybara/ for more inputs.
The reason you're getting the error is because the Capybara::TimeoutError class was removed in Capybara v2, along with the #wait_until method. As the answer by #RaVeN states you should just be telling Capybara to expect some content or elements on the page which will make Capybara wait for it to appear automatically (as long as you're using a JS capable driver)
expect(page).to have_content("Some content that appears after the page has loaded") # will wait up to Capybara.default_max_wait_time seconds for the content to appear
or if the path of the page changes you could do
expect(page).to have_current_path('<the new path you want to wait to load>')
As an aside - there is no reason sleep in tests should pause a page loading since the tests, app, browser each run in separate threads/processes assuming you're running a JS capable driver. If you're not running a JS capable driver and are instead using the default rack_test driver then waiting/sleeping for anything is pointless because every action occurs synchronously.

Rails (/Rspec) (/Capibara): Test Icon is visible?

I'm currently using Rails, Rspec and Capybara.
How would I test that an Icon is actually visible and coming through on the client side? Something like this:
<i class="ionicons ion-man"></i>
I've searched stack overflow for "Rspec Icons" and "Rails Test Icons" and haven't found an existing question that can answer this.
edit:
I don't want to show that the css is present and visible on the page, but whether the icon is coming down the asset pipeline. I just tested this solution and a passing test after 'breaking' the asset pipeline so that the icons come through as boxes. Perhaps my request is out of the ability of capybara?
You can use the have_css selector.
expect(page).to have_css('i.ion-man')
You can check if a particular node (in your case li) with a specific class (in your case ionicons) exist and is also visible:
page.has_css?('li.ionicons', :visible => true)

Testing more than one same link rspec capybara

I have two About Us links in the homepage (one in the header and one in the footer of the page). I need to test that both links point to the same About Us page.
I have written test in rspec like this inside the describe block:
it { should have_link('About Us', href: aboutus_path) }
However this test does not give error if there is at least one link is ok in the page. I need something that checks for both links and it should give error if any one of the links has been changed.
Thanks in advance for the help Guys!

Why is Rspec/Capybara giving me a expected css ... to return something error?

The test I am using is:
it { should have_link('Logout', href: logout_url) }
I am just testing for the existence of a <a> tag with Logout as the text, and the href to be logout_url path. Should I be using another syntax for this?
Gems:
rails 3.2.6
rspec-rails 2.10.1
capybara 1.1.2
There are a number of ways you can do this. I'd recommend (if you haven't already) familiarizing yourself with the overall capybara dsl as well as some of the specific sections such as the capybara matchers.
I believe your test is fine written as:
it { should have_link('Logout', href: logout_url) }
as long as the link text really is 'Logout' and the href is specified correctly (see below).
According to the capybara docs, the first parameter to have_link is what's called the locator. In your case it's 'Logout'. This can be either the text in the link itself, or it can be the #id of the dom element. So in your case you need to have the text 'Logout' in the link that has the logout_url link.
Note that locaters in capybara are case sensitive, so make sure you match on case.
You might also want to consider whether you should be using logout_url and not logout_path. By default, rails doesn't always generate the full url for most links. It just generates paths. Here's the difference:
users_url: http://localhost/users
users_path: /users
Check your page to see which of these types of url's are being generated by your app.
Probably it should be logout_path.
Also you can try to use save_and_open_page to understand what's going wrong.

Capybara not able to recognize ajax loaded elements.?

In my Rails 3 application, I have a "image" on my HTML page, which creates a "div" element in html page, when clicked.
I have to test the creation of this new "div" through RSpec (and i am using Capybara for views based testing).
I have written the following code :
it "clicks the extended details button" do
Capybara.default_wait_time = 5
within('.table_expand') do
find("#img_dealer_code_04039").click
should have_selector('#extended_details_04039')
end
end
yeah i have already added, :js=>true in corresponding describe.
I thought the problem would be of Ajax time , so i added Capybara.default_wait_time = 5 for it to load properly. (But it didnt work)
Also i tried putting :visible=>true in line should have_selector('#extended_details_04039') , but with no success..
Is there anything that i am missing..??
Please help me...I am stuck with this from quite a long time..!!
https://github.com/rspec/rspec-rails/issues/478
are you sure that you don't have duplicated ID on site?
Try to save it to file and check the source for duplications.
To see what's going on the page try
page.save_and_open_page method
Is img part of your ID or is it the tag type? I believe you shoul have
find("img#dealer_code_0409").click

Resources