Skip to content

Commit

Permalink
Establish system test capability
Browse files Browse the repository at this point in the history
  • Loading branch information
dinjas committed Feb 9, 2019
1 parent 08e8979 commit b107215
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 9 deletions.
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ group :development do
gem 'hint-rubocop_style'
end

group :test do
gem 'capybara', '>= 2.15'
gem 'selenium-webdriver'
gem 'chromedriver-helper'
end


# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
26 changes: 26 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ GEM
momentjs-rails (~> 2.8)
sass-rails (~> 5.0)
selectize-rails (~> 0.6)
archive-zip (0.11.0)
io-like (~> 0.3.0)
arel (9.0.0)
ast (2.4.0)
autoprefixer-rails (9.4.4)
Expand All @@ -79,6 +81,19 @@ GEM
msgpack (~> 1.0)
builder (3.2.3)
byebug (10.0.2)
capybara (3.13.2)
addressable
mini_mime (>= 0.1.3)
nokogiri (~> 1.8)
rack (>= 1.6.0)
rack-test (>= 0.6.3)
regexp_parser (~> 1.2)
xpath (~> 3.2)
childprocess (0.9.0)
ffi (~> 1.0, >= 1.0.11)
chromedriver-helper (2.1.0)
archive-zip (~> 0.10)
nokogiri (~> 1.8)
coderay (1.1.2)
concord (0.1.5)
adamantium (~> 0.2.0)
Expand Down Expand Up @@ -134,6 +149,7 @@ GEM
i18n (1.5.1)
concurrent-ruby (~> 1.0)
ice_nine (0.11.2)
io-like (0.3.0)
jaro_winkler (1.5.2)
jbuilder (2.8.0)
activesupport (>= 4.2.0)
Expand Down Expand Up @@ -247,6 +263,7 @@ GEM
execjs
railties (>= 3.2)
tilt
regexp_parser (1.3.0)
responders (2.4.0)
actionpack (>= 4.2.0, < 5.3)
railties (>= 4.2.0, < 5.3)
Expand Down Expand Up @@ -285,6 +302,7 @@ GEM
ruby_dep (1.5.0)
ruby_parser (3.12.0)
sexp_processor (~> 4.9)
rubyzip (1.2.2)
safe_yaml (1.0.4)
sass (3.7.3)
sass-listen (~> 4.0.0)
Expand All @@ -298,6 +316,9 @@ GEM
sprockets-rails (>= 2.0, < 4.0)
tilt (>= 1.1, < 3)
selectize-rails (0.12.6)
selenium-webdriver (3.141.0)
childprocess (~> 0.5)
rubyzip (~> 1.2, >= 1.2.2)
sexp_processor (4.11.0)
shoulda-matchers (3.1.2)
activesupport (>= 4.0.0)
Expand Down Expand Up @@ -349,6 +370,8 @@ GEM
websocket-driver (0.7.0)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.3)
xpath (3.2.0)
nokogiri (~> 1.8)

PLATFORMS
ruby
Expand All @@ -358,6 +381,8 @@ DEPENDENCIES
administrate
bootsnap (>= 1.1.0)
byebug
capybara (>= 2.15)
chromedriver-helper
concord
devise
devise_invitable
Expand All @@ -381,6 +406,7 @@ DEPENDENCIES
rrule
rspec-rails
rspec_junit_formatter
selenium-webdriver
shoulda-matchers (~> 3.1)
simplecov
spring
Expand Down
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* `docker-compose run app bash` Starts a bash session on app, bringing up only dependent services.
* `docker exec -it ID_FROM_DOCKER_PS bash`. Connects another bash session to a running container.
* `docker attach ID_FROM_DOCKER_PS` Attach is useful for pry debugging in a running container
> Note: To dettach use `ctrl-p + ctrl-q`
> Note: To detach use `ctrl-p + ctrl-q`
### Rails Commands in a Docker World
Now that the app is running in Docker we will run all Rails and Rake commands in the container.
Expand All @@ -41,4 +41,16 @@ Here are a few examples:
The pattern is docker-compose exec (container_name) rails or rake command.
This pattern works for non-Rails commands also.

* `docker-compose exec app bash` will open the terminal on the container
* `docker-compose exec app bash` will open the terminal on the container

### System Tests

System tests open the browser and make assertions against the content of the
page or verify expected behavior. These tests can be run in headless mode (the
default), which means that they are executed in a virtual browser. If you would
like them to be run in an actual, viewable browser, you will need to disable
headless mode by setting the HEADLESS environment variable to 'false' and ensure
you have the [ChromeDriver WebDriver](https://sites.google.com/a/chromium.org/chromedriver/downloads)
downloaded and installed on your host machine.

E.g. `HEADLESS=false bundle exec rspec spec/system/mytest_spec.rb`
7 changes: 7 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ services:
ports:
- "3000:3000"
- "1080:1080"
- "43447:43447"
depends_on:
- postgres
entrypoint: /app/dev-entrypoint
Expand All @@ -18,6 +19,12 @@ services:
- WEBPACKER_DEV_SERVER_HOST=webpacker
- SEED_ADMIN_EMAIL=${SEED_ADMIN_EMAIL}
- SEED_ADMIN_PASSWORD=${SEED_ADMIN_PASSWORD}
- SELENIUM_REMOTE_HOST=selenium
selenium:
image: selenium/standalone-chrome
ports:
- 4444:4444
- 5900:5900
webpacker:
build: .
command: ./bin/webpack-dev-server
Expand Down
2 changes: 1 addition & 1 deletion spec/models/address_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@
end
end
end
end
end
13 changes: 7 additions & 6 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
require 'support/factory_bot'
require 'support/capybara'
require 'support/devise'
require 'support/factory_bot'
require 'pundit/matchers'

include WebMock::API

VCR.configure do |config|
config.cassette_library_dir = "spec/fixtures/vcr_cassettes"
config.hook_into :webmock
config.configure_rspec_metadata!
end
# VCR.configure do |config|
# config.cassette_library_dir = "spec/fixtures/vcr_cassettes"
# config.hook_into :webmock
# config.configure_rspec_metadata!
# end

# Globally stub smartystreets
stub_request(:any, /smartystreets.com/).to_return(
Expand Down
46 changes: 46 additions & 0 deletions spec/support/capybara.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

RSpec.configure do |config|
headless = ENV.fetch('HEADLESS', true) != 'false'

config.before(:each, type: :system) do
driven_by :rack_test
end

config.before :each, type: :system, js: true do
# Docker environment
url = if headless
"http://#{ENV['SELENIUM_REMOTE_HOST']}:4444/wd/hub"
else
'http://host.docker.internal:9515'
end

driven_by :selenium, using: :chrome, options: {
browser: :remote,
url: url,
desired_capabilities: :chrome
}

# Find Docker IP address
Capybara.server_host = if headless
`/sbin/ip route|awk '/scope/ { print $9 }'`.strip
else
'0.0.0.0'
end
Capybara.server_port = '43447'
Capybara.app_host = "http://#{Capybara.current_session.server.host}:#{Capybara.current_session.server.port}"
end

config.after :each, type: :system, js: true do
page.driver.browser.manage.logs.get(:browser).each do |log|
case log.message
when /This page includes a password or credit card input in a non-secure context/
# Ignore this warning in tests
next
else
message = "[#{log.level}] #{log.message}"
raise message
end
end
end
end
15 changes: 15 additions & 0 deletions spec/system/something_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe "Something", type: :system, js: true do

describe 'home page' do
it 'does something' do
visit '/'
expect(page).to have_text('Log In')
end
end
end


0 comments on commit b107215

Please sign in to comment.