titusfortner / webdrivers

Keep your Selenium WebDrivers updated automatically
MIT License
593 stars 110 forks source link

Feature specs fail in Docker but not locally #164

Closed sterankin closed 4 years ago

sterankin commented 4 years ago

Summary

I am trying to get my feature specs working in a Docker container for my CI build but the specs fail. However, the specs pass locally on my OSX. Note that I am trying to run the tests against a URL (not localhost)

I can run:

bundle exec rake spec:features

on my Mac and the tests pass. But when I try using Docker on my mac it fails. I noticed that the bundle install of webdrivers was not causing a $HOME/.webdrivers folder to be installed, so I am forcing it now in my Dockerfile.

Debug Info

Webdriver Debug Logs from Docker:

2019-12-23 19:44:32 DEBUG Webdrivers Checking current version 2019-12-23 19:44:32 DEBUG Webdrivers /root/.webdrivers/chromedriver is already downloaded 2019-12-23 19:44:32 DEBUG Webdrivers making System call: ["/root/.webdrivers/chromedriver", "--version"] 2019-12-23 19:44:32 DEBUG Webdrivers System call returned: ChromeDriver 79.0.3945.36 (3582db32b33893869b8c1339e8f4d9ed1816f143-refs/branch-heads/3945@{#614})

2019-12-23 19:44:32 DEBUG Webdrivers Current version of /root/.webdrivers/chromedriver is ChromeDriver 79.0.3945.36 (3582db32b33893869b8c1339e8f4d9ed1816f143-refs/branch-heads/3945@{#614})

2019-12-23 19:44:32 DEBUG Webdrivers making System call: ["/usr/bin/google-chrome", "--product-version"] 2019-12-23 19:44:32 DEBUG Webdrivers System call returned: 79.0.3945.88

2019-12-23 19:44:32 DEBUG Webdrivers Browser version: 79.0.3945.88 2019-12-23 19:44:32 DEBUG Webdrivers Latest version available: 79.0.3945.36 2019-12-23 19:44:32 DEBUG Webdrivers A working webdriver version is already on the system

Gem file:

group :development, :test do
  gem 'minitest', '5.4.0'
  gem 'rspec-rails', '3.6.0'
  gem 'shoulda-matchers', '~> 3.1'
  gem 'factory_girl_rails', '4.3.0'
  gem 'spork-rails'
  gem 'quiet_assets', '1.0.2'
  gem 'simplecov', '0.8.2'
  gem 'simplecov-bamboo', '0.1.0'
  gem 'dotenv-rails'
  gem 'pry'
end

group :test do
  gem 'faker', '1.2.0'
  gem 'launchy', '2.4.2'
  gem 'database_cleaner', '1.2.0'
  gem 'capybara', '2.2.1'
  gem 'webdrivers',  '4.1.3'
  gem 'rspec_junit_formatter'
  gem 'rack_session_access', '0.1.1'
end

spec_helper.rb:

require 'rack_session_access/capybara'
require 'webdrivers/chromedriver'
  require 'rspec/rails'
  require 'capybara/rails'
  require 'capybara/rspec'

Capybara.configure do |config|
  config.run_server = false
  config.app_host = 'https://my-test-site.com'
end

Capybara.register_driver :chrome_headless do |app|
  caps = Selenium::WebDriver::Remote::Capabilities.chrome(loggingPrefs: { browser: 'ALL' })
  opts = Selenium::WebDriver::Chrome::Options.new

  chrome_args = %w[--headless --no-sandbox --disable-gpu --disable-dev-shm-usage]
  chrome_args.each { |arg| opts.add_argument(arg) }
  Capybara::Selenium::Driver.new(app, browser: :chrome, options: opts, desired_capabilities: caps)
end

Capybara.javascript_driver = if ENV['SHOW_BROWSER']
                               :chrome_windowed
                             else
                               :chrome_headless
                             end

Expected Behavior

I expect the tests to pass the same as my local

Actual Behavior

Test fail e.g.: Capybara::ElementNotFound: Unable to find field "username"

I can see this is in logs caused by my Dockerfile rake task to update chromedriver: INFO Webdrivers Updated to chromedriver 79.0.3945.36

So I know its installing ok. But I'm not sure why the tests are failing.

Here is my Dockerfile to replicate:

# Dockerfile
# /path/to/your/app/Dockerfile
FROM jruby:9.1.17

RUN apt-get update
RUN apt-get install -y wget git

# Essentials
RUN apt-get install -y git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev nodejs yarn unzip

# Install Chrome
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
    && echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list
RUN apt-get update && apt-get -y install google-chrome-stable

RUN apt-get clean

WORKDIR /tmp

# Cache Gems
ADD Gemfile .
ADD Gemfile.lock .

# Set environment to test for bundle install
ENV RAILS_ENV=test

# Install correct bundler version and bunde install
RUN gem install bundler -v 1.17.3
RUN bundler _1.17.3_ install --jobs 4 --retry 3

# Copy your app's code into the image
WORKDIR /usr/src/app
ADD . /usr/src/app

# ensure webdrivers is installed
RUN gem install webdrivers -v 4.1.3

# run rake task to update webdriver to ensure .webdrivers folder is created at $HOME
# then kick off tests.
RUN /bin/bash -c "bundle exec rake webdrivers:chromedriver:update && bundle exec rake spec:features"

Can anyone suggest what might be the issue here?

kapoorlakshya commented 4 years ago

@sterankin This is not related to webdrivers gem. The error (failure reason) is coming from Capybara/Selenium:

Capybara::ElementNotFound:
Unable to find field "username"

It could be a timing issue if the element is found during local execution, but not in Docker. I would recommend taking a screenshot upon test failure to visually confirm if the element is displayed or not when the error happens. You can seek more help here: https://groups.google.com/forum/#!forum/ruby-capybara Good luck!