Skip to content

Commit 1dcac3e

Browse files
committed
Add basic cypress test
1 parent b45381d commit 1dcac3e

15 files changed

Lines changed: 336 additions & 4 deletions

File tree

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,4 @@ group :test do
6767
end
6868

6969
gem "minitest", "~> 5.0"
70+
gem "cypress-on-rails", "~> 1.0"

Gemfile.lock

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ GEM
102102
concurrent-ruby (1.3.6)
103103
connection_pool (3.0.2)
104104
crass (1.0.6)
105+
cypress-on-rails (1.20.0)
106+
rack
105107
date (3.5.1)
106108
debug (1.11.1)
107109
irb (~> 1.10)
@@ -392,6 +394,7 @@ DEPENDENCIES
392394
brakeman
393395
bundler-audit
394396
capybara
397+
cypress-on-rails (~> 1.0)
395398
debug
396399
image_processing (~> 1.2)
397400
importmap-rails
@@ -441,9 +444,10 @@ CHECKSUMS
441444
concurrent-ruby (1.3.6) sha256=6b56837e1e7e5292f9864f34b69c5a2cbc75c0cf5338f1ce9903d10fa762d5ab
442445
connection_pool (3.0.2) sha256=33fff5ba71a12d2aa26cb72b1db8bba2a1a01823559fb01d29eb74c286e62e0a
443446
crass (1.0.6) sha256=dc516022a56e7b3b156099abc81b6d2b08ea1ed12676ac7a5657617f012bd45d
447+
cypress-on-rails (1.20.0) sha256=36e79d77d073f4b83d73a275b4908cb838e103adbad305882a683db0eb2ca7e4
444448
date (3.5.1) sha256=750d06384d7b9c15d562c76291407d89e368dda4d4fff957eb94962d325a0dc0
445449
debug (1.11.1) sha256=2e0b0ac6119f2207a6f8ac7d4a73ca8eb4e440f64da0a3136c30343146e952b6
446-
docile (1.4.1)
450+
docile (1.4.1) sha256=96159be799bfa73cdb721b840e9802126e4e03dfc26863db73647204c727f21e
447451
dotenv (3.2.0) sha256=e375b83121ea7ca4ce20f214740076129ab8514cd81378161f11c03853fe619d
448452
drb (2.2.3) sha256=0b00d6fdb50995fe4a45dea13663493c841112e4068656854646f418fda13373
449453
ed25519 (1.4.0) sha256=16e97f5198689a154247169f3453ef4cfd3f7a47481fde0ae33206cdfdcac506
@@ -527,9 +531,9 @@ CHECKSUMS
527531
rubyzip (3.2.2) sha256=c0ed99385f0625415c8f05bcae33fe649ed2952894a95ff8b08f26ca57ea5b3c
528532
securerandom (0.4.1) sha256=cc5193d414a4341b6e225f0cb4446aceca8e50d5e1888743fac16987638ea0b1
529533
selenium-webdriver (4.39.0) sha256=984a1e63d39472eaf286bac3c6f1822fa7eea6eed9c07a66ce7b3bc5417ba826
530-
simplecov (0.22.0)
531-
simplecov-html (0.13.2)
532-
simplecov_json_formatter (0.1.4)
534+
simplecov (0.22.0) sha256=fe2622c7834ff23b98066bb0a854284b2729a569ac659f82621fc22ef36213a5
535+
simplecov-html (0.13.2) sha256=bd0b8e54e7c2d7685927e8d6286466359b6f16b18cb0df47b508e8d73c777246
536+
simplecov_json_formatter (0.1.4) sha256=529418fbe8de1713ac2b2d612aa3daa56d316975d307244399fa4838c601b428
533537
solid_cable (3.0.12) sha256=a168a54731a455d5627af48d8441ea3b554b8c1f6e6cd6074109de493e6b0460
534538
solid_cache (1.0.10) sha256=bc05a2fb3ac78a6f43cbb5946679cf9db67dd30d22939ededc385cb93e120d41
535539
solid_queue (1.2.4) sha256=bb60f9552a969ac377d87601b0ff6a088f5e6f20b0cbbe3844a59d022cac0e4b
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
if defined?(CypressOnRails)
2+
CypressOnRails.configure do |c|
3+
c.api_prefix = ""
4+
c.install_folder = File.expand_path("#{__dir__}/../../e2e")
5+
6+
c.server_readiness_path = "/users/new"
7+
# WARNING!! CypressOnRails can execute arbitrary ruby code
8+
# please use with extra caution if enabling on hosted servers or starting your local server on 0.0.0.0
9+
c.use_middleware = !Rails.env.production?
10+
# c.use_vcr_middleware = !Rails.env.production?
11+
# # Use this if you want to use use_cassette wrapper instead of manual insert/eject
12+
# # c.use_vcr_use_cassette_middleware = !Rails.env.production?
13+
# # Pass custom VCR options
14+
# c.vcr_options = {
15+
# hook_into: :webmock,
16+
# default_cassette_options: { record: :once },
17+
# cassette_library_dir: File.expand_path("#{__dir__}/../../e2e/fixtures/vcr_cassettes")
18+
# }
19+
c.logger = Rails.logger
20+
21+
# Server configuration for rake tasks (cypress:open, cypress:run, playwright:open, playwright:run)
22+
# c.server_host = 'localhost' # or use ENV['CYPRESS_RAILS_HOST']
23+
# c.server_port = 3001 # or use ENV['CYPRESS_RAILS_PORT']
24+
# c.transactional_server = true # Enable automatic transaction rollback between tests
25+
26+
# Server lifecycle hooks for rake tasks
27+
# c.before_server_start = -> { DatabaseCleaner.clean_with(:truncation) }
28+
# c.after_server_start = -> { puts "Test server started on port #{CypressOnRails.configuration.server_port}" }
29+
# c.after_transaction_start = -> { Rails.application.load_seed }
30+
# c.after_state_reset = -> { Rails.cache.clear }
31+
# c.before_server_stop = -> { puts "Stopping test server..." }
32+
33+
# If you want to enable a before_request logic, such as authentication, logging, sending metrics, etc.
34+
# Refer to https://www.rubydoc.info/gems/rack/Rack/Request for the `request` argument.
35+
# Return nil to continue through the Cypress command. Return a response [status, header, body] to halt.
36+
# c.before_request = lambda { |request|
37+
# unless request.env['warden'].authenticate(:secret_key)
38+
# return [403, {}, ["forbidden"]]
39+
# end
40+
# }
41+
end
42+
43+
# # if you compile your asssets on CI
44+
# if ENV['CYPRESS'].present? && ENV['CI'].present?
45+
# Rails.application.configure do
46+
# config.assets.compile = false
47+
# config.assets.unknown_asset_fallback = false
48+
# end
49+
# end
50+
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# you can delete this file if you don't use Rails Test Fixtures
2+
3+
fixtures_dir = command_options.try(:[], 'fixtures_dir')
4+
fixture_files = command_options.try(:[], 'fixtures')
5+
6+
if defined?(ActiveRecord)
7+
require "active_record/fixtures"
8+
9+
fixtures_dir ||= ActiveRecord::Tasks::DatabaseTasks.fixtures_path
10+
fixture_files ||= Dir["#{fixtures_dir}/**/*.yml"].map { |f| f[(fixtures_dir.size + 1)..-5] }
11+
12+
logger.debug "loading fixtures: { dir: #{fixtures_dir}, files: #{fixture_files} }"
13+
ActiveRecord::FixtureSet.reset_cache
14+
ActiveRecord::FixtureSet.create_fixtures(fixtures_dir, fixture_files)
15+
"Fixtures Done" # this gets returned
16+
else # this else part can be removed
17+
logger.error "Looks like activerecord_fixtures has to be modified to suite your need"
18+
User.create(name: 'User1', lastname: "Lastname1")
19+
User.create(name: 'User2', lastname: "Lastname2")
20+
User.create(name: 'User3', lastname: "Lastname3")
21+
User.create(name: 'User4', lastname: "Lastname4")
22+
end

e2e/app_commands/clean.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
if defined?(DatabaseCleaner)
2+
# cleaning the database using database_cleaner
3+
DatabaseCleaner.strategy = :truncation
4+
DatabaseCleaner.clean
5+
else
6+
logger.warn "add database_cleaner or update cypress/app_commands/clean.rb"
7+
Post.delete_all if defined?(Post)
8+
end
9+
10+
CypressOnRails::SmartFactoryWrapper.reload
11+
12+
if defined?(VCR)
13+
VCR.eject_cassette # make sure we no cassette inserted before the next test starts
14+
VCR.turn_off!
15+
WebMock.disable! if defined?(WebMock)
16+
end
17+
18+
Rails.logger.info "APPCLEANED" # used by log_fail.rb

e2e/app_commands/eval.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Kernel.eval(command_options) unless command_options.nil?

e2e/app_commands/factory_bot.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Array.wrap(command_options).map do |factory_options|
2+
factory_method = factory_options.shift
3+
begin
4+
logger.debug "running #{factory_method}, #{factory_options}"
5+
CypressOnRails::SmartFactoryWrapper.public_send(factory_method, *factory_options)
6+
rescue => e
7+
logger.error "#{e.class}: #{e.message}"
8+
logger.error e.backtrace.join("\n")
9+
logger.error "#{e.record.inspect}" if e.is_a?(ActiveRecord::RecordInvalid)
10+
raise e
11+
end
12+
end

e2e/app_commands/log_fail.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# This file is called when a cypress spec fails and allows for extra logging to be captured
2+
filename = command_options.fetch('runnable_full_title', 'no title').gsub(/[^[:print:]]/, '')
3+
4+
# grab last lines until "APPCLEANED" (Make sure in clean.rb to log the text "APPCLEANED")
5+
system "tail -n 10000 -r log/#{Rails.env}.log | sed \"/APPCLEANED/ q\" | sed 'x;1!H;$!d;x' > 'log/#{filename}.log'"
6+
# Alternative command if the above does not work
7+
# system "tail -n 10000 log/#{Rails.env}.log | tac | sed \"/APPCLEANED/ q\" | sed 'x;1!H;$!d;x' > 'log/#{filename}.log'"
8+
9+
# create a json debug file for server debugging
10+
json_result = {}
11+
json_result['error'] = command_options.fetch('error_message', 'no error message')
12+
13+
if defined?(ActiveRecord::Base)
14+
json_result['records'] =
15+
ActiveRecord::Base.descendants.each_with_object({}) do |record_class, records|
16+
begin
17+
records[record_class.to_s] = record_class.limit(100).map(&:attributes)
18+
rescue
19+
end
20+
end
21+
end
22+
23+
filename = command_options.fetch('runnable_full_title', 'no title').gsub(/[^[:print:]]/, '')
24+
File.open("#{Rails.root}/log/#{filename}.json", "w+") do |file|
25+
file << JSON.pretty_generate(json_result)
26+
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# You can setup your Rails state here
2+
# MyModel.create name: 'something'
3+
User.create(name: "User", lastname: "Lastname")

e2e/cypress.config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const { defineConfig } = require('cypress')
2+
3+
module.exports = defineConfig({
4+
e2e: {
5+
baseUrl: "http://localhost:5017",
6+
defaultCommandTimeout: 10000,
7+
supportFile: "cypress/support/index.js",
8+
}
9+
})

0 commit comments

Comments
 (0)