Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Test JavaScript error in Capybara/Poltergeist

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

...
like image 726
vovan Avatar asked Mar 17 '15 23:03

vovan


1 Answers

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

like image 111
bmalets Avatar answered Sep 20 '22 20:09

bmalets