Did cypress have solution like tags in robot framework

Viewed 172

I want to test some case in dev environment and test some case on production environment without separate test file, How can i do that? I try to reseach about .skip but it seem like it not have condition like i just want to skip this case when run on production.

2 Answers

You can use the cypress-grep plugin for this.

  1. To install: npm i -D cypress-grep

  2. Under cypress/plugins/index.js write:

module.exports = (on, config) => {
  require('cypress-grep/src/plugin')(config)
  return config
}
  1. Under cypress/support/index.js write:
require('cypress-grep')()

Then you can run youer tests based on title sub-string or Tags:

# run only the tests with "auth user" in the title
$ npx cypress run --env grep="auth user"
# run tests with "hello" or "auth user" in their titles
# by separating them with ";" character
$ npx cypress run --env grep="hello; auth user"
# run tests tagged @fast
$ npx cypress run --env grepTags=@fast
# run only the tests tagged "smoke"
# that have "login" in their titles
$ npx cypress run --env grep=login,grepTags=smoke
# only run the specs that have any tests with "user" in their titles
$ npx cypress run --env grep=user,grepFilterSpecs=true
# only run the specs that have any tests tagged "@smoke"
$ npx cypress run --env grepTags=@smoke,grepFilterSpecs=true
# run only tests that do not have any tags
# and are not inside suites that have any tags
$ npx cypress run --env grepUntagged=true

If you want to add tags in your tests, you can do like this:

it('Test Case', {tags: 'regression'}, function () {})
describe('Test Suite', {tags: 'smoke'}, function () {})

The easiest way is to check an environment variable and use this.skip

it('only runs in development' function() {  // note function() here

  const isProduction = Cypress.env('test_env') === 'production';
  if (isProduction ) {
    this.skip();
  }

  // test here
})

There are four methods of setting the 'production' flag, see Cypress.env() - Setting

For example on the command line or package.json script

cypress run --env test_env=production
Related