diff --git a/cypress/support/e2e.ts b/cypress/support/e2e.ts new file mode 100644 index 0000000..a04fad6 --- /dev/null +++ b/cypress/support/e2e.ts @@ -0,0 +1 @@ +import '../../src'; diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..8ad0a78 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,51 @@ +import { getSnapshotMessage } from './utils'; + +declare global { + namespace Cypress { + interface Chainable { + /** + * @param times number of clicks - `default 1` + * + * @param options click options + * @see https://docs.cypress.io/api/commands/click + */ + clicks(times?: number, options?: Partial): Chainable; + } + } +} + +Cypress.Commands.add('clicks', { prevSubject: 'element' }, (subject, times = 1, options) => { + if (times < 1) throw new Error(`"times" can not be negative or 0 - received "${times}"`); + + cy.wrap(subject, { log: false }).as('element'); + + const log = Cypress.log({ + $el: subject, + autoEnd: false, + name: 'clicks', + message: `times: ${times}`, + consoleProps: () => ({ + command: 'clicks', + selector: subject.selector, + element: subject[0], + times + }) + }); + + log.snapshot('before'); + + for (let i = 0; i < times; i++) + cy.get('@element', { log: false }) + .click({ ...options, log: false }) + .then($el => { + log.set({ $el }); + log.snapshot(getSnapshotMessage(times, i)); + log.end(); + }); + + cy.on('fail', err => { + log.error(err); + log.end(); + throw err; + }); +}); diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 0000000..b320dfa --- /dev/null +++ b/src/utils.ts @@ -0,0 +1,5 @@ +export const getSnapshotMessage = (times: number, index: number): string => { + if (times === 1) return 'after'; + if (index === times - 1) return `after [#${index + 1}]`; + return `click #${index + 1}`; +};