How to Test JavaScript error in Capybara/Poltergeist
Asked Answered
B

2

7

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

...
Bradfield answered 17/3, 2015 at 23:25 Comment(1)
Have the same problem. Please, populate your question with configs from spec_helper.Outgrowth
O
2

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

Outgrowth answered 1/12, 2015 at 14:21 Comment(0)
O
0

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!

Outgrowth answered 22/11, 2015 at 20:27 Comment(5)
Thank you, but I don't use capybara-webkit. I tried solve trouble with poltergeist and found solution for capybara-webkit. I need same solutionBradfield
@Bradfield I use poltergeist too, my problem - js: true rspec tests locally are green but fails on codeship...Outgrowth
@Bradfield you want to check poltergeist errors of javascript errors on front-end side?Outgrowth
I want to check javascript errors on front-end sideBradfield
@Bradfield check out my new answer in this case :)Outgrowth

© 2022 - 2024 — McMap. All rights reserved.