Selenium isn't catching JavaScript errors

754 Views Asked by At

I'm using Sinatra and my test environment is set up like this.

Gemfile:

gem 'rspec'
gem 'capybara'
gem 'pry'
gem 'selenium-webdriver'

spec_helper.rb

require 'view_helpers'
require 'capybara/rspec'
require 'rspec'
require 'selenium-webdriver'

Capybara.javascript_driver = :selenium

staticpages_integration_test.rb

require 'capybara/rspec'
require './app'

Capybara.app = Sinatra::Application 
set :show_exceptions, false 

...


describe 'the home page should not have javascript errors', {:type => :feature } do

# Enable Selenium for JavaScript testing 
before :all do
    Capybara.current_driver = :selenium
    visit '/'
end

it 'the home page should run scripts without error', :js => true do 
    expect(page).not_to have_errors
end 

# Disable Selenium for standard testing 
after :all do
    Capybara.use_default_driver
end

end 

The rest of my test suite loads fine and Selenium runs and opens the firefox browser but then outputs the following error:

Failure/Error: expect(page).not_to have_errors
   expected #<Capybara::Session:0x007f8146be1e90> to respond to     `has_errors?`

What am I missing? I've tried using error_message as a method as well. The problem seems to be that Selenium isn't running/doesn't have access to those methods. Am I missing a dependency? Thanks.

Alternatively, if there's a cleaner way to test for js errors, I'd love to learn.

2

There are 2 best solutions below

1
On

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
On

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