save_and_open_page not working with capybara 2.0? - ruby-on-rails

I just had my capybara updated to 2.0, and all of my save_and_open_page calls return an html page without styling. It has the style sheets links properly at the top of the page. When I revert back to capybara 1.3, the styles work again. Anyone know how to fix this, and/or what the problem is?
thanks,
Dave
Update 1.
hack:
I have found a reasonable way to get around the problem following the links recommended by simonmorley:
Capybara Webkit problem
which points to: capybara-screenshot
It involves using the capybara-screenshot gem to get a screenshot of what the page looks like. When you combine it with the save_and_open_page, which generates the html, you can see what it looks like, and see the HTML.
save_and_open_page
Capybara::Screenshot.screenshot_and_open_image
If I could get Capybara::Screenshot.screenshot_and_save_page to work, then I think that I might have a solution (if the name implies the action I think it does). However, when I try it, I get cannot load such file -- capybara/util/save_and_open_page
Update 2.
!! wait, the screenshot_and_open_image is not working now (no png image created or displayed). !!
Update 3.
Test App:
I created a test app, and posted it at test_capybara_screenshot on Github.
When I run this test app in development, the page come up with the blaring red background style displayed. When I run the tests, the only thing that works for me is the save_and_open_page, except there is no styling.
Note, when I run any tests, I get the following warning:
WARNING: Nokogiri was built against LibXML version 2.7.8, but has dynamically loaded 2.7.3
Details of the rspec test:
The save_and_open_page comes up with a boring white background (no
styling).
the screenshot_and_open_image returns with the error:
Rack::Test capybara driver has no ability to output screen shots. Skipping.
Failure in opening /~/Documents/experiments/test_capybara_screenshot/tmp/capybara/screenshot-2012-11-26-07-48-29.png with options {}: No application found to handle '/~/Documents/experiments/test_capybara_screenshot/tmp/capybara/screenshot-2012-11-26-07-48-29.png'
The screenshot_and_save_page returns:
An error occurred in an after hook
LoadError: cannot load such file -- capybara/util/save_and_open_page
occurred at ~/.rvm/gems/ruby-1.9.3-p125#global/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:251:in `require'

Try adding the following to your gem file
group :test do
gem "capybara"
gem "launchy"
end
save_and_open_page won't work without this I don't believe.
-- Updated --
I've tested this for you and when I used the save and open method, I get the same error. In my set up, this is because my page is referencing:
<link href="/assets/application.css" media="screen" rel="stylesheet" type="text/css" />
This happens in Firefox, Chrome and Safari. According to the inspector, none of the files are found.
I haven't looked into this in too much detail and don't have an answer why it's not working. However there are a number of people struggling with this also:
https://github.com/jnicklas/capybara/issues/485
And, a bit more information about capybara and asset precompilation:
https://github.com/jnicklas/capybara/pull/609
And what might be a fix here:
https://groups.google.com/forum/#!msg/ruby-capybara/SBCor8UUj7w/hv97EgUQ1P4J
However, if you just want to see your page you could try this: delete scrap save_and_open_page and change your tests to use javascript. This way, my pages open fine.
require 'spec_helper'
describe "Foo" do
it "should fail if Bar is lactose intolerant", :js => true do
# .....
end
end
Try that and let me know if that helps.
response (From Taylored Web Sites):
I see the page for a few seconds, then it goes away. Is there a way for the browser window to not close?

I was able to fix the problem with save_and_open_page after some blog browsing and digging. I have dropped the capybara_screenshot gem from my project. You can see my working code on my github test_capybara_screenshot repository. The solution that I figured out uses some pointers that I found on the capybara github site.
Assumptions:
You are using rspec for testing.
Your app is already configured for using the asset pipeline.
The first thing that I do is set it up so that assets are precompiled into a test directory. I do this by adding the following code into the spec/spec_helper.rb file within the RSpec.configure loop:
config.before (scope = :suite) do
%x[bundle exec rake assets:precompile]
end
I specify where the assets are pre-compiled to in the config/environments/test.rb file:
config.assets.prefix = "assets_test" # place test assets in public/assets_test directory
config.action_controller.asset_host = "file://#{::Rails.root}/public"
This makes it so that the test assets are independent of the development assets, and are only generated during a test suite run.
If you do this, you will probably want to have git ignore the /public/assets* directories.

Related

Net::ReadTimeout issues when opening pdf after moving from capybara-webkit to selenium-chromedriver

I updated my test environment from Capybara-webkit to Selenium and with Chrome to test against a more modern browser. But now I have this issue with a test that opens a pdf in a new tab. I'm not sure what causes this problem.
click_on 'Print'
within_window windows.last do
io = StringIO.new(page.body)
strings = PDF::Reader.new(io).pages.map(&:text).join(' ').split('
')
# Do some testing here.
end
When Print is clicked a pdf file is generated in a new tab.
I like to read the files content using the pdf-reader gem.
When I run this test I get a Net::ReadTimeout.
In my console I see some warnings for loading the .css and .js files:
Warning: Failed to load http:localhost:3000/assets/application-92d08a385444e234a36aeb5970e4fbbaf5a5c0130ccfcc429905fa333a588b32.css
This test worked just fine with capybara-webkit.
Any ideas how to resolve this issue?
Found it!
For those running into the same problem. ActionDispatch.SystemTesting.Server is registering a Puma server with 0:1 threads. For working with pdf's you need more then 1 thread.

Why are my Javascript-dependent feature specs passing when not in Javascript mode?

I have non-Javascript specs and Javascript feature specs that use capybara/capybara-webkit.
I have some tests that I expected to fail when in non-Javascript mode, but they are passing. I've tried the functionality in my browser with Javascript turned off, and the functionality does indeed not work as expected. But in Capybara, it works.
I've also tried adding js: false to the individual test, to make sure there wasn't something hidden in my config turning it on. The spec still passes.
The selenium gem is not included in my Gemfile.
I see two possibilities: your tests are correct but there might be some reason why they pass unexpectedly, or your tests are incorrect and are passing for the wrong reason when you run them in Javascript.
A few suggestions for how to debug that apply to both possibilities:
Use a debugger. (If you have, let us know what you learned.)
Look at the Rails log from a single run of a spec to see what controller actions are called and in what order.
Use save_and_open_page to snapshot your page and see whether your spec should pass on what you're looking at.
Double-check your tests when run with Javascript:
Run them with the poltergeist driver instead of the capybara-webkit driver. It gives better error messages than capybara-webkit and can be configured to report Javascript error messages.
Use save_screenshot to take an actual screenshot of the page and verify that the spec is not passing for a spurious reason, perhaps because some content is off the screen.
If you want to be sure whether rspec is running Javascript, use ps to see whether there is a capybara-webkit or phantomjs process running.

Test Unit fails to load xsd file

I have a ruby file that requires a file which has require 'xsd/qname'
and all my files work fine, but when I go to test them with test unit I keep getting these errors
LoadError: no such file to load -- xsd/qname
I've been scouring google for a while and fail to see a solution. (I'm new to test unit so it might be incredibly simple).
EDIT
I believe my problem is related to the fact that the code is in a gem and not the rails environment, therefore the code using it loads rails while these tests do not.
The odd part is if I go in the gem with irb, I can require 'xsd/qname', but I can't require 'soap/rpc/driver' which is another error I was getting
It's probably path related. It's difficult to advise a best solution without seeing how your project is laid out, but try replacing the require statement with:
require File.dirname(Rails.root + '/xsd/qname')
You may need to adjust that depending on where that file exists within your project.
I think the problem was using files built in ruby 1.8 with 1.9. Using soap4r solved my problem.
https://github.com/spox/soap4r-spox
Fyi if you just want to just rescue the error do this:
begin
require 'xsd/qname'
rescue LoadError
puts "xsd/qname not found...."
exit
end

rubymine only able to run model tests one by one

I am able to right-click on any of my 3 spec/models but when I right click the spec/models folder and select 'run all tests in models' I get 'Unable to attach test reporter to test framework'.
The first line of my model tests is: require 'spec_helper.rb'
I posted this answer for another question. It may help with this one as well:
On my system, the problem was the "redgreen" gem. It automatically colors the progress marks (dots) and the Test::Unit summary messages based on success or failure. There must be something in RubyMine that's trying to parse the Test::Unit results (the output of "rake test", I imagine), and it's choking on the ANSI sequences.
I commented out "require 'redgreen'" in my test/test_helper.rb, and the problem went away. I really like redgreen for executing "rake test" from the shell, though, so I put this in test_helper to make it work for both "rake test" and within RubyMine:
require 'redgreen' if $stdin.tty?
It may not be redgreen that's causing your problem, but be suspicious of anything which might create unconventional Test::Unit output.
Good luck!
I had the same issue and had to add the following to /etc/launcd.conf (I had to create this file as well):
setenv DYLD_LIBRARY_PATH /usr/local/mysql/lib/
and reboot. There are other ways to do it as well (a plist file, adding this environmental variable to RubyMine, etc… but this was most reliable. Of course, this assumes that you use MySQL.
The answer in the end for my setup was to fiddle around with the IDE settings including the ruby version, making sure it was 1.9.2 and the directories referenced in the config. screens were correct. This plus some restarts resolved the issue.

How can I see what capybara found in a failing cucumber step?

I started migrating from cucumber + webrat to cucumber + capybara. Now the behavior of "I should see " seems to be somewhat different. Most of these fail now, although I didn't change anything on the page. I replaced the snippet that should be found with some stuff that is on every page and for some text it works and for other text it doesn't. I can't find any pattern in what is found in the page's content and what is not.
Webrat used to print what the page content is that it found, in case it did not contain the required phrase. Is there anyway to have capybara show what text it got from the page in which it tried to find the text?
Then show me the page calls webrat/capybara's underlying save_and_open_page method. Found that useful when working with steak.
Try adding this step:
Then show me the page
If you want to have the browser open the page when the page fails you use the 'launchy' gem.
Add it to your gem file, and then in /features/support create a file called debugging.rb with contents:
After do |scenario|
save_and_open_page if scenario.failed?
end
If you're using Javascript or Ajax in your pages and want to see what's going on, I've found that the Poltergeist driver is very good at letting you get into the DOM and find out what's going wrong.
If you setup your Capybara driver with the remote debugging option:
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, inspector: true)
end
You can then put the following line in your steps:
page.driver.debug
Which fires up a new Chromium browser with the current DOM state set, letting you get at the console. (On my version of Linux, I had to symlink chromium to chromium-browser but it otherwise worked fine).
Source Info: http://jonathanleighton.com/articles/2012/poltergeist-0-6-0/
Then show me the response didn't work for me with cucumber 1.1. I found useful to write a step using capybara's command:
print page.html
This outputs the current state of the DOM
You could also use "Then show me the response" which outputs the HTML to the console if you don't want to use a browser.
You could always have it take a screen shot when something failed. I debug a LOT of failing features that way.

Resources