Skip to content

Commit 493be0d

Browse files
committed
Improve system tests configuration
1 parent d01e621 commit 493be0d

17 files changed

Lines changed: 82 additions & 84 deletions

spec/support/capybara_setup.rb

Lines changed: 0 additions & 31 deletions
This file was deleted.

spec/support/session_helpers.rb

Lines changed: 0 additions & 29 deletions
This file was deleted.

spec/system/chat/messaging_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
require "rails_helper"
3+
require "system_helper"
44

55
describe "Workspaces -> Chat" do
66
fixtures :workspaces

spec/system/lists/create_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
require "rails_helper"
3+
require "system_helper"
44

55
describe "Workspaces -> New list" do
66
fixtures :workspaces

spec/system/lists/delete_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
require "rails_helper"
3+
require "system_helper"
44

55
describe "Workspaces -> List -> Delete" do
66
fixtures :workspaces, :lists

spec/system/lists/items_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
require "rails_helper"
3+
require "system_helper"
44

55
describe "Workspaces -> List -> Items" do
66
fixtures :workspaces, :lists, :items

spec/system/sessions/login_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
require "rails_helper"
3+
require "system_helper"
44

55
describe "Log in", auth: false do
66
fixtures :workspaces

spec/system/sessions/logout_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
require "rails_helper"
3+
require "system_helper"
44

55
describe "Log in" do
66
it "I can logout" do

spec/support/better_rails_system_tests.rb renamed to spec/system/support/better_rails_system_tests.rb

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@
22

33
# Rails is not flexible enough; at least for now(
44
module BetterRailsSystemTests
5-
# Use our `Capybara.save_path` to store images (required for CircleCI artifacts)
5+
# Use our `Capybara.save_path` to store screenshots with other capybara artifacts
66
# (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)
77
def absolute_image_path
8-
Rails.root.join("#{Capybara.save_path}/#{image_name}.png")
8+
Rails.root.join("#{Capybara.save_path}/screenshots/#{image_name}.png")
99
end
1010

11-
# Generate clickable paths for VS Code
12-
def display_image
13-
path = Pathname.new(image_path)
14-
path = path.relative_path_from(Rails.root) unless path.relative?
15-
"🖼 [Screenshot]: #{path}\n"
11+
# Use relative path in screenshot message to make it clickable in VS Code when running in Docker
12+
def image_path
13+
absolute_image_path.relative_path_from(Rails.root).to_s
14+
end
15+
16+
# Make failure screenshots compatible with multi-session setup
17+
def take_screenshot
18+
return super unless Capybara.last_used_session
19+
Capybara.using_session(Capybara.last_used_session) { super }
1620
end
1721
end
1822

@@ -26,7 +30,8 @@ def display_image
2630
Rails.application.default_url_options[:host] = was_host
2731
end
2832

29-
config.before(:each, type: :system) do
33+
# Make sure this hook runs before others
34+
config.prepend_before(:each, type: :system) do
3035
# Rails sets host to `127.0.0.1` for every test by default.
3136
# That would break in Docker
3237
# NOTE: Fixed in 6.1 (https://github.com/rails/rails/commit/d415eb4f6d6bb24b78b968ae28c22bb7e1721285#diff-9de6fe0bff4847b77cba72441ee855c2)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
# Capybara settings (not covered by Rails system tests)
4+
5+
# Use a hostname accessible from the outside world
6+
# (Required for running tests in Docker)
7+
Capybara.server_host = `hostname`.strip&.downcase || "0.0.0.0"
8+
Capybara.app_host = "http://#{Capybara.server_host}"
9+
10+
# Don't wait too long in `have_xyz` matchers
11+
Capybara.default_max_wait_time = 2
12+
13+
# Normalizes whitespaces when using `has_text?` and similar matchers
14+
Capybara.default_normalize_ws = true
15+
16+
# Where to store artifacts (e.g. screenshots, downloaded files, etc.)
17+
Capybara.save_path = ENV.fetch("CAPYBARA_ARTIFACTS", "./tmp/capybara")
18+
19+
Capybara.singleton_class.prepend(Module.new do
20+
attr_accessor :last_used_session
21+
22+
def using_session(name, &block)
23+
self.last_used_session = name
24+
super
25+
ensure
26+
self.last_used_session = nil
27+
end
28+
end)

0 commit comments

Comments
 (0)