Get content of a page in IRB (within debugger) - ruby-on-rails

I have a failing test for my Rails project (where a certain piece content is supposed to appear on the page, but is not), and I'm trying to debug it using the debugger gem. Because I see what I think is correct content when I view a page in the browser, I want to run IRB within the debugger and see what the content of the page is when the test is running. My test executes visit sponsor_path(sponsor). Is there a way to fetch the content of that route/path in IRB?

When using rspec and capybara, you can have a look at the page with save_and_open_page
Call it after you visit a path, and it will save the current page (much like doing a save from your browser) and open it.

You can perform html requests through the rails console like this:
app.get("/my_url/1")
html = app.response.body

Related

Why is my RSpec + Capybara test failing to render view?

I use wicked (1.3.1) (not sure if relevant) for an onboarding flow on my rails 5 app. On the last step of my onboarding process, there is an <a> link that navigates to a user#dashboard page.
This works totally fine in all browsers. For some reason that transition does not work in Capybara (rspec 3.7, rspec-rails 3.7.2, Capybara 2.14). The url changes in the automated browser to the desired route, but the page does not render any content, it retains the old view. Visually its as if someone typed in a url but did not press return, however, the controller method and view are getting touched when i debug them. They appear to return a rendering, but the value is not rendered in the browser
If i throw a sleep in my test after the <a> click, I can manually click on the url bar, press return (to navigate to the correct url) and the page will render then. But not on its own. Anyone experience this before?
I have tried changing the href to a different path to see if it is a problem with the target view/controller - it is not, happens to all of the paths I try. I have also tried different capybara drivers: :selenium, :chrome, :poltergeist. All same result!
Would love to provide more detail but i'm not sure what to show. Its a simple href and i'm not sure what could go wrong.
Cheers
EDIT:
turns out there was an error in the logs. Will update with a solution.
error:
Could not log "render_template.action_view" event. NoMethodError: undefined method `render_views?' for RSpec::ExampleGroups::LayoutsSplash::View:Class ["/Users/mitchellmeyer/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.6.0/lib/rspec/core/example_group.rb:732:in `method_missing'",
The error shown in the logs is a know rspec/rspec-rails 3.6 issue - upgrading will fix that. Beyond that, the fact it works when a URL is submitted manually to the browser but not when clicking on the link indicates the link is probably being interfered with by JS. Most likely that's Turbolinks which you can disable for a given link by adding a data-turbolinks=“false” attribute to the link. If that fixes the behavior (which you state it did) then it's probable you have a JS error in one of your files. Check the developer console in the browser for any errors shown and fix them.
Since you mention Poltergeist, as a driver you tried, in your question it's possible for there to be silent errors that cause JS to fail when using it since it doesn't support anything beyond ES 5.1 so it's not really that suitable for testing modern webapps any more and I would recommend sticking to headless chrome if you need headless testing.

rubymine view html generated by erb rails

Is it possible to see the html generated by html.erb files within RubyMine? I know that I have been able to see the css.min generated by css, so there must be a similar option to be able to see the direct html generated by embedded ruby?
With your Rails server running you can right click a view template file in the project tree and select open in browser
But this is essentially the same as visiting localhost:3000/posts manually, e.g.
Rubymine can't render the view and make the necessary calls to the controller actions and database without the whole app running

Capybara: cannot find CSS and page.body is blank

I've just started writing integration tests with Capybara and I am running into the following symptoms after successfully accessing a page.
#response.body shows the correct HTML as expected.
page.body shows nothing.
save_and_open_page saves the file but the file is empty.
click_on("#item") fails (note I can see it in #response.body). Many other types of CSS access also fail here.
I surmise that Capybara failing to access known CSS is related to its rendering a blank page. Is there an underlying reason for all this behavior?
You need to use #visit to have Capybara go to a page. Capybara is designed to emulate a user using a browser - it has no support for direct get/post/put... since a user doesn't have that. It also has no direct access to the response from the app, since the browser handles that. What it does let you do is move around the app, in a browser, like the user would and then assert against elements of the page appearing, disappearing, and displaying expected information

Is there a way in Ruby on Rails to generate a screenshot of a referer page?

I have a custom exception engine that catches an exceptions, emails details about the exception to me, and then renders a friendly error page. What I would really like to do is to be able to grab a screenshot of the page where the error occurred and attach it to the email.
The problem I have been running into is that the page I want to screenshot is technically the page previous to the current request. So when I have tried inspecting the env hash on an exception, it appears to only have information related to the exception, not the previous page. So if I call render_to_string and use the HTML to generate a screenshot, it ends up being a shot of the friendly error page, rather than the referer page.
Is this possible?
And if it is possible, will it get the page with any JavaScript that was running? For example, if a user opened a JS modal on a page and then got an exception when clicking something inside the modal, I would want the screenshot showing the modal.

capybara and reloading iframe with javascript

so I have a rails app with an IFrame, and at some point I reload that IFrame via javascript and it's content changes.
So I'm trying to test that behavior with following code
sleep 3
within_frame('email-content') do
page.should have_content "<h1>Email content</h1>"
end
When I run it with selenium I can see that IFrame's content has updated, however capybara still sees old content. Making it wait longer before checking it's content does not work either. Any help is appreciated. Thanks :)

Resources