7
votes

I need to check has page a JavaScript error.

Solution for capybara-webkit http://blog.55minutes.com/2013/10/test-javascript-with-capybara-webkit/

require 'spec_helper'

feature 'Home' do
  it 'should not have JavaScript errors', :js => true do
    visit(root_path)
    expect(page).not_to have_errors
  end
end

How to make one look the same as for Poltergeist?

spec_helper.rb

...
require 'capybara/rails'
require 'capybara/selenium/driver'
...


selenium_hub_host = "selenium"
selenium_hub_port = "100"
selenium_url = "http://#{selenium_hub_host}:#{selenium_hub_port}/wd/hub"

...
Capybara.register_driver :selenium_remote do |app|
  options = {}
  options[:browser] = :remote
  capabilities = Selenium::WebDriver::Remote::Capabilities.firefox
  capabilities[:platform] = :any
  capabilities[:takes_screenshot] = true
  options[:url] = selenium_url
  options[:desired_capabilities] = capabilities
  Capybara::Selenium::Driver.new(app, options)
end

Capybara.javascript_driver = :selenium_remote
Capybara.default_max_wait_time = 30

Capybara.server do |app, port|
  require 'rack/handler/thin'
  Rack::Handler::Thin.run(app, :Host => '0.0.0.0', :Port => port)
end

...
2
Have the same problem. Please, populate your question with configs from spec_helper.bmalets

2 Answers

2
votes

Try this configurations:

Capybara.register_driver :poltergeist do |app|
  Capybara::Poltergeist::Driver.new( app, {
    debug:     true,  # turn on poltergeist debug mode
    js_errors: true,  # turn on javascript errors on page
    timeout:   10000,
    phantomjs_options: ['--load-images=yes', '--ignore-ssl-errors=yes', '--ssl-protocol=any']
  })
end

Capybara.javascript_driver = :poltergeist
Capybara.current_driver    = :poltergeist
Capybara.default_wait_time = 5
Capybara.server_port       = '3000'
Capybara.app_host          = "http://127.0.0.1:3000"

Turn off loading images and poltergeist debug mode if you don't need it.

BTW, Capybara doesn't include a have_errors matcher. To use that matcher you would need to be using the capybara-webkit gem/driver instead of selenium

If you use PhantomJS/Poltergeist with Capybara to run your tests it will fail the test and output the any error (including JS errors)...

Along with that it will also output JS warnings, which does not fail the test but still gives you visibility of mess in your site...

If JS errors is a big deal for the product your are testing I suggest using it along with teaspoon...

Regarding Selenium WD, it's a bit out of scope to monitor JS errors on the page given there are specific tools out there to do that...

0
votes

Link to turtorial, which is attached in your question describes:

  • rspec, capybara and capybara-webkit configurations

But in title of your question you mention poltergeist. In this case you don't need to setup capybara-webkit! You should use this set of gems:

  • rspec, capybara and poltergeist (poltergeist use phantomjs)

Please read this: http://tutorials.jumpstartlab.com/topics/capybara/capybara_and_phantom.html

Hope it helps you!