Skip to content

Commit

Permalink
clean up e2e commands
Browse files Browse the repository at this point in the history
  • Loading branch information
kcinay055679 committed Nov 7, 2024
1 parent 7b95477 commit 0eb517d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 51 deletions.
70 changes: 19 additions & 51 deletions frontend/cypress/support/commands.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { validateScoring } from './scoringSupport';
import { keyCodeDefinitions } from 'cypress-real-events/keyCodeDefinitions';
import { pressUntilContains, doUntilSelector } from './utils';

Cypress.Commands.add('loginAsUser', (user: any) => {
cy.viewport(1920, 1080);
loginWithCredentials(user.username, user.password);
overviewIsLoaded();
});

Cypress.Commands.add('getByTestId', (testId: string): Cypress.Chainable<JQuery<HTMLElement>> => {
Expand All @@ -25,6 +25,22 @@ Cypress.Commands.add('pressUntilContains', (text: string, key: keyof typeof keyC
pressUntilContains(text, key);
});

Cypress.Commands.add('tabForward', () => {
cy.realPress('Tab');
});

Cypress.Commands.add('tabBackward', () => {
cy.realPress(['Shift', 'Tab']);
});

Cypress.Commands.add('tabForwardUntil', (selector: string, limit?: number) => {
doUntilSelector(selector, cy.tabForward, limit);
});

Cypress.Commands.add('tabBackwardUntil', (selector: string, limit?: number) => {
doUntilSelector(selector, cy.tabBackward, limit);
});

Cypress.Commands.add('getZone', (zone: string, onOverview: boolean) => {
return (onOverview ? cy.focused() : cy.getByTestId('side-panel')).findByTestId(zone);
});
Expand Down Expand Up @@ -112,22 +128,6 @@ Cypress.Commands.add(
},
);

Cypress.Commands.add('tabForward', () => {
cy.realPress('Tab');
});

Cypress.Commands.add('tabBackward', () => {
cy.realPress(['Shift', 'Tab']);
});

Cypress.Commands.add('tabForwardUntil', (selector: string, limit?: number) => {
doUntilSelector(selector, cy.tabForward, limit);
});

Cypress.Commands.add('tabBackwardUntil', (selector: string, limit?: number) => {
doUntilSelector(selector, cy.tabBackward, limit);
});

Cypress.Commands.add('createOrdinalKeyresult', (title: string | null = null, owner: string | null = null) => {
cy.getByTestId('objective').first().getByTestId('add-keyResult').first().click();
cy.getByTestId('submit').should('be.disabled');
Expand Down Expand Up @@ -234,38 +234,6 @@ Cypress.Commands.add(
},
);

function pressUntilContains(text: string, key: keyof typeof keyCodeDefinitions) {
const condition = (element: HTMLElement) => element.innerText.includes(text);
pressUntil(key, condition);
}

function pressUntil(key: keyof typeof keyCodeDefinitions, condition: (elem: HTMLElement) => boolean) {
doUntil(condition, () => cy.realPress(key));
}

function doUntilSelector(selector: string, tab: () => void, limit: number = 100, count: number = 0) {
const condition = (element: HTMLElement) => element.matches(selector);
doUntil(condition, tab, limit, count);
}

function doUntil(
condition: (element: HTMLElement) => boolean,
tab: () => void,
limit: number = 100,
count: number = 0,
) {
if (count >= limit) return;

cy.focused().then((element) => {
if (condition(element.get(0))) {
return;
} else {
tab();
doUntil(condition, tab, limit, count + 1);
}
});
}

function setConfidence(confidence: number) {
cy.getByTestId('confidence-slider').find('input').focus();
for (let i = 0; i < 10; i++) {
Expand All @@ -278,12 +246,14 @@ function setConfidence(confidence: number) {

function loginWithCredentials(username: string, password: string) {
cy.visit('/');
cy.intercept('GET', '**/overview').as('getOverview');
cy.intercept('GET', '**/users/current').as('getCurrentUser');
cy.origin(Cypress.env('login_url'), { args: { username, password } }, ({ username, password }) => {
cy.get('input[name="username"]').type(username);
cy.get('input[name="password"]').type(password);
cy.get('button[type="submit"]').click();
cy.wait('@getCurrentUser', { responseTimeout: 10000 });
cy.wait('@getOverview', { responseTimeout: 10000 });
});
cy.url().then((url) => {
const currentUrl = new URL(url);
Expand All @@ -302,8 +272,6 @@ function checkForToaster(selector: string, amount: number, messages: string[] =
.then((e) => messages.forEach((m) => expect(e).contains(m)));
}

const overviewIsLoaded = () => cy.get('mat-chip').should('have.length.at.least', 2);

// -- This is a parent command --
// Cypress.Commands.add("login", (email, password) => { ... })
//
Expand Down
33 changes: 33 additions & 0 deletions frontend/cypress/support/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
import { v4 as uuidv4 } from 'uuid';
import { keyCodeDefinitions } from 'cypress-real-events/keyCodeDefinitions';

export const uniqueSuffix = (value: string): string => {
return `${value}-${uuidv4()}`;
};

export function pressUntilContains(text: string, key: keyof typeof keyCodeDefinitions) {
const condition = (element: HTMLElement) => element.innerText.includes(text);
pressUntil(key, condition);
}

export function doUntilSelector(selector: string, tab: () => void, limit: number = 100, count: number = 0) {
const condition = (element: HTMLElement) => element.matches(selector);
doUntil(condition, tab, limit, count);
}

function pressUntil(key: keyof typeof keyCodeDefinitions, condition: (elem: HTMLElement) => boolean) {
doUntil(condition, () => cy.realPress(key));
}

function doUntil(
condition: (element: HTMLElement) => boolean,
tab: () => void,
limit: number = 100,
count: number = 0,
) {
if (count >= limit) return;

cy.focused().then((element) => {
if (condition(element.get(0))) {
return;
} else {
tab();
doUntil(condition, tab, limit, count + 1);
}
});
}

0 comments on commit 0eb517d

Please sign in to comment.