Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cypress reuse test cases in multiple test suites

I am new to Cypress and trying out one POC.

The application I am working on requires me to test the same component in different test suites. Is there any why in which I can avoid rewriting the same chunk of code, like using function?

export function testWelcomeBanner() {
    ...
 welcomeBanner.should('have.text', 'welcome');
    ...
}

I did try some ways, like trying to call this function from it block in test suites etc. but received errors. Any help appreciated.

like image 839
user3567993 Avatar asked Dec 13 '22 08:12

user3567993


1 Answers

You can use custom commands to reuse your scripts. You can read more about custom commands from the cypress docs.

You can write your reusable functions under cypress/support/command.js

Cypress.Commands.add('welcomeBanner', (text) => {
  cy.get('locator').should('have.text', 'text');
})

You can use the above command in any of your tests like

cy.welcomeBanner('Welcome Text')
like image 196
Alapan Das Avatar answered Dec 31 '22 14:12

Alapan Das