How do I conditionally skip a test if the URL contains "xyz"? some tests that run in the QA environment "abc" should not be run in Production "xyz" environment.
I've not been able to find a good example of conditionally checking for environment to trigger a test. The baseURL needs to be checked dynamically and the test skipped preferably in the beforeEach.
running cypress 6.2.0
beforeEach(() => {
    login.loginByUser('TomJones');
    cy.visit(`${environment.getBaseUrl()}${route}`);
 });
it('test page', function () {
     if environment.getBaseUrl().contains("xyz")
       then *skip test* 
     else
       cy.intercept('GET', '**/some-api/v1/test*').as('Test'););     
       cy.get('#submitButton').click();
})
Potential Solution (tested and tried successfully): I used a combination of filtering (grouping) and folder structures via CLI I set folders /integrations/smokeTest/QA and /integrations/smokeTest/Prod/
1.QA Test Run: 
  npm run *cy:filter:qa* --spec "cypresss/integration/smokeTests/QA/*-spec.ts"
2.Run All (both QA and PROD tests)
  npm run cypress:open --spec "cypresss/integration/smokeTests/*/*-spec.ts"
3. Prod Test Run: 
npm run cy:filter:prod --spec "cypresss/integration/smokeTests/PROD*/*-spec.ts"
Normally I wouldn't write a custom command just to exercise one Cypress command, but in this case it's useful to obtain the global test context this.
Using the function form of callback with the custom command allows access to this, then you are free to use arrow functions on the test themselves.
Cypress.Commands.add('skipWhen', function (expression) {
  if (expression) {
    this.skip()
  }
})
it('test skipping with arrow function', () => {
  cy.skipWhen(Cypress.config('baseUrl').includes('localhost'));
  // NOTE: a "naked" expect() will not be skipped
  // if you call your custom command within the test
  // Wrap it in a .then() to make sure it executes on the command queue
  cy.then(() => {
    expect('this.stackOverflow.answer').to.eq('a.better.example')
  })
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With