Skip to content

Commit

Permalink
Update install generator and remove update generator (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
grantspeelman committed Jan 4, 2024
1 parent 7b54a14 commit 92ed2fd
Show file tree
Hide file tree
Showing 15 changed files with 228 additions and 99 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ vendor/bundle
.vscode
node_modules
package-lock.json
yarn.lock
yarn.lock
specs_e2e/server.pid
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## [Unreleased]

### Changed
* Removed the update generator and reduced options for install generator [PR 149](https://github.com/shakacode/cypress-on-rails/pull/149)

## [1.16.0]
[Compare]: https://github.com/shakacode/cypress-on-rails/compare/v1.15.1...v1.16.0

Expand Down
47 changes: 21 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,46 +61,43 @@ Generate the boilerplate code using:
# by default installs only cypress
bin/rails g cypress_on_rails:install

# if you have/want a different cypress folder (default is cypress)
bin/rails g cypress_on_rails:install --cypress_folder=spec/cypress
# if you have/want a different cypress folder (default is e2e)
bin/rails g cypress_on_rails:install --install_folder=spec/cypress

# to install both cypress and playwright
bin/rails g cypress_on_rails:install --install_cypress --install_playwright --playwright_folder=playwright

# to change where the Ruby files reside (default is e2e)
bin/rails g cypress_on_rails:install --install_folder=test/e2e
# to install playwright instead of cypress
bin/rails g cypress_on_rails:install --framework playwright

# if you target the Rails server with a path prefix to your URL
bin/rails g cypress_on_rails:install --api_prefix=/api

# if you want to install cypress with npm
# if you want to install with npm instead
bin/rails g cypress_on_rails:install --install_with=npm

# if you already have cypress installed globally
bin/rails g cypress_on_rails:install --no-install-cypress
bin/rails g cypress_on_rails:install --install_with=skip

# to update the generated files run
bin/rails g cypress_on_rails:update
bin/rails g cypress_on_rails:install --install_with=skip
```

The generator modifies/adds the following files/directory in your application:
* `config/initializers/cypress_on_rails.rb` used to configure Cypress on Rails
* `spec/cypress/e2e/` contains your cypress tests
* `spec/playwright/e2e/` contains your playwright tests
* `spec/cypress/support/on-rails.js` contains Cypress on Rails support code
* `spec/playwright/support/on-rails.js` contains Playwright on Rails support code
* `spec/e2e/app_commands/scenarios/` contains your Cypress on Rails scenario definitions
* `spec/e2e/cypress_helper.rb` contains helper code for Cypress on Rails app commands
* `e2e/cypress/integration/` contains your cypress tests
* `e2e/cypress/support/on-rails.js` contains Cypress on Rails support code
* `e2e/cypress/e2e_helper.rb` contains helper code to require libraries like factory_bot
* `e2e/cypress/app_commands/` contains your scenario definitions
* `e2e/playwright/e2e/` contains your playwright tests
* `e2e/playwright/support/on-rails.js` contains Playwright on Rails support code

If you are not using `database_cleaner` look at `spec/e2e/app_commands/clean.rb`.
If you are not using `factory_bot` look at `spec/e2e/app_commands/factory_bot.rb`.
If you are not using `database_cleaner` look at `e2e/cypress/app_commands/clean.rb`.
If you are not using `factory_bot` look at `e2e/cypress/app_commands/factory_bot.rb`.

Now you can create scenarios and commands that are plain Ruby files that get loaded through middleware, the ruby sky is your limit.

### Update your database.yml

When running `cypress test` or `playwright test` on your local computer it's recommended to start your server in development mode so that changes you
make are picked up without having to restart the server.
When writing and running tests on your local computer it's recommended to start your server in development mode so that changes you
make are picked up without having to restart your local server.
It's recommended you update your `database.yml` to check if the `CYPRESS` environment variable is set and switch it to the test database
otherwise cypress will keep clearing your development database.

Expand All @@ -127,11 +124,9 @@ Getting started on your local environment
CYPRESS=1 bin/rails server -p 5017

# in separate window start cypress
yarn cypress open
yarn cypress open --project ./e2e
# or for npm
node_modules/.bin/cypress open
# or if you changed the cypress folder to spec/cypress
yarn cypress open --project ./spec
npx cypress open --project ./e2e
# or for playwright
yarn playwright test --ui
# or using npm
Expand All @@ -144,9 +139,9 @@ How to run cypress on CI
# setup rails and start server in background
# ...

yarn run cypress run
yarn run cypress run --project ./e2e
# or for npm
npx cypress run
npx cypress run --project ./e2e
```

### Example of using factory bot
Expand Down
2 changes: 1 addition & 1 deletion cypress-on-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'rake'
s.add_development_dependency 'rspec'
s.add_development_dependency 'railties', '>= 3.2'
s.add_development_dependency 'factory_bot'
s.add_development_dependency 'factory_bot', '!= 6.4.5'
s.add_development_dependency 'vcr'
s.metadata = {
"bug_tracker_uri" => "https://github.com/shakacode/cypress-on-rails/issues",
Expand Down
44 changes: 21 additions & 23 deletions lib/generators/cypress_on_rails/install_generator.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
module CypressOnRails
class InstallGenerator < Rails::Generators::Base
class_option :api_prefix, type: :string, default: ''
class_option :framework, type: :string, default: 'cypress'
class_option :install_folder, type: :string, default: 'e2e'
class_option :install_cypress, type: :boolean, default: true
class_option :install_playwright, type: :boolean, default: false
class_option :install_with, type: :string, default: 'yarn'
class_option :cypress_folder, type: :string, default: 'cypress'
class_option :playwright_folder, type: :string, default: 'playwright'
class_option :experimental, type: :boolean, default: false
source_root File.expand_path('../templates', __FILE__)

Expand All @@ -17,10 +14,11 @@ def install_framework

command = nil
packages = []

packages << 'cypress' if options.install_cypress
packages.push('playwright', '@playwright/test') if options.install_playwright

packages = if options.framework == 'cypress'
['cypress', 'cypress-on-rails']
elsif options.framework == 'playwright'
['playwright', '@playwright/test']
end
if options.install_with == 'yarn'
command = "yarn --cwd=#{install_dir} add #{packages.join(' ')} --dev"
elsif options.install_with == 'npm'
Expand All @@ -31,28 +29,28 @@ def install_framework
fail "failed to install #{packages.join(' ')}" unless system(command)
end

if options.install_cypress
template "spec/cypress/support/index.js.erb", "#{options.cypress_folder}/support/index.js"
copy_file "spec/cypress/support/commands.js", "#{options.cypress_folder}/support/commands.js"
copy_file "spec/cypress.config.js", "#{options.cypress_folder}/../cypress.config.js"
if options.framework == 'cypress'
template "spec/cypress/support/index.js.erb", "#{options.install_folder}/cypress/support/index.js"
copy_file "spec/cypress/support/commands.js", "#{options.install_folder}/cypress/support/commands.js"
copy_file "spec/cypress.config.js", "#{options.install_folder}/cypress.config.js"
end
if options.install_playwright
template "spec/playwright/support/index.js.erb", "#{options.playwright_folder}/support/index.js"
copy_file "spec/playwright.config.js", "#{options.playwright_folder}/../playwright.config.js"
if options.framework == 'playwright'
template "spec/playwright/support/index.js.erb", "#{options.install_folder}/playwright/support/index.js"
copy_file "spec/playwright.config.js", "#{options.install_folder}/playwright.config.js"
end
end

def add_initial_files
template "config/initializers/cypress_on_rails.rb.erb", "config/initializers/cypress_on_rails.rb"
template "spec/e2e/e2e_helper.rb.erb", "#{options.install_folder}/e2e_helper.rb"
directory 'spec/e2e/app_commands', "#{options.install_folder}/app_commands"
if options.install_cypress
copy_file "spec/cypress/support/on-rails.js", "#{options.cypress_folder}/support/on-rails.js"
directory 'spec/cypress/e2e/rails_examples', "#{options.cypress_folder}/e2e/rails_examples"
template "spec/e2e/e2e_helper.rb.erb", "#{options.install_folder}/#{options.framework}/e2e_helper.rb"
directory 'spec/e2e/app_commands', "#{options.install_folder}/#{options.framework}/app_commands"
if options.framework == 'cypress'
copy_file "spec/cypress/support/on-rails.js", "#{options.install_folder}/cypress/support/on-rails.js"
directory 'spec/cypress/e2e/rails_examples', "#{options.install_folder}/cypress/e2e/rails_examples"
end
if options.install_playwright
copy_file "spec/playwright/support/on-rails.js", "#{options.playwright_folder}/support/on-rails.js"
directory 'spec/playwright/e2e/rails_examples', "#{options.playwright_folder}/e2e/rails_examples"
if options.framework == 'playwright'
copy_file "spec/playwright/support/on-rails.js", "#{options.install_folder}/playwright/support/on-rails.js"
directory 'spec/playwright/e2e/rails_examples', "#{options.install_folder}/playwright/e2e/rails_examples"
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
if defined?(CypressOnRails)
CypressOnRails.configure do |c|
c.api_prefix = "<%= options.api_prefix %>"
c.install_folder = File.expand_path("#{__dir__}/../../<%= options.install_folder %>")
c.install_folder = File.expand_path("#{__dir__}/../../<%= options.install_folder %>/<%= options.framework %>")
# WARNING!! CypressOnRails can execute arbitrary ruby code
# please use with extra caution if enabling on hosted servers or starting your local server on 0.0.0.0
c.use_middleware = !Rails.env.production?
Expand Down
24 changes: 0 additions & 24 deletions lib/generators/cypress_on_rails/update_generator.rb

This file was deleted.

21 changes: 14 additions & 7 deletions specs_e2e/rails_3_2/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@ export BUNDLE_GEMFILE="$DIR/Gemfile"
cd $DIR

echo '-- bundle install'
bundle --version
bundle install --quiet --gemfile="$DIR/Gemfile" --retry 2 --path vendor/bundle
gem install bundler -v '1.0.22'
bundle _1.0.22_ --version
bundle _1.0.22_ install --quiet --gemfile="$DIR/Gemfile" --path vendor/bundle

echo '-- cypress install'
bundle exec ./bin/rails g cypress_on_rails:install --install_cypress --install_playwright --install_with=npm
bundle exec ./bin/rails g cypress_on_rails:install --install_with=npm --install_folder="." --force
rm -vf cypress/e2e/rails_examples/advance_factory_bot.cy.js
rm -vf cypress/e2e/rails_examples/using_vcr.cy.js

echo '-- start rails server'
# make sure the server is not running
(kill -9 `cat tmp/pids/server.pid` || true )
(kill -9 `cat ../server.pid` || true )

bundle exec ./bin/rails server -p 5017 -e test &
bundle exec ./bin/rails server -p 5017 -e test -P ../server.pid &
sleep 2 # give rails a chance to start up correctly

echo '-- cypress run'
Expand All @@ -31,13 +32,19 @@ cp -fv ../cypress.config.js .
# then
# node_modules/.bin/cypress run
# else
node_modules/.bin/cypress run --record
npx cypress run --record
# fi

echo '-- playwright install'
bundle exec ./bin/rails g cypress_on_rails:install --framework playwright --install_with=npm --install_folder="." --force
rm -vf playwright/e2e/rails_examples/advance_factory_bot.cy.js
rm -vf playwright/e2e/rails_examples/using_vcr.cy.js

echo '-- playwright run'
cp -fv ../playwright.config.js .
npx playwright install-deps
npx playwright install
npx playwright test playwright/e2e/

echo '-- stop rails server'
kill -9 `cat tmp/pids/server.pid`
kill -9 `cat ../server.pid` || true
1 change: 1 addition & 0 deletions specs_e2e/rails_4_2/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ spec/cypress.config.js
spec/package.json
spec/yarn.lock
spec/cypress
spec/app_commands
config/initializers/cypress_on_rails.rb
vendor/bundle
tmp/pids
Expand Down
6 changes: 4 additions & 2 deletions specs_e2e/rails_4_2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"main": "index.js",
"license": "MIT",
"devDependencies": {
"cypress": "^10.0.2",
"cypress-on-rails": "file:../../plugin"
"@playwright/test": "^1.40.1",
"cypress": "^10.11.0",
"cypress-on-rails": "file:../../plugin",
"playwright": "^1.40.1"
}
}

0 comments on commit 92ed2fd

Please sign in to comment.