hasClass() is a method for CSS selectors or has() is an internal method in jQuery, used to check if an element with the specified class name exists. You can then return a boolean value for assertion purposes.
javascriptCypress.Commands.add('isExistElement', selector => { cy.get('body').then(($el) => { if ($el.has(selector)) { return true } else { return false } }) });
Then, you can create a special Cypress method using a TypeScript file (index.d.ts) and make it chainable.
typescriptdeclare namespace Cypress { interface Chainable { isExistElement(cssSelector: string): Cypress.Chainable<boolean> } }
As illustrated below:
javascriptshouldSeeCreateTicketTab() { cy.isExistElement(homePageSelector.createTicketTab).should("be.true"); }
2024年6月29日 12:07 回复