Skip to content

Commit

Permalink
feat(queries): add "When I find elements by title"
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed Nov 30, 2024
1 parent 0258967 commit 74c24bd
Showing 1 changed file with 79 additions and 9 deletions.
88 changes: 79 additions & 9 deletions src/queries/title.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,61 @@
import { When } from '@badeball/cypress-cucumber-preprocessor';
import { DataTable, When } from '@badeball/cypress-cucumber-preprocessor';

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

/**
* When I find elements by title:
*
* ```gherkin
* When I find elements by title {string}
* ```
*
* Finds elements with a matching `title` attribute. This will also find `title` elements within SVGs.
*
* @example
*
* ```gherkin
* When I find elements by title "Title"
* ```
*
* With [options](https://docs.cypress.io/api/commands/get#Arguments):
*
* ```gherkin
* When I find elements by title "Title"
* | log | true |
* | timeout | 4000 |
* | withinSubject | null |
* | includeShadowDom | false |
* | pseudoSelector | visible |
* ```
*
* @remarks
*
* This precedes steps like {@link When_I_click | "When I click"}. For example:
*
* ```gherkin
* When I find elements by title "Close"
* And I get 1st element
* And I click
* ```
*
* Inspired by Testing Library's [ByTitle](https://testing-library.com/docs/queries/bytitle).
*
* @see
*
* - {@link When_I_find_element_by_title | When I find element by title}
*/
export function When_I_find_elements_by_title(
title: string,
options?: DataTable,
) {
const selectors = [
`[title=${JSON.stringify(title)}]`,
`svg title:contains(${JSON.stringify(title)})`,
];
setCypressElement(cy.get(selectors.join(','), getOptions(options)));
}

When('I find elements by title {string}', When_I_find_elements_by_title);

/**
* When I find element by title:
Expand All @@ -14,7 +69,18 @@ import { setCypressElement } from '../utils';
* @example
*
* ```gherkin
* When I find element by title "Close"
* When I find element by title "Title"
* ```
*
* With [options](https://docs.cypress.io/api/commands/get#Arguments):
*
* ```gherkin
* When I find element by title "Title"
* | log | true |
* | timeout | 4000 |
* | withinSubject | null |
* | includeShadowDom | false |
* | pseudoSelector | visible |
* ```
*
* @remarks
Expand All @@ -27,13 +93,17 @@ import { setCypressElement } from '../utils';
* ```
*
* Inspired by Testing Library's [ByTitle](https://testing-library.com/docs/queries/bytitle).
*
* @see
*
* - {@link When_I_find_elements_by_title | When I find elements by title}
*/
export function When_I_find_element_by_title(title: string) {
const selector = [
`svg title:contains(${JSON.stringify(title)})`,
`[title=${JSON.stringify(title)}]`,
].join(',');
setCypressElement(cy.get(selector).first());
export function When_I_find_element_by_title(
title: string,
options?: DataTable,
) {
When_I_find_elements_by_title(title, options);
setCypressElement(getCypressElement().first());
}

When('I find element by title {string}', When_I_find_element_by_title);

0 comments on commit 74c24bd

Please sign in to comment.