Skip to content

Commit

Permalink
fix: set focus after within action
Browse files Browse the repository at this point in the history
  • Loading branch information
luifr10 committed Aug 1, 2024
1 parent 018dfe8 commit 593351e
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 6 deletions.
25 changes: 25 additions & 0 deletions packages/runner-cypress/e2e/en-within-and-type.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Feature: Within and type
Scenario: key.when.withinElement.withRoleBased and click
Given I visit path "https://e2e-test-quest.github.io/weather-app/?isStarted=true"
And I click on button named "Add new town"
When within the element with selector "#new-town-name"
And I type the sentence "why always me ?"
And I reset context
And I should see a text box named "Town name" and containing "why always me ?"
And within a spin button named "Latitude"
And I enter the value "15"
And I reset context
Then I should see a spin button named "Latitude" and containing "15"


Scenario: key.when.withinElement.selector and click
Given I visit path "https://e2e-test-quest.github.io/weather-app/?isStarted=true"
And I click on button named "Add new town"
When within the element with selector "#new-town-name"
And I type the sentence "why always me ?"
And I reset context
And I should see a text box named "Town name" and containing "why always me ?"
And within the element with selector "#new-town-latitude"
And I enter the value "15"
And I reset context
Then I should see a spin button named "Latitude" and containing "15"
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ When(`${key.when.withinElement.ariaLabel}`, function(expectedAriaLabel: string)
cy.uuvPatchContext({
withinFocusedElement: foundedElement
});

foundedElement.focus();
});

/**
Expand All @@ -177,6 +179,8 @@ When(`${key.when.withinElement.testId}`, function(testId: string) {
cy.uuvPatchContext({
withinFocusedElement: foundedElement
});

foundedElement.focus();
});

/**
Expand All @@ -199,6 +203,8 @@ When(`${key.when.withinElement.selector}`, function(selector: string) {
cy.uuvPatchContext({
withinFocusedElement: foundedElement
});

foundedElement.focus();
});

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/runner-cypress/src/tests/report/report.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe("Runner Cypress JunitReport", () => {
});

test("Should have good results", () => {
expect(report.testsuites.tests).toEqual("110");
expect(report.testsuites.tests).toEqual("112");
expect(report.testsuites.failures).toEqual("2");
expect(report.testsuites.errors).toBeUndefined();
expect(report.testsuites.skipped).toBeUndefined();
Expand Down
25 changes: 25 additions & 0 deletions packages/runner-playwright/e2e/en-within-and-type.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Feature: Within and type
Scenario: key.when.withinElement.withRoleBased and click
Given I visit path "https://e2e-test-quest.github.io/weather-app/?isStarted=true"
And I click on button named "Add new town"
When within the element with selector "#new-town-name"
And I type the sentence "why always me ?"
And I reset context
And I should see a text box named "Town name" and containing "why always me ?"
And within a spin button named "Latitude"
And I enter the value "15"
And I reset context
Then I should see a spin button named "Latitude" and containing "15"


Scenario: key.when.withinElement.selector and click
Given I visit path "https://e2e-test-quest.github.io/weather-app/?isStarted=true"
And I click on button named "Add new town"
When within the element with selector "#new-town-name"
And I type the sentence "why always me ?"
And I reset context
And I should see a text box named "Town name" and containing "why always me ?"
And within the element with selector "#new-town-latitude"
And I enter the value "15"
And I reset context
Then I should see a spin button named "Latitude" and containing "15"
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ When(`${key.when.click.withRole}`, async function(this: World, role: string, nam
* */
When(`${key.when.withinElement.ariaLabel}`, async function(this: World, expectedAriaLabel: string) {
const sanitizedExpectedAriaLabel = encodeURIComponent(expectedAriaLabel).replaceAll("%20", " ");
await getPageOrElement(this).then((element) => expect(element.getByLabel(sanitizedExpectedAriaLabel, { exact: true })).toHaveCount(1));
await getPageOrElement(this).then(async (element) => {
const locator = element.getByLabel(sanitizedExpectedAriaLabel, { exact: true });
await expect(locator).toHaveCount(1);
await locator.focus({ timeout: 10000 });
});
await addCookie(this, COOKIE_NAME.SELECTED_ELEMENT, new SelectedElementCookie(FILTER_TYPE.ARIA_LABEL, sanitizedExpectedAriaLabel));
});

Expand All @@ -114,6 +118,11 @@ When(`${key.when.resetContext}`, async function(this: World) {
* key.when.withinElement.selector.description
* */
When(`${key.when.withinElement.selector}`, async function(this: World, selector: string) {
await getPageOrElement(this).then(async (element) => {
const locator = element.locator(selector);
await expect(locator).toHaveCount(1);
await locator.focus({ timeout: 10000 });
});
await getPageOrElement(this).then((element) => expect(element.locator(selector)).toHaveCount(1));
await addCookie(this, COOKIE_NAME.SELECTED_ELEMENT, new SelectedElementCookie(FILTER_TYPE.SELECTOR, selector));
});
Expand Down Expand Up @@ -181,7 +190,11 @@ When(`${key.when.withinElement.roleAndName}`, async function(this: World, role:
* */
When(`${key.when.withinElement.testId}`, async function(this: World, testId: string) {
testId = encodeURIComponent(testId);
await getPageOrElement(this).then(async (element) => await expect(element.getByTestId(testId)).toHaveCount(1));
await getPageOrElement(this).then(async (element) => {
const locator = element.getByTestId(testId);
await expect(locator).toHaveCount(1);
await locator.focus({ timeout: 10000 });
});
await addCookie(this, COOKIE_NAME.SELECTED_ELEMENT, new SelectedElementCookie(FILTER_TYPE.TEST_ID, testId));
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export async function findWithRoleAndName(world: World, role: string, name: stri
}

export async function withinRoleAndName(world: World, role: string, name: string) {
await findWithRoleAndNameAndContent(world, role, name);
await findWithRoleAndNameAndContent(world, role, name, undefined, true);
await addCookie(world, COOKIE_NAME.SELECTED_ELEMENT, new SelectedElementCookie(FILTER_TYPE.SELECTOR, `role=${role}[name="${name}"]`));

}
Expand All @@ -173,14 +173,15 @@ export async function notFoundWithRoleAndName(world: World, role: string, name:

}

export async function findWithRoleAndNameAndContent(world: World, expectedRole: string, name: string, expectedTextContent: string | undefined = undefined): Promise<any> {
export async function findWithRoleAndNameAndContent(world: World, expectedRole: string, name: string, expectedTextContent: string | undefined = undefined, setFocus = false): Promise<any> {
expectedRole = encodeURIComponent(expectedRole);
await getPageOrElement(world).then(async (element) => {
const byRole = await element.getByRole(expectedRole, { name: name, includeHidden: true, exact: true });
await expect(byRole).toHaveCount(1);
if (expectedTextContent !== undefined) {
await checkTextContentLocator(byRole, expectedTextContent);
}
await byRole.focus({ timeout: 10000 });
});
}

Expand Down
2 changes: 1 addition & 1 deletion packages/runner-playwright/src/tests/report/report.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe("Runner Playwright JunitReport", () => {
});

test("Should have good results", () => {
expect(report.testsuites.tests).toEqual("108");
expect(report.testsuites.tests).toEqual("110");
expect(report.testsuites.failures).toEqual("3");
expect(report.testsuites.errors).toEqual("0");
expect(report.testsuites.skipped).toEqual("0");
Expand Down

0 comments on commit 593351e

Please sign in to comment.