Skip to content

Commit

Permalink
[cypress.rake] Add cypress.env.json generation
Browse files Browse the repository at this point in the history
Cypress handles environment variables itself, so `process.env` isn't
available.

As a result, "Option 2" is what is being done here:

https://docs.cypress.io/guides/guides/environment-variables.html#Option-2-cypress-env-json

which makes sure a default config file exists as part of the `setup`
step for running the specs.  A `file` task is used here to only run it
if the file doesn't exist, but makes it a dependency for the `:setup`
task.

By default, screenshots are disabledin CI, but enabled in dev.

The cypress.env.json file is also ignored by git, so local edits can
happen and can be customized for each developer.  On CI, it will just be
copied in and the defaults from `.cypress.ci.env.json` will be what is
conifigured in that environment.
  • Loading branch information
NickLaMuro committed Jul 22, 2020
1 parent b07ea0a commit 29fc118
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .cypress.ci.env.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"disable_screenshots": "true"
}
3 changes: 3 additions & 0 deletions .cypress.dev.env.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"disable_screenshots": "false"
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ config/webpack/paths.json
*.sw[po]
cypress/screenshots
cypress/videos
cypress.env.json
8 changes: 3 additions & 5 deletions cypress/support/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import './commands'
// Alternatively you can use CommonJS syntax:
// require('./commands')

const { env } = require('process')

if (env.TRAVIS || env.CI) {
Cypress.Screenshot.defaults({screenshotOnRunFailure: false})
}
Cypress.Screenshot.defaults({
screenshotOnRunFailure: !(Cypress.env('disable_screenshots') == 'true')
})
14 changes: 13 additions & 1 deletion lib/tasks/manageiq/cypress.rake
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
CYPRESS_ENV_FILE = File.expand_path(File.join("..", "..", "..", "cypress.env.json"), __dir__) unless defined?(CYPRESS_ENV_FILE)

namespace :cypress do
desc "Run cypress tests"
task :ui => 'ui:run'
Expand Down Expand Up @@ -25,11 +27,21 @@ namespace :cypress do
Rake::Task["#{app_prefix}integration:stop_server"].invoke
end

task :setup do |rake_task|
task :setup => CYPRESS_ENV_FILE do |rake_task|
app_prefix = rake_task.name.chomp('cypress:ui:setup')
Rake::Task["#{app_prefix}integration:start_server"].invoke
end

file CYPRESS_ENV_FILE do |cypress_env_json_file|
unless File.exist?(CYPRESS_ENV_FILE)
cypress_dir = File.dirname(CYPRESS_ENV_FILE)
cypress_env_example = ".cypress.dev.env.json"
cypress_env_example = ".cypress.ci.env.json" if ENV['CI'] || ENV['TRAVIS']

cp File.join(cypress_dir, cypress_env_example), cypress_env_json_file.name
end
end

desc "Run cypress tests in 'development' mode"
task :dev => 'dev:run'

Expand Down

0 comments on commit 29fc118

Please sign in to comment.