ruby-on-railsautomated-testscapybararspec-railscodeship

Capybara errors in remote environment only


I am trying to automate feature tests using rspec/Capybara in Rails when pushing commits to GitHub. It seems not find the form elements, double checked the ID and tested without the within helper. The tests run and pass without issue when running locally. However, using the same Capybara driver, they fail when running the automated tests, showing:

Capybara::ElementNotFound:
   Unable to find css "#new_user"

When executing in GitHub, the following appears when the run command begins (unsure if this has any importance to this issue):

config.eager_load is set to nil. Please update your config/environments/*.rb files accordingly

in my spec_helper.rb:

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('--disable-dev-shm-usage')
    options.add_argument('--window-size=1400,1400')

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

Capybara.configure do |config|
   config.javascript_driver = :chrome_headless
end

Full Screenshot of errors: enter image description here enter image description here

Update 8/18/2020. I am now using the following configuration in spec_helper.rb and it is not finding select boxes. Now working on implenting Codeship, I am seeing the same error.

  Capybara.register_driver :selenium_headless_chrome do |app|
    caps = Selenium::WebDriver::Remote::Capabilities.chrome(loggingPrefs: { browser: 'ALL' })
    opts = Selenium::WebDriver::Chrome::Options.new
  
    chrome_args = %w[--headless --window-size=1920,1080 --no-sandbox]
    chrome_args.each { |arg| opts.add_argument(arg) }
    Capybara::Selenium::Driver.new(app, browser: :chrome, options: opts, desired_capabilities: caps)
  end

  Capybara.configure do |config|
    # change this to :chrome to observe tests in a real browser
    config.default_max_wait_time = 10 # seconds
    config.javascript_driver = :selenium_headless_chrome
  end

Solution

  • Solved: Using puts page.body, the output showed that it was displaying a template error. After some troubleshooting it was determined that the error was happening because of the application.js manifest being empty and assets not precompiling. After adding yarn install then rails assets:precompile in the tests.yml file (for GitHub actions) or project settings in Codeship, everything worked as expected. The full list of setup commands is: rvm use 2.5.8 --install gem install bundler:2.1.4 bundle install yarn install rails assets:precompile