Skip to content

Instantly share code, notes, and snippets.

@hscheuerle
Created June 28, 2021 16:55
Show Gist options
  • Save hscheuerle/35c21a8d41a56b182d02acbd4b538d09 to your computer and use it in GitHub Desktop.
Save hscheuerle/35c21a8d41a56b182d02acbd4b538d09 to your computer and use it in GitHub Desktop.
non variadic cypress page object utility function
export const dataCy = (value: string) => cy.get(`[data-cy=${value}]`);
export const dataCyPath = (...attrs: string[]) => cy.get(attrs.map(attr => `[data-cy=${attr}]`).join(' '));
export const dataCyObject = <T extends readonly string[]>(hostAttr: string, attrs: T) =>
attrs.reduce((cyObject, attr) => (cyObject[attr] = () => dataCyPath(hostAttr, attr)) && cyObject, {} as Record<T[number], () => Cypress.Chainable<JQuery<HTMLElement>>>);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment