From f6df82196567ec135c7e3d9678a86eb868a49ebe Mon Sep 17 00:00:00 2001 From: Mark Date: Sat, 7 Oct 2023 13:32:30 -0400 Subject: [PATCH] feat(queries): add "When I find elements by label text" --- src/queries/label.ts | 51 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) 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(