Skip to content

Commit

Permalink
Merge pull request #485 from remarkablemark/feat/assertions
Browse files Browse the repository at this point in the history
feat(queries): add "When I find headings by text" and "When I find heading by text"
  • Loading branch information
remarkablemark authored Oct 2, 2023
2 parents c84f50f + 9148693 commit a71e49a
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 5 deletions.
8 changes: 8 additions & 0 deletions cypress/e2e/example/example.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ Feature: Example
Then I see document title "Example Domain"
And I see document title contains "Example"

Scenario: See heading
Given I visit "http://example.com/"
Then I see heading "Example Domain"
When I find headings by text "Example Domain"
Then I count 1 element
When I find heading by text "Example Domain"
Then I count 1 element

Scenario: See and not see text
Given I visit "http://example.com/"
Then I see text "Example Domain"
Expand Down
6 changes: 5 additions & 1 deletion src/assertions/button.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Then } from '@badeball/cypress-cucumber-preprocessor';

import { When_I_find_button_by_text } from '../queries';
import { getCypressElement } from '../utils';

/**
* Then I see button:
*
Expand All @@ -22,7 +25,8 @@ import { Then } from '@badeball/cypress-cucumber-preprocessor';
* - {@link Then_I_see_text | Then I see text}
*/
export function Then_I_see_button(text: string) {
cy.contains('button:visible', text).should('exist');
When_I_find_button_by_text(text);
getCypressElement().should('exist');
}

Then('I see button {string}', Then_I_see_button);
9 changes: 5 additions & 4 deletions src/assertions/heading.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Then } from '@badeball/cypress-cucumber-preprocessor';

import { When_I_find_heading_by_text } from '../queries';
import { getCypressElement } from '../utils';

/**
* Then I see heading:
*
Expand All @@ -22,10 +25,8 @@ import { Then } from '@badeball/cypress-cucumber-preprocessor';
* - {@link Then_I_see_text | Then I see text}
*/
export function Then_I_see_heading(text: string) {
const selector = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']
.map((tag) => `${tag}:contains('${text}'):visible`)
.join(',');
cy.get(selector).should('exist');
When_I_find_heading_by_text(text);
getCypressElement().should('exist');
}

Then('I see heading {string}', Then_I_see_heading);
74 changes: 74 additions & 0 deletions src/queries/heading.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { When } from '@badeball/cypress-cucumber-preprocessor';

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

/**
* When I find headings by text:
*
* ```gherkin
* When I find headings by text {string}
* ```
*
* @example
*
* ```gherkin
* When I find headings by text "Heading"
* ```
*
* @remarks
*
* This precedes steps like {@link When_I_click | "When I click"}. For example:
*
* ```gherkin
* When I find headings by text "Heading"
* And I get 1st element
* And I click
* ```
*
* @see
*
* - {@link When_I_find_heading_by_text | When I find heading by text}
*/
export function When_I_find_headings_by_text(text: string) {
const selector = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']
.map((tag) => `${tag}:contains('${text}'):visible`)
.join(',');
setCypressElement(cy.get(selector));
}

When('I find headings by text {string}', When_I_find_headings_by_text);

/**
* When I find heading by text:
*
* ```gherkin
* When I find heading by text {string}
* ```
*
* Finds first heading element that matches text.
*
* @example
*
* ```gherkin
* When I find heading by text "Heading"
* ```
*
* @remarks
*
* This precedes steps like {@link When_I_click | "When I click"}. For example:
*
* ```gherkin
* When I find heading by text "heading"
* And I click
* ```
*
* @see
*
* - {@link When_I_find_headings_by_text | When I find headings by text}
*/
export function When_I_find_heading_by_text(text: string) {
When_I_find_headings_by_text(text);
setCypressElement(getCypressElement().first());
}

When('I find heading by text {string}', When_I_find_heading_by_text);
1 change: 1 addition & 0 deletions src/queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * from './find';
export * from './focused';
export * from './form';
export * from './get';
export * from './heading';
export * from './input';
export * from './label';
export * from './link';
Expand Down

0 comments on commit a71e49a

Please sign in to comment.