I'm using poltergeist to spin the rails feature spec on jenkins.
Error Message
One or more errors were raised in the Javascript code on the page.If you don't care about these errors, you ignore them by setting js_errors: false in your Police configuration (see documentation for details) ...
Stack trace
One or more errors were raised in the Javascript code on the page.If you don't care about these errors, you can ignore them by setting js_errors: false in your Police configuration (see documentation for details).
ReferenceError: Can't find variable: $td
ReferenceError: Can't find variable: $td
at ttp://127.0.0.1:38079/assets/admin/application.js:14065
[object Object]
[object Object]
at ttp://www.googletagmanager.com/gtm.js?id=HOGE:51
at ttp://www.googletagmanager.com/gtm.js?id=HOGE:59 (Capybara::Poltergeist::JavascriptError)
Capybara::Poltergeist::JavascriptError:
One or more errors were raised in the Javascript code on the page.If you don't care about these errors, you can ignore them by setting js_errors: false in your Police configuration (see documentation for details).
ReferenceError: Can't find variable: $td
ReferenceError: Can't find variable: $td
at ttp://127.0.0.1:38079/assets/admin/application.js:14065
[object Object]
[object Object]
at ttp://www.googletagmanager.com/gtm.js?id=HOGE:51
at ttp://www.googletagmanager.com/gtm.js?id=HOGE:59
./spec/features/admin/hoge_spec.rb:237:in`block(4 levels)in'
As shown in , javascript errors appear and fall (renaming hoge_spec and url id).Also, I was told not to include url, so I set http as http).The above information is an example, and the location of the error changes every time you do it.
The spec_helper.rb includes:
#This file is copied to spec/when you run 'rails generate rspec:install'
ENV["RAILS_ENV"]||='test'
require File.expand_path("../../config/environment", __FILE__)
require'rspec/rails'
require 'rspec/autorun'
require'simplecov'
require'simplecov-rcov'
SimpleCov.formatter=SimpleCov::Formatter::RcovFormatter
SimpleCov.start do
add_filter'/vendor/'
end
# Requirements supporting ruby files with custom matches and macros, etc.,
# spec/support/and its subdirectories.
Dir [Rails.root.join("spec/support/**/*.rb")] .each {|f|require f}
# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending!if defined?
includeWarden::Test::Helpers
Warden.test_mode!
RSpec.configure do | config |
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the approve line:
#
# config.mock_with —mocha
# config.mock_with —Flexmock
# config.mock_with —rr
# Remove this line if you're not using ActiveRecord or ActiveRecord fixes
config.fixure_path="#{:Rails.root}/spec/fixes"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples with a transaction, remove the following line or assign false
# installed of true.
config.use_transactional_fixes=!true
# If true, the base class of anonymous controllers will be interested
# automatically.This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers=false
# Run specifics in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by provisioning
# The seed, which is printed after each run.
# --seed1234
config.order="random"
config.include FactoryGirl::Syntax::Methods
config.before(:suite)do
DatabaseCleaner.strategy=:truncation
DatabaseCleaner.clean_with:truncation
end
config.before —Each do
DatabaseCleaner.start
end
config.after —Each do
DatabaseCleaner.clean
end
config.before —all do
FactoryGirl.reload
end
config.includeDevice::TestHelpers, type::controller
config.include Capybara:DSL
config.include Login
config.include NewWindow
config.before do
if page.driver.try —headers
page.driver.headers={'User-Agent'=>'Mozilla/5.0 (Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36'}
end
end
config.after do | example |
example=example.example
if example.metadata [:type] == :feature and Capybara.javascript_driver==:poltergeist and example.exception.present?
page.save_screenshot"spec/screenshot/#{example.full_description}.png"
end
end
end
Capybara.default_wait_time=ENV['WAIT_TIME']||3
require 'capybara/rspec'
require 'capybara/rails'
# require'selenium-webdriver'
require 'capybara/poltergeist'
Phantomjs.path
# Capybara.javascript_driver=: selenium_chrome
# Capybara.javascript_driver=: selenium_firefox
Capybara.javascript_driver=:poltergeist
Capybara.register_driver:poltergeist do | app |
driver=Capybara::Poltergeist::Driver.new(app,js_errors:false,timeout:10000,phantomjs:Phantomjs.path,phantomjs_options:['--ignore-ssl-errors=yes','--ssl-protocol=any')
driver.headers={'User-Agent'=>'Mozilla/5.0 (Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36'}
driver
end
Capybara.register_driver: selenium_chrome do|app|
Capybara::Selenium::Driver.new (app,:browser=>:chrome)
end
Capybara.register_driver: selenium_firefox do|app|
profile=Selenium::WebDriver::Firefox::Profile.new
ua= 'Mozilla / 5.0 (Intel Mac OS X 10_10_5) Apple WebKit / 537.36 (KHTML, like Gecko) Chrome / 46.0.2490.86 Safari / 537.36'
profile ["general.useragent.override"] =ua
options={browser::firefox,profile:profile}
Capybara::Selenium::Driver.new(app,opts)
end
As mentioned above, if javascript errors occur even though js_errors:false is turned on, the test will fail.
In order to correct the javascript error, there seems to be some errors derived from tagmanager, so I think it will be difficult.
When I did the same test in the local environment, the test didn't fail due to a javascript error, so I think it might be due to the Jenkins environment.
How can I ignore the javascript error and not pass the rspec test?
Environment:
CentOS release 6.4(Final)
ruby2.1.5p273
Rails 4.0.5
Jenkins ver.1.552
phantomjs 1.9.2
policergeist (1.6.0)
It is not a fundamental measure, but there is an approach that reads only during testing.
<%=javascript_include_tag('//example.com/foo_bar.js')unless Rails.env.test?%>
If it's your JS code, you should eliminate warnings and errors, but since external scripts such as Google and Facebook are often hard to control, it's one way to give up if you don't want to. (In fact, there are projects like that in the past.)
However, of course, this approach is not available if the JS causing the error is critical to running the test.
© 2024 OneMinuteCode. All rights reserved.