Skip to content

Commit

Permalink
feat(queries): pass options to "When I find elements by label text"
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed Dec 8, 2023
1 parent 84e09df commit 2ad8bad
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions src/queries/label.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { When } from '@badeball/cypress-cucumber-preprocessor';
import { DataTable, When } from '@badeball/cypress-cucumber-preprocessor';

import { getCypressElement, setCypressElement } from '../utils';
import { getCypressElement, getOptions, setCypressElement } from '../utils';

/**
* When I find elements by label text:
Expand All @@ -9,14 +9,24 @@ import { getCypressElement, setCypressElement } from '../utils';
* When I find elements by label text {string}
* ```
*
* Finds all visible `label`, `aria-labelledby`, or `aria-label` that matches the text.
* Find all visible `label`, `aria-labelledby`, or `aria-label` that matches the text.
*
* @example
*
* ```gherkin
* When I find elements by label text "Email"
* ```
*
* With [options](https://docs.cypress.io/api/commands/get#Arguments):
*
* ```gherkin
* When I find elements by label text "Email"
* | log | true |
* | timeout | 4000 |
* | withinSubject | null |
* | includeShadowDom | false |
* ```
*
* @remarks
*
* This precedes steps like {@link When_I_type | "When I type"}. For example:
Expand All @@ -33,14 +43,17 @@ import { getCypressElement, setCypressElement } from '../utils';
*
* - {@link When_I_find_element_by_label_text | When I find element by label text }
*/
export function When_I_find_elements_by_label_text(text: string) {
export function When_I_find_elements_by_label_text(
text: string,
options?: DataTable,
) {
const selectors = [
`label:contains(${JSON.stringify(text)})`,
`[aria-labelledby=${JSON.stringify(text)}]`,
`[aria-label=${JSON.stringify(text)}]`,
].map((selector) => `${selector}:visible`);

setCypressElement(cy.get(selectors.join(',')));
setCypressElement(cy.get(selectors.join(','), getOptions(options)));
}

When(
Expand All @@ -55,14 +68,24 @@ When(
* When I find element by label text {string}
* ```
*
* Finds the first visible `label`, `aria-labelledby`, or `aria-label` that matches the text.
* Find the first visible `label`, `aria-labelledby`, or `aria-label` that matches the text.
*
* @example
*
* ```gherkin
* When I find element by label text "Email"
* ```
*
* With [options](https://docs.cypress.io/api/commands/get#Arguments):
*
* ```gherkin
* When I find element by label text "Email"
* | log | true |
* | timeout | 4000 |
* | withinSubject | null |
* | includeShadowDom | false |
* ```
*
* @remarks
*
* This precedes steps like {@link When_I_type | "When I type"}. For example:
Expand All @@ -78,8 +101,11 @@ When(
*
* - {@link When_I_find_elements_by_label_text | When I find elements by label text }
*/
export function When_I_find_element_by_label_text(text: string) {
When_I_find_elements_by_label_text(text);
export function When_I_find_element_by_label_text(
text: string,
options?: DataTable,
) {
When_I_find_elements_by_label_text(text, options);
setCypressElement(getCypressElement().first());
}

Expand Down

0 comments on commit 2ad8bad

Please sign in to comment.