Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium isn't catching JavaScript errors

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.

like image 351
user3162553 Avatar asked Mar 09 '26 03:03

user3162553


1 Answers

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

like image 166
Thomas Walpole Avatar answered Mar 10 '26 15:03

Thomas Walpole