Skip to content

Commit

Permalink
feat: add clicks plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
MoKhajavi75 committed Mar 11, 2023
1 parent 4b2ba40 commit 251c00b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions cypress/support/e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '../../src';
51 changes: 51 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { getSnapshotMessage } from './utils';

declare global {
namespace Cypress {
interface Chainable<Subject> {
/**
* @param times number of clicks - `default 1`
*
* @param options click options
* @see https://docs.cypress.io/api/commands/click
*/
clicks(times?: number, options?: Partial<ClickOptions>): Chainable<Subject>;
}
}
}

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;
});
});
5 changes: 5 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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}`;
};

0 comments on commit 251c00b

Please sign in to comment.