Skip to content

Commit

Permalink
Improve system tests configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Jun 18, 2020
1 parent d01e621 commit 283cbc1
Show file tree
Hide file tree
Showing 17 changed files with 86 additions and 85 deletions.
31 changes: 0 additions & 31 deletions spec/support/capybara_setup.rb

This file was deleted.

29 changes: 0 additions & 29 deletions spec/support/session_helpers.rb

This file was deleted.

2 changes: 1 addition & 1 deletion spec/system/chat/messaging_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require "rails_helper"
require "system_helper"

describe "Workspaces -> Chat" do
fixtures :workspaces
Expand Down
2 changes: 1 addition & 1 deletion spec/system/lists/create_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require "rails_helper"
require "system_helper"

describe "Workspaces -> New list" do
fixtures :workspaces
Expand Down
2 changes: 1 addition & 1 deletion spec/system/lists/delete_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require "rails_helper"
require "system_helper"

describe "Workspaces -> List -> Delete" do
fixtures :workspaces, :lists
Expand Down
2 changes: 1 addition & 1 deletion spec/system/lists/items_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require "rails_helper"
require "system_helper"

describe "Workspaces -> List -> Items" do
fixtures :workspaces, :lists, :items
Expand Down
2 changes: 1 addition & 1 deletion spec/system/sessions/login_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require "rails_helper"
require "system_helper"

describe "Log in", auth: false do
fixtures :workspaces
Expand Down
2 changes: 1 addition & 1 deletion spec/system/sessions/logout_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require "rails_helper"
require "system_helper"

describe "Log in" do
it "I can logout" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@

# Rails is not flexible enough; at least for now(
module BetterRailsSystemTests
# Use our `Capybara.save_path` to store images (required for CircleCI artifacts)
# Use our `Capybara.save_path` to store screenshots with other capybara artifacts
# (Rails screenshots path is not configurable https://github.com/rails/rails/blob/49baf092439fc74fc3377b12e3334c3dd9d0752f/actionpack/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb#L79)
def absolute_image_path
Rails.root.join("#{Capybara.save_path}/#{image_name}.png")
Rails.root.join("#{Capybara.save_path}/screenshots/#{image_name}.png")
end

# Generate clickable paths for VS Code
def display_image
path = Pathname.new(image_path)
path = path.relative_path_from(Rails.root) unless path.relative?
"🖼 [Screenshot]: #{path}\n"
# Use relative path in screenshot message to make it clickable in VS Code when running in Docker
def image_path
absolute_image_path.relative_path_from(Rails.root).to_s
end

# Make failure screenshots compatible with multi-session setup
def take_screenshot
return super unless Capybara.last_used_session
Capybara.using_session(Capybara.last_used_session) { super }
end
end

Expand All @@ -26,11 +30,12 @@ def display_image
Rails.application.default_url_options[:host] = was_host
end

config.before(:each, type: :system) do
# Make sure this hook runs before others
config.prepend_before(:each, type: :system) do
# Rails sets host to `127.0.0.1` for every test by default.
# That would break in Docker
# NOTE: Fixed in 6.1 (https://github.com/rails/rails/commit/d415eb4f6d6bb24b78b968ae28c22bb7e1721285#diff-9de6fe0bff4847b77cba72441ee855c2)
host! "http://#{Capybara.server_host}"
host! CAPYBARA_APP_HOST
# Use JS driver always
driven_by Capybara.javascript_driver
end
Expand Down
31 changes: 31 additions & 0 deletions spec/system/support/capybara_setup.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

# Capybara settings (not covered by Rails system tests)

# Make server listening on all hosts
Capybara.server_host = "0.0.0.0"
# Use a hostname accessible from the outside world
# NOTE: Rails overrides Capybara.app_host in Rails <6.1, so we have
# to store it differently
CAPYBARA_APP_HOST = "http://#{`hostname`.strip&.downcase || "0.0.0.0"}"
# Capybara.app_host = "http://#{`hostname`.strip&.downcase || "0.0.0.0"}"

# Don't wait too long in `have_xyz` matchers
Capybara.default_max_wait_time = 2

# Normalizes whitespaces when using `has_text?` and similar matchers
Capybara.default_normalize_ws = true

# Where to store artifacts (e.g. screenshots, downloaded files, etc.)
Capybara.save_path = ENV.fetch("CAPYBARA_ARTIFACTS", "./tmp/capybara")

Capybara.singleton_class.prepend(Module.new do
attr_accessor :last_used_session

def using_session(name, &block)
self.last_used_session = name
super
ensure
self.last_used_session = nil
end
end)
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
app,
**{
window_size: [1200, 800],
browser_options: {"no-sandbox" => nil},
browser_options: remote_chrome ? {"no-sandbox" => nil} : {},
inspector: true
}.merge(remote_options)
)
Expand All @@ -49,8 +49,10 @@ def pause
page.driver.pause
end

def debug(*args)
page.driver.debug(*args)
def debug(binding = nil)
$stdout.puts "🔎 Open Chrome inspector at http://localhost:3333"
return binding.pry if binding
page.driver.pause
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

# Precompile assets before running tests to avoid timeouts.
# Do not precompile if webpack-dev-server is running (NOTE: MUST be launched with RAILS_ENV=test)
# rubocop:disable Rails/TimeZone
RSpec.configure do |config|
# Skip assets precompilcation if we exclude system specs.
next if config.filter.opposite.rules[:type] == "system" || config.exclude_pattern.match?(%r{spec/system})
Expand All @@ -15,7 +14,7 @@
$stdout.puts "\n🐢 Precompiling assets.\n"
original_stdout = $stdout.clone
# Use test-prof now 'cause it couldn't be monkey-patched (e.g., by Timecop or similar)
start = Time.now
start = Time.current
begin
# Silence Webpacker output
$stdout.reopen(File.new("/dev/null", "w"))
Expand All @@ -25,9 +24,8 @@
Rake::Task["webpacker:compile"].execute
ensure
$stdout.reopen(original_stdout)
$stdout.puts "Finished in #{(Time.now - start).round(2)} seconds"
$stdout.puts "Finished in #{(Time.current - start).round(2)} seconds"
end
end
end
end
# rubocop:enable Rails/TimeZone
19 changes: 19 additions & 0 deletions spec/system/support/session_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module SessionHelpers
def within_session(...)
Capybara.using_session(...)
end

def mark_all_banners_as_read!
page.driver.set_cookie "show_banners", "N"
end
end

RSpec.configure do |config|
config.include SessionHelpers, type: :system

config.before(type: :system) do
mark_all_banners_as_read!
end
end
2 changes: 1 addition & 1 deletion spec/system/workspaces/create_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require "rails_helper"
require "system_helper"

describe "Workspaces -> New" do
it "I can create a new workspace" do
Expand Down
2 changes: 1 addition & 1 deletion spec/system/workspaces/index_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require "rails_helper"
require "system_helper"

describe "Workspaces" do
fixtures :workspaces
Expand Down
2 changes: 1 addition & 1 deletion spec/system/workspaces/show_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require "rails_helper"
require "system_helper"

describe "Workspaces -> Show" do
fixtures :workspaces
Expand Down
6 changes: 6 additions & 0 deletions spec/system_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

require "rails_helper"

# system/support/ files contain system tests configurations and helpers
Dir[File.join(__dir__, "system/support/**/*.rb")].sort.each { |file| require file }

0 comments on commit 283cbc1

Please sign in to comment.