Skip to content

Commit

Permalink
Refactored couple of Cypress commands (#8979)
Browse files Browse the repository at this point in the history
  • Loading branch information
bsekachev authored Jan 22, 2025
1 parent f9b7138 commit e568888
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 25 deletions.
4 changes: 2 additions & 2 deletions tests/cypress/e2e/actions_objects/test_annotations_saving.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ context('Test annotations saving works correctly', () => {
}

before(() => {
cy.headlessLogin();
cy.visit('/tasks/create');
cy.visit('/auth/login');
cy.headlessLogin({ nextURL: '/tasks/create' });
cy.get('#name').type(taskName);
cy.addNewLabel({ name: generalLabel.name });
cy.addNewSkeletonLabel(skeletonLabel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ context('Basic markdown pipeline', () => {
before(() => {
cy.headlessLogout();
cy.visit('/auth/login');
cy.get('.cvat-login-form-wrapper').should('exist').and('be.visible');

for (const user of Object.values(additionalUsers)) {
cy.headlessCreateUser(user);
Expand Down
4 changes: 2 additions & 2 deletions tests/cypress/e2e/actions_tasks2/test_default_attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ context('Test default value for an attribute', () => {
}

before(() => {
cy.headlessLogin();
cy.visit('/tasks/create');
cy.visit('/auth/login');
cy.headlessLogin({ nextURL: '/tasks/create' });
cy.get('#name').type(taskName);
cy.addNewLabel({ name: label }, attributes);
cy.selectFilesFromShare(serverFiles);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ context('Review pipeline feature', () => {
before(() => {
cy.headlessLogout();
cy.visit('/auth/login');
cy.get('.cvat-login-form-wrapper').should('exist').and('be.visible');

// register additional users
for (const user of Object.values(additionalUsers)) {
Expand Down
50 changes: 31 additions & 19 deletions tests/cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,25 @@ Cypress.Commands.add('selectFilesFromShare', (serverFiles) => {
selectServerFiles(serverFiles);
});

Cypress.Commands.add('headlessLogin', (username = Cypress.env('user'), password = Cypress.env('password')) => {
cy.visit('/auth/login');
cy.get('#root').should('exist').and('be.visible');
cy.window().then(async ($win) => {
await $win.cvat.server.login(username, password);
Cypress.Commands.add('headlessLogin', ({
username,
password,
nextURL,
} = {}) => {
cy.window().its('cvat', { timeout: 25000 }).should('not.be.undefined');
cy.window().then((win) => {
cy.headlessLogout().then(() => (
win.cvat.server.login(
username || Cypress.env('user'),
password || Cypress.env('password'),
).then(() => win.cvat.users.get({ self: true }).then((users) => {
if (nextURL) {
cy.visit(nextURL);
}

return users[0];
}))
));
});
});

Expand Down Expand Up @@ -345,33 +359,31 @@ Cypress.Commands.add('headlessDeleteTask', (taskID) => {
});

Cypress.Commands.add('headlessCreateUser', (userSpec) => {
cy.window().its('cvat', { timeout: 25000 }).should('not.be.undefined');
cy.intercept('POST', '/api/auth/register**', (req) => {
req.continue((res) => {
delete res.headers['set-cookie'];
req.continue((response) => {
delete response.headers['set-cookie'];
expect(response.statusCode).to.eq(201);
expect(response.body.username).to.eq(userSpec.username);
expect(response.body.email).to.eq(userSpec.email);
});
}).as('registerRequest');

cy.window().then(async ($win) => {
await $win.cvat.server.register(
return cy.window().then((win) => (
win.cvat.server.register(
userSpec.username,
userSpec.firstName,
userSpec.lastName,
userSpec.email,
userSpec.password,
[],
);
return cy.wrap();
});

cy.wait('@registerRequest').then((interception) => {
const { response } = interception;
expect(response.statusCode).to.eq(201);
expect(response.body.username).to.eq(userSpec.username);
expect(response.body.email).to.eq(userSpec.email);
});
)
));
});

Cypress.Commands.add('headlessLogout', () => {
// currently it is supposed that headlessLogout does not need core initialized to perform its logic
// this may be improved in the future, but now this behaviour is enough
cy.clearCookies();
});

Expand Down

0 comments on commit e568888

Please sign in to comment.