Skip to content

Commit 3cd1912

Browse files
authored
Fix running Selenium in Docker (#2341)
1 parent 3c6c6de commit 3cd1912

File tree

5 files changed

+65
-6
lines changed

5 files changed

+65
-6
lines changed

Dockerfile

+6-1
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@ RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -\
1515

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

18+
# Install the postgresql client
19+
RUN apt-get update -qq && apt-get install -y postgresql-client
20+
21+
RUN apt-get update && apt-get install -y iproute2
22+
1823
RUN mkdir /app
1924
WORKDIR /app
2025

2126
RUN gem install bundler -v 2.1.4
2227

2328
ENTRYPOINT ["./docker-entrypoint.sh"]
24-
CMD ["bash"]
29+
CMD ["bash"]

config/database.yml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ test:
1717
username: <%= ENV['PSQL_USERNAME'] %>
1818
password: <%= ENV['PSQL_PASSWORD'] %>
1919
min_messages: error
20+
url: <%= ENV['DATABASE_URL'] %>
2021
production:
2122
<<: *default
2223
database: ifme_production

docker-compose.test.yml

+45-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,56 @@
11
version: '3'
22
services:
33
app:
4+
build:
5+
context: .
6+
volumes:
7+
- .:/app
8+
- node_cache:/app/client/node_modules
9+
- bundle_cache:/bundle
410
depends_on:
511
- selenium
12+
- db
613
environment:
714
- SELENIUM_REMOTE_HOST=selenium
15+
- BUNDLE_PATH=/bundle/vendor
816
- RAILS_ENV=test
9-
- RACK_ENV=test
17+
- NODE_ENV=test
18+
- DATABASE_URL=postgres://postgres:password@db:5432/ifme_test
19+
- GITHUB_CLIENT_ID=
20+
- GITHUB_CLIENT_SECRET=
21+
- GOOGLE_CLIENT_ID=
22+
- GOOGLE_CLIENT_SECRET=
23+
tty: true
24+
stdin_open: true
25+
command: /bin/sh -c "wait-for-it db:5432 --timeout=30 -- bundle exec rspec"
26+
27+
db:
28+
image: postgres:9.6-alpine
29+
ports:
30+
- "5432:5432"
31+
volumes:
32+
- postgres_volume:/var/lib/postgresql/data
33+
environment:
34+
- POSTGRES_PASSWORD=password
35+
- POSTGRES_USER=postgres
36+
- POSTGRES_DB=ifme_test
37+
healthcheck:
38+
test: ["CMD", "pg_isready", "-U", "postgres", "-d", "ifme_test", "-h", "localhost"]
39+
interval: 5s
40+
retries: 5
41+
start_period: 5s
42+
timeout: 5s
43+
1044
selenium:
1145
image: selenium/standalone-firefox
1246
container_name: selenium
47+
environment:
48+
- SE_NODE_MAX_INSTANCES=1
49+
- SE_NODE_PORT=5555
50+
- SE_NODE_SESSION_TIMEOUT=300
51+
- SE_NODE_CLEAN_UP_CYCLE=5000
52+
53+
volumes:
54+
bundle_cache:
55+
postgres_volume:
56+
node_cache:

docker-compose.yml

-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ services:
2424
tty: true
2525
stdin_open: true
2626
command: /bin/sh -c "bundle exec foreman start -f Procfile.dev"
27-
28-
2927
db:
3028
image: postgres:9.6-alpine
3129
ports:

spec/spec_helper.rb

+13-2
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,28 @@
2626
ActiveRecord::Migration.maintain_test_schema!
2727

2828
if ENV['SELENIUM_REMOTE_HOST']
29+
# Set up remote Firefox for Capybara
2930
Capybara.javascript_driver = :selenium_remote_firefox
30-
Capybara.register_driver 'selenium_remote_firefox'.to_sym do |app|
31+
32+
Capybara.register_driver :selenium_remote_firefox do |app|
33+
options = Selenium::WebDriver::Firefox::Options.new
34+
options.args << '--headless' # Optional: Run Firefox in headless mode
35+
36+
# Create the remote driver with options set
3137
Capybara::Selenium::Driver.new(
3238
app,
3339
browser: :remote,
3440
url: "http://#{ENV['SELENIUM_REMOTE_HOST']}:4444/wd/hub",
35-
desired_capabilities: :firefox,
41+
options: options, # Ensure options are passed here
3642
timeout: 60
3743
)
3844
end
45+
46+
# Get container's IP address (if needed for server_host setup)
3947
ip = `/sbin/ip route|awk '/scope/ { print $9 }'`.delete("\n")
4048
Capybara.server_host = ip
4149
else
50+
# Fallback if not using remote Selenium
4251
Capybara.javascript_driver = :selenium_chrome_headless
4352
end
4453

@@ -112,3 +121,5 @@
112121
with.library :rails
113122
end
114123
end
124+
125+
DatabaseCleaner.allow_remote_database_url = true

0 commit comments

Comments
 (0)