diff --git a/src/queries/label.ts b/src/queries/label.ts index 0508655d1..8eeecabc6 100644 --- a/src/queries/label.ts +++ b/src/queries/label.ts @@ -1,6 +1,50 @@ import { When } from '@badeball/cypress-cucumber-preprocessor'; -import { setCypressElementByLabelText } from '../utils'; +import { + getCypressElement, + setCypressElement, + setCypressElementsByLabelText, +} from '../utils'; + +/** + * When I find elements by label text: + * + * ```gherkin + * When I find elements by label text {string} + * ``` + * + * Finds all `label`, `aria-labelledby`, or `aria-label` that matches the text. + * + * @example + * + * ```gherkin + * When I find elements by label text "Email" + * ``` + * + * @remarks + * + * This precedes steps like {@link When_I_type | "When I type"}. For example: + * + * ```gherkin + * When I find elements by label text "Email" + * And I get 1st element + * And I type "user@example.com" + * ``` + * + * Inspired by Testing Library's [ByLabelText](https://testing-library.com/docs/queries/bylabeltext). + * + * @see + * + * - {@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) { + setCypressElementsByLabelText(text); +} + +When( + 'I find elements by label text {string}', + When_I_find_elements_by_label_text, +); /** * When I find element by label text: @@ -30,10 +74,11 @@ import { setCypressElementByLabelText } from '../utils'; * * @see * - * - {@link When_I_find_input_by_label_text | When I find input by label text } + * - {@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) { - setCypressElementByLabelText(text); + When_I_find_elements_by_label_text(text); + setCypressElement(getCypressElement().first()); } When(