Skip to content

Commit

Permalink
Use aliases to remove unsafe chainings in cypress tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomTannenbaum committed Nov 19, 2024
1 parent 196beb0 commit bac9b3e
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 20 deletions.
2 changes: 2 additions & 0 deletions frontend/cypress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export default defineConfig({
baseUrl: 'http://pitc.okr.localhost:4200',
experimentalMemoryManagement: true,
testIsolation: true,
viewportWidth: 1920,
viewportHeight: 1080,
},
env: {
login_url: 'http://localhost:8544',
Expand Down
5 changes: 3 additions & 2 deletions frontend/cypress/e2e/crud.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ describe('CRUD operations', () => {
op.addObjective().fillObjectiveTitle(objectiveTitle).selectQuarter('3');
cy.getByTestId(buttonTestId).click();
cy.visit('/?quarter=3');
const objective = op.getObjectiveByName(objectiveTitle);
objective.findByTestId('objective-state').should('have.attr', 'src', `assets/icons/${icon}`);
op.getObjectiveByName(objectiveTitle)
.findByTestId('objective-state')
.should('have.attr', 'src', `assets/icons/${icon}`);
});
});

Expand Down
8 changes: 4 additions & 4 deletions frontend/cypress/e2e/duplicated-scoring.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ describe('e2e test for scoring adjustment on objective duplicate', () => {
.submit();
cy.visit('/?quarter=3');

const failArea = op
.getKeyResultByName('stretch keyresult for testing')
op.getKeyResultByName('stretch keyresult for testing')
.findByTestId('scoring-component')
.findByTestId('fail');
.findByTestId('fail')
.as('fail-area');

failArea.should(($fail) => {
cy.get('@fail-area').should(($fail) => {
expect($fail).not.to.have.css('score-red');
expect($fail).not.to.have.css('score-yellow');
expect($fail).not.to.have.css('score-green');
Expand Down
4 changes: 3 additions & 1 deletion frontend/cypress/e2e/objective-backlog.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ describe('OKR Objective Backlog e2e tests', () => {
op.selectFromThreeDotMenu('Objective veröffentlichen');

cy.contains('Objective veröffentlichen');
cy.getByTestId('title').first().clear().type('This is our first released objective');
cy.getByTestId('title').first().as('title');
cy.get('@title').clear();
cy.get('@title').type('This is our first released objective');

cy.get('select#quarter').should('not.contain', 'Backlog');
cy.get('select#quarter').select('GJ ForTests');
Expand Down
9 changes: 6 additions & 3 deletions frontend/cypress/e2e/objective.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,20 @@ describe('OKR Objective e2e tests', () => {
cy.contains('Search after this objective');
cy.contains('We dont want to search for this').should('not.exist');

cy.getByTestId('objectiveSearch').first().clear().type('this').wait(350);
cy.getByTestId('objectiveSearch').first().as('objective-search').clear();
cy.get('@objective-search').type('this').wait(350);

cy.contains('Search after this objective');
cy.contains('We dont want to search for this');

cy.getByTestId('objectiveSearch').first().clear().type('dont want to').wait(350);
cy.get('@objective-search').clear();
cy.get('@objective-search').type('dont want to').wait(350);

cy.contains('We dont want to search for this');
cy.contains('Search after this objective').should('not.exist');

cy.getByTestId('objectiveSearch').first().clear().type('there is no objective').wait(350);
cy.get('@objective-search').clear();
cy.get('@objective-search').type('there is no objective').wait(350);

cy.contains('We dont want to search for this').should('not.exist');
cy.contains('Search after this objective').should('not.exist');
Expand Down
8 changes: 5 additions & 3 deletions frontend/cypress/e2e/teammanagement.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ describe('Team management tests', () => {
.addAnotherUser()
.enterUser('Stefan', 'Schmidt', '[email protected]')
.addAnotherUser()

.getFirstNames();

// test error messages
Expand Down Expand Up @@ -331,7 +330,8 @@ describe('Team management tests', () => {
cy.get(matOption).contains(nameEsha).should('not.exist');

// add findus peterson
cy.getByTestId('search-member-to-add').click().type('Find');
cy.getByTestId('search-member-to-add').as('member-search').click();
cy.get('@member-search').type('Find');
cy.contains(matOption, 'Findus Peterson').click();

// add robin papierer
Expand Down Expand Up @@ -436,7 +436,9 @@ function checkRolesForEsha() {
function editTeamNameAndTest(teamName: string) {
cy.intercept('PUT', '**/teams/*').as('saveTeam');
cy.getByTestId('editTeamButton').click();
cy.getByTestId('add-team-name').click().clear().type(teamName);
cy.getByTestId('add-team-name').as('team-name').click();
cy.get('@team-name').clear();
cy.get('@team-name').type(teamName);
cy.getByTestId('safe').click();
cy.wait('@saveTeam');
cy.contains(teamName);
Expand Down
14 changes: 7 additions & 7 deletions frontend/cypress/support/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@ import { keyCodeDefinitions } from 'cypress-real-events/keyCodeDefinitions';
import { pressUntilContains, doUntilSelector } from './helper/utils';

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

Cypress.Commands.add('getByTestId', (testId: string, text?: string): Cypress.Chainable<JQuery<HTMLElement>> => {
const selector = `[data-testId=${testId}]`;
const element = cy.get(selector);

if (text) {
element.contains(text);
return cy.get(selector).contains(text);
} else {
return cy.get(selector);
}
return element;
});

Cypress.Commands.add(
'findByTestId',
{ prevSubject: true },
(subject: JQuery<HTMLElement>, testId: string, text?: string): Cypress.Chainable<JQuery<HTMLElement>> => {
const selector = `[data-testId=${testId}]`;
const element = cy.wrap(subject).find(selector);
if (text) {
element.contains(text);
return cy.wrap(subject).find(selector).contains(text);
} else {
return cy.wrap(subject).find(selector);
}
return element;
},
);

Expand Down

0 comments on commit bac9b3e

Please sign in to comment.