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 3, 2024
1 parent 018dfe8 commit d15baeb
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/runner-cypress/e2e/en-step-definition.feature
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Feature: English Test Step Definition
And within the element with role "textbox" and name "Last name"
When I type the sentence "Toto"
And I reset context
Then I should see an element with role "textbox" and name "Last name" and content "TotoDoe"
Then I should see an element with role "textbox" and name "Last name" and content "DoeToto"

Scenario: key.given.within.selector
Then within the element with selector '[data-testid="fieldset"]'
Expand Down
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"
2 changes: 1 addition & 1 deletion packages/runner-cypress/e2e/fr-step-definition.feature
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Fonctionnalité: Dictionnaire français de phrases de base utilisant cypress
Et je vais à l'intérieur de l'élément ayant pour rôle "textbox" et pour nom "Last name"
Quand je saisie les mots "Toto"
Et je reinitialise le contexte
Alors je dois voir un élément avec le rôle "textbox" et le nom "Last name" et pour contenu "TotoDoe"
Alors je dois voir un élément avec le rôle "textbox" et le nom "Last name" et pour contenu "DoeToto"

Règle: Other
Scénario: key.given.within.selector
Expand Down
27 changes: 27 additions & 0 deletions packages/runner-cypress/e2e/fr-within-and-type.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#language: fr
Fonctionnalité: Within and type
Scénario: key.when.withinElement.withRoleBased and click
Etant donné que je visite l'Url "https://e2e-test-quest.github.io/weather-app/?isStarted=true"
Et je clique sur le bouton nommé "Add new town"
Quand je vais à l'intérieur de l'élément ayant pour sélecteur "#new-town-name"
Et je saisie les mots "why always me ?"
Et je reinitialise le contexte
Et je dois voir une boîte à texte nommée "Town name" et contenant "why always me ?"
Et je vais à l'intérieur du bouton rotatif nommé "Latitude"
Et j'entre la valeur "15"
Et je reinitialise le contexte
Alors je dois voir un bouton rotatif nommé "Latitude" et contenant "15"


Scénario: key.when.withinElement.selector and click
Etant donné que je visite l'Url "https://e2e-test-quest.github.io/weather-app/?isStarted=true"
Et je clique sur le bouton nommé "Add new town"
Quand je vais à l'intérieur de l'élément ayant pour sélecteur "#new-town-name"
Et je saisie les mots "why always me ?"
Et je reinitialise le contexte
Et je dois voir une boîte à texte nommée "Town name" et contenant "why always me ?"
Et je vais à l'intérieur de l'élément ayant pour sélecteur "#new-town-latitude"
Et j'entre la valeur "15"
Et je reinitialise le contexte
Alors je dois voir un bouton rotatif nommé "Latitude" et contenant "15"

Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ export const uuvGetContext = (): Chainable<Context> => {
return cy.get<Context>(`@${contextAlias}`);
};

export function uuvCheckContextWithinFocusedElement(): Cypress.Chainable<Context> {
export function uuvCheckContextWithinFocusedElement(dontThrowError = false): Cypress.Chainable<Context> {
return cy.get<Context>(`@${contextAlias}`)
.then(context => {
if (!context.withinFocusedElement) {
if (!context.withinFocusedElement && !dontThrowError) {
throw new Error("No element currently selected");
}
return context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,12 +611,11 @@ function haveKeyBoardFocused() {
}

function type(textToType: string) {
if (haveKeyBoardFocused()) {
cy.focused().type(textToType);
} else {
cy.uuvCheckContextWithinFocusedElement().then((context) => {
context.withinFocusedElement!.focus();
context.withinFocusedElement!.type(textToType);
});
cy.uuvCheckContextWithinFocusedElement(true).then((context) => {
if (context.withinFocusedElement) {
context.withinFocusedElement!.type(textToType);
} else if (haveKeyBoardFocused()) {
cy.focused().type(textToType);
}
});
}
2 changes: 1 addition & 1 deletion packages/runner-cypress/src/cypress/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ declare global {
interface Chainable {
uuvGetContext(contextName?: string): Cypress.Chainable<Context>;

uuvCheckContextWithinFocusedElement(): Cypress.Chainable<Context>;
uuvCheckContextWithinFocusedElement(dontThrowError?: boolean): Cypress.Chainable<Context>;

uuvCheckContextKeyboardFocusedElement(): Cypress.Chainable<Context>;

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("114");
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"
27 changes: 27 additions & 0 deletions packages/runner-playwright/e2e/fr-within-and-type.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#language: fr
Fonctionnalité: Within and type
Scénario: key.when.withinElement.withRoleBased and click
Etant donné que je visite l'Url "https://e2e-test-quest.github.io/weather-app/?isStarted=true"
Et je clique sur le bouton nommé "Add new town"
Quand je vais à l'intérieur de l'élément ayant pour sélecteur "#new-town-name"
Et je saisie les mots "why always me ?"
Et je reinitialise le contexte
Et je dois voir une boîte à texte nommée "Town name" et contenant "why always me ?"
Et je vais à l'intérieur du bouton rotatif nommé "Latitude"
Et j'entre la valeur "15"
Et je reinitialise le contexte
Alors je dois voir un bouton rotatif nommé "Latitude" et contenant "15"


Scénario: key.when.withinElement.selector and click
Etant donné que je visite l'Url "https://e2e-test-quest.github.io/weather-app/?isStarted=true"
Et je clique sur le bouton nommé "Add new town"
Quand je vais à l'intérieur de l'élément ayant pour sélecteur "#new-town-name"
Et je saisie les mots "why always me ?"
Et je reinitialise le contexte
Et je dois voir une boîte à texte nommée "Town name" et contenant "why always me ?"
Et je vais à l'intérieur de l'élément ayant pour sélecteur "#new-town-latitude"
Et j'entre la valeur "15"
Et je reinitialise le contexte
Alors je dois voir un bouton rotatif nommé "Latitude" et contenant "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("112");
expect(report.testsuites.failures).toEqual("3");
expect(report.testsuites.errors).toEqual("0");
expect(report.testsuites.skipped).toEqual("0");
Expand Down

0 comments on commit d15baeb

Please sign in to comment.