Skip to content

Instantly share code, notes, and snippets.

@jfroom
Last active March 15, 2017 20:48
Show Gist options
  • Save jfroom/0ab0f96fa052ce9c4d3c2d29bdad066f to your computer and use it in GitHub Desktop.
Save jfroom/0ab0f96fa052ce9c4d3c2d29bdad066f to your computer and use it in GitHub Desktop.
DC-Selenium
# ./Dockerfile
FROM ruby:2.3-slim
RUN apt-get update && apt-get install -qq -y --no-install-recommends \
build-essential nodejs
# Ensure that our apt package list is updated and install a few
# packages to ensure that we can compile assets (nodejs).
RUN mkdir -p /app
WORKDIR /app
COPY . .
# Add app files into docker image
COPY ./docker-entrypoint.sh /
RUN chmod +x /docker-entrypoint.sh
ENTRYPOINT ["/docker-entrypoint.sh"]
# Add bundle entry point to handle bundle cache
ENV BUNDLE_PATH=/bundle \
BUNDLE_BIN=/bundle/bin \
GEM_HOME=/bundle
ENV PATH="${BUNDLE_BIN}:${PATH}"
# Bundle installs with binstubs to our custom /bundle/bin volume path. Let system use those stubs.
# ./docker-compose.yml
version: '3.1'
services:
web:
build: .
command: >
bash -c "
RAILS_ENV=test puma -b tcp://0.0.0.0:3001 -d &&
# Run test server detached
puma -C config/puma.rb
# Run web server
"
ports: ['3000:3000', '3001:3001']
# Bind service ports to host machine.
environment:
- SELENIUM_HOST=selenium
- SELENIUM_PORT=4444
- TEST_APP_HOST=web
- TEST_PORT=3001
# See ./test/test_helper.rb for Capybara & Selenium webdriver config
volumes:
- bundle_cache:/bundle
# Map bundle cache to named volume
- .:/app
# Map /app files back to host drive, and visa versa
stdin_open: true
tty: true
# Allow interactive byebug sessions.
selenium:
image: selenium/standalone-chrome-debug:3.0.1-germanium
# Debug version enables VNC ability
ports: ['4444:4444', '5900:5900']
# Bind selenium port & VNC port
logging:
driver: none
# Disable noisy logs.
volumes:
bundle_cache:
# Mount volume with default driver
# ./docker-entrypoint.sh
#!/bin/bash
# Bash script identifier
set -e
# Exit on fail
bundle check || bundle install --binstubs="$BUNDLE_BIN"
# Ensure all gems installed. Add binstubs to bin which has been added to PATH in Dockerfile.
exec "$@"
# Finally call command issued to the docker service
# ./Gemfile
source 'https://rubygems.org'
ruby '2.3.3'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.2'
# Use Puma as the app server
gem 'puma', '~> 3.0'
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platform: :mri
end
group :test do
gem 'selenium-webdriver', '~> 3.0'
gem 'minitest-rails-capybara', '~> 3.0'
end
# ./test/test_helper.rb
# Boilerplate Rails test code
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
# Capybara config with docker-compose environment vars
require 'minitest/rails/capybara'
Capybara.app_host = "http://#{ENV['TEST_APP_HOST']}:#{ENV['TEST_PORT']}"
Capybara.javascript_driver = :selenium
Capybara.run_server = false
# Configure the Chrome driver capabilities & register
args = ['--no-default-browser-check', '--start-maximized']
caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {"args" => args})
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(
app,
browser: :remote,
url: "http://#{ENV['SELENIUM_HOST']}:#{ENV['SELENIUM_PORT']}/wd/hub",
desired_capabilities: caps
)
end
# ./config/routes.rb
Rails.application.routes.draw do
root "welcome#index"
end
# ./app/controllers/welcome_cotnroller.rb
class WelcomeController < ApplicationController
# Empty controller will automatically load 'index' view.
end
# ./app/views/welcome/index.html.erb
<h1>Hello World!</h1>
# ./test/acceptance/welcome_page.rb
require 'test_helper'
describe "Can Access Home", :capybara do
it "has content", js: true do
visit root_path
page.must_have_content "Hello World!"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment