Cypress: It is possible to create a single test can identify which browser you are using and then it will execute that test create for that browser

Viewed 125

Just a simple question in optimizing my test.

It is possible to create a single test can identify which browser you are using and then it will execute that test create for that browser

For example:

If detects chrome it will run this and it will not run in firefox

describe('Sample test', () => {
  it('verify header!', () => {
    get('#Header')
  })
})

If detects firefox it will run this and it will not run in chrome

describe('Sample test', () => {
  it('verify header!', () => {
    get('#fixHeader')
  })
})
2 Answers

Yes, it's possible to skip when browser is a certain type

describe('Sample test', () => {
  it('verify header!', () => {
    if (Cypress.browser.name === 'chrome') {
      this.skip
    }
    get('#Header')
  })
})

Or by family (e.g cover Chrome, Edge and Electron with one step)

describe('Sample test', () => {
  it('verify header!', () => {
    if (Cypress.browser.family === 'chromium') {
      this.skip
    }
    get('#Header')
  })
})

You can sun specific tests on specific browser like this. You can learn more about it from the cypress docs

//Runs in Electron
it('Test 1', { browser: 'electron' }, () => {
  ...
})

//Runs in Firefox
it('Test 2', { browser: 'firefox' }, () => {
  ...
})

//Runs in Chrome
it('Test 3', { browser: 'chrome' }, () => {
  ...
})

//Doesn't run in Chrome
it('Test 4', { browser: '!chrome' }, () => {
  ...
})
Related