Skip to content

Commit

Permalink
fix(queries): include value and escape text in button steps
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed Oct 6, 2023
1 parent de85821 commit 2c9e170
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/queries/button.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { When } from '@badeball/cypress-cucumber-preprocessor';

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

/**
* When I find buttons by text:
Expand Down Expand Up @@ -30,10 +33,7 @@ import { getCypressElement, setCypressElement } from '../utils';
* - {@link When_I_find_button_by_text | When I find button by text}
*/
export function When_I_find_buttons_by_text(text: string) {
const selector = ['button', "[type='button']", "[type='submit']"]
.map((value) => `${value}:contains('${text}'):visible`)
.join(',');
setCypressElement(cy.get(selector));
setCypressElementsByButtonText(text);
}

When('I find buttons by text {string}', When_I_find_buttons_by_text);
Expand Down Expand Up @@ -67,8 +67,7 @@ When('I find buttons by text {string}', When_I_find_buttons_by_text);
* - {@link When_I_find_buttons_by_text | When I find buttons by text}
*/
export function When_I_find_button_by_text(text: string) {
When_I_find_buttons_by_text(text);
setCypressElement(getCypressElement().first());
setCypressElementByButtonText(text);
}

When('I find button by text {string}', When_I_find_button_by_text);
74 changes: 74 additions & 0 deletions src/utils/button.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* eslint-disable tsdoc/syntax */

import { setCypressElement } from '../utils';

/**
* Set Cypress elements by button text:
*
* ```ts
* setCypressElementsByButtonText('...');
* ```
*
* @example
*
* ```ts
* setCypressElementsByButtonText('Text');
* ```
*
* @see
*
* - {@link getCypressElement}
* - {@link setCypressElement}
*
* @param text - Button text.
* @returns - Cypress element.
*
* @private
*/
export function setCypressElementsByButtonText(text: string) {
const buttons = [
'button',
"[type='button']",
"[type='submit']",
"[role='button']",
];
const selector1 = buttons
.map((selector) => `${selector}:contains(${JSON.stringify(text)}):visible`)
.join(',');
const selector2 = buttons
.map((selector) => `${selector}[value=${JSON.stringify(text)}]:visible`)
.join(',');
const elements = cy.get(`${selector1},${selector2}`);
setCypressElement(elements);
return elements;
}

/**
* Set Cypress element by button text:
*
* ```ts
* setCypressElementByButtonText('...');
* ```
*
* @example
*
* ```ts
* setCypressElementByButtonText('Text');
* ```
*
* @see
*
* - {@link getCypressElement}
* - {@link setCypressElement}
*
* @param text - Button text.
* @returns - Cypress element.
*
* @private
*/
export function setCypressElementByButtonText(text: string) {
const elements = setCypressElementsByButtonText(text);
const element = elements.first();
setCypressElement(element);
return element;
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './button';
export * from './display-value';
export * from './element';
export * from './label';
Expand Down

0 comments on commit 2c9e170

Please sign in to comment.