Skip to content

Commit

Permalink
Fix running Selenium in Docker (#2341)
Browse files Browse the repository at this point in the history
  • Loading branch information
julianguyen authored Dec 9, 2024
1 parent 3c6c6de commit 3cd1912
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 6 deletions.
7 changes: 6 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -\

RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Install the postgresql client
RUN apt-get update -qq && apt-get install -y postgresql-client

RUN apt-get update && apt-get install -y iproute2

RUN mkdir /app
WORKDIR /app

RUN gem install bundler -v 2.1.4

ENTRYPOINT ["./docker-entrypoint.sh"]
CMD ["bash"]
CMD ["bash"]
1 change: 1 addition & 0 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ test:
username: <%= ENV['PSQL_USERNAME'] %>
password: <%= ENV['PSQL_PASSWORD'] %>
min_messages: error
url: <%= ENV['DATABASE_URL'] %>
production:
<<: *default
database: ifme_production
Expand Down
46 changes: 45 additions & 1 deletion docker-compose.test.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,56 @@
version: '3'
services:
app:
build:
context: .
volumes:
- .:/app
- node_cache:/app/client/node_modules
- bundle_cache:/bundle
depends_on:
- selenium
- db
environment:
- SELENIUM_REMOTE_HOST=selenium
- BUNDLE_PATH=/bundle/vendor
- RAILS_ENV=test
- RACK_ENV=test
- NODE_ENV=test
- DATABASE_URL=postgres://postgres:password@db:5432/ifme_test
- GITHUB_CLIENT_ID=
- GITHUB_CLIENT_SECRET=
- GOOGLE_CLIENT_ID=
- GOOGLE_CLIENT_SECRET=
tty: true
stdin_open: true
command: /bin/sh -c "wait-for-it db:5432 --timeout=30 -- bundle exec rspec"

db:
image: postgres:9.6-alpine
ports:
- "5432:5432"
volumes:
- postgres_volume:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=password
- POSTGRES_USER=postgres
- POSTGRES_DB=ifme_test
healthcheck:
test: ["CMD", "pg_isready", "-U", "postgres", "-d", "ifme_test", "-h", "localhost"]
interval: 5s
retries: 5
start_period: 5s
timeout: 5s

selenium:
image: selenium/standalone-firefox
container_name: selenium
environment:
- SE_NODE_MAX_INSTANCES=1
- SE_NODE_PORT=5555
- SE_NODE_SESSION_TIMEOUT=300
- SE_NODE_CLEAN_UP_CYCLE=5000

volumes:
bundle_cache:
postgres_volume:
node_cache:
2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ services:
tty: true
stdin_open: true
command: /bin/sh -c "bundle exec foreman start -f Procfile.dev"


db:
image: postgres:9.6-alpine
ports:
Expand Down
15 changes: 13 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,28 @@
ActiveRecord::Migration.maintain_test_schema!

if ENV['SELENIUM_REMOTE_HOST']
# Set up remote Firefox for Capybara
Capybara.javascript_driver = :selenium_remote_firefox
Capybara.register_driver 'selenium_remote_firefox'.to_sym do |app|

Capybara.register_driver :selenium_remote_firefox do |app|
options = Selenium::WebDriver::Firefox::Options.new
options.args << '--headless' # Optional: Run Firefox in headless mode

# Create the remote driver with options set
Capybara::Selenium::Driver.new(
app,
browser: :remote,
url: "http://#{ENV['SELENIUM_REMOTE_HOST']}:4444/wd/hub",
desired_capabilities: :firefox,
options: options, # Ensure options are passed here
timeout: 60
)
end

# Get container's IP address (if needed for server_host setup)
ip = `/sbin/ip route|awk '/scope/ { print $9 }'`.delete("\n")
Capybara.server_host = ip
else
# Fallback if not using remote Selenium
Capybara.javascript_driver = :selenium_chrome_headless
end

Expand Down Expand Up @@ -112,3 +121,5 @@
with.library :rails
end
end

DatabaseCleaner.allow_remote_database_url = true

0 comments on commit 3cd1912

Please sign in to comment.