Search code examples
javascriptruby-on-railsseleniumcapybararspec-rails

Rspec failed for Javascript tests headless chrome on Github actions


I am using Github action to run Rspec tests which have many feature tests with few javascript. All other test passing but Javascript one are failing. Is there way to run rspec capaybara tests with javascript using github actions. Following is my capybara helper configuration

Capybara.server = :puma, { Silent: true }

Capybara.register_driver :chrome_headless do |app|
  options = ::Selenium::WebDriver::Chrome::Options.new

  options.add_argument('--headless')
  options.add_argument('--no-sandbox')
  options.add_argument('--window-size=1400,1400')

  Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end

Capybara.javascript_driver = :chrome_headless

# Setup rspec
RSpec.configure do |config|
  #config.before(:each, type: :system) do
  #  driven_by :rack_test
  #end

  config.before(:each, type: :system, js: true) do
    driven_by :chrome_headless
  end
end

I am getting error for Javascript test errors

2) Visitor signs up with valid email and password
     Failure/Error: width = page.evaluate_script("$('#LeftNavPaneContent').width();")
     Capybara::NotSupportedByDriverError:
       Capybara::Driver::Base#evaluate_script
     # ./spec/features/javascript/navigation_toggle_spec.rb:12:in `block (2 levels) in <top (required)>'
  3) Login and out it should redirect to the dashboard and allow you to log out
     Failure/Error: page.driver.browser.navigate.refresh
     NoMethodError:
       undefined method `navigate' for #<Capybara::RackTest::Browser:0x00005589b2afd070>
     # ./spec/features/login_logout/login_spec.rb:15:in `block (2 levels) in <top (required)>'

Following is my action file for chromedriver

run:  |
  sudo apt-get -yqq install libpq-dev
  sudo apt-get install google-chrome-stable
.
.
.
  run: |
   export DISPLAY=:99
   chromedriver --url-base=/wd/hut &
   sudo Xvfb -ac :99 -screen 0 1280/1024/24 > /dev/null 2>&1 & # optional
   bundle exec rspec

These tests locally pass and no issue. I think it is because these have Javascripts inside test I am using js: true but seem it is not working properly. Any help


Solution

  • I used following configuration in my rails_helper and it worked.

    Capybara.register_driver :headless_chrome do |app|
      capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
          chromeOptions: { args: %w[--headless --disable-gpu] },
          'goog:loggingPrefs': {
              browser: 'ALL'
          }
        )
    
      options = ::Selenium::WebDriver::Chrome::Options.new
    
      options.add_argument('--headless')
      options.add_argument('--no-sandbox')
      options.add_argument('--window-size=1400,1400')
    
      Capybara::Selenium::Driver.new(
          app,
          browser: :chrome,
          desired_capabilities: capabilities,
          options: options
        )
    end
    
    Capybara.default_driver = :headless_chrome