From d90cdb31a36b77e6ad1ea1fdd64c3ba12144339a Mon Sep 17 00:00:00 2001 From: Walmyr Date: Mon, 15 Nov 2021 17:00:12 +0100 Subject: [PATCH 1/4] Refactor login tests and custom command The login tests now have better assertions. And the login custom command can now use the cy.session experimental feature, which allows restoring the session of a user, which speeds up tests. But, for the login tests, we don't want any cache to be restored. This is why for the login tests we pass { cacheSession: false } --- cypress.json | 1 + cypress/integration/login.spec.js | 39 +++++++++++++++++++++++-------- cypress/support/commands.js | 24 +++++++++++++------ 3 files changed, 47 insertions(+), 17 deletions(-) diff --git a/cypress.json b/cypress.json index 2cb3cd47..429656ca 100644 --- a/cypress.json +++ b/cypress.json @@ -1,4 +1,5 @@ { + "experimentalSessionSupport": true, "baseUrl": "http://localhost:9000", "fixturesFolder": false, "pluginsFile": false diff --git a/cypress/integration/login.spec.js b/cypress/integration/login.spec.js index d582b2c3..26e5c9fc 100644 --- a/cypress/integration/login.spec.js +++ b/cypress/integration/login.spec.js @@ -1,47 +1,66 @@ describe('Login', () => { - beforeEach(() => cy.visit('/')) + const userLoginData = { + user: Cypress.env('user'), + password: Cypress.env('password'), + useCachedSession: { + cacheSession: false + } + } + + const { user, password, useCachedSession } = userLoginData it('logs in successfully', () => { - cy.login() + cy.intercept('get', '/models?userId=*').as('getUserModels') + + cy.login(user, password, useCachedSession) + + cy.wait('@getUserModels') cy.url() - .should('be.equal', `${Cypress.config('baseUrl')}/#!/main`) + .should('be.equal', `${Cypress.config('baseUrl')}/#!/main`) cy.contains('h2', 'Modelagens') .should('be.visible') }) context('Form validations', () => { it('alerts on invalid user email', () => { - cy.login('invalid#user.com', Cypress.env('password')) + cy.login('invalid#user.com', password, useCachedSession) - cy.contains('.alert', 'Preencha os campos em vermelho') + cy.contains('.alert-danger', 'Preencha os campos em vermelho') .should('be.visible') + cy.get('#userEmail') + .should('have.class', 'error') cy.get('.error') .should('have.length', 1) .and('be.visible') }) it('alerts when clicking ENTER without filling the form', () => { + cy.visit('/') cy.contains('button', 'Entrar') .click() - cy.contains('.alert', 'Preencha os campos em vermelho') + cy.contains('.alert-danger', 'Preencha os campos em vermelho') .should('be.visible') + cy.get('#userEmail') + .should('have.class', 'error') + cy.get('#userPassword') + .should('have.class', 'error') cy.get('.error') .should('have.length', 2) .and('be.visible') }) it('alerts on invalid user', () => { - cy.login('invalid@user.com', Cypress.env('password')) + cy.login('invalid@user.com', password, useCachedSession) - cy.contains('.alert', 'Login ou senha incorretos') + cy.contains('.alert-danger', 'Login ou senha incorretos') .should('be.visible') }) it('alerts on invalid password', () => { - cy.login(Cypress.env('user'), 'invalid-pwd') + cy.login(Cypress.env('user'), 'invalid-pwd', useCachedSession) - cy.contains('.alert', 'Login ou senha incorretos') + cy.contains('.alert-danger', 'Login ou senha incorretos') .should('be.visible') }) }) diff --git a/cypress/support/commands.js b/cypress/support/commands.js index 291dce30..e34d39a3 100644 --- a/cypress/support/commands.js +++ b/cypress/support/commands.js @@ -26,12 +26,22 @@ Cypress.Commands.add('login', ( user = Cypress.env('user'), - password = Cypress.env('password') + password = Cypress.env('password'), + { cacheSession = true } = {} ) => { - cy.get('#userEmail') - .type(user) - cy.get('#userPassword') - .type(password, { log: false }) - cy.contains('button', 'Entrar') - .click() + const fillLoginFormAndSubmit = () => { + cy.visit('/') + cy.get('#userEmail') + .type(user) + cy.get('#userPassword') + .type(password, { log: false }) + cy.contains('button', 'Entrar') + .click() + } + + if (cacheSession) { + cy.session([user], fillLoginFormAndSubmit) + } else { + fillLoginFormAndSubmit() + } }) From 5cc04c1ff16acc0c9d8018b0e10da52616479c82 Mon Sep 17 00:00:00 2001 From: Walmyr Date: Mon, 15 Nov 2021 17:00:34 +0100 Subject: [PATCH 2/4] Add tests for models Such tests make use of the refactored login command, which means that only the first tests needs to log in via the GUI, while others benefit from the session to be restored from the cache. In this test suite, I've also create a custom command to cleanup all existing models on the beforeEach hook so that tests always start in a fresh state. --- cypress.json | 1 + cypress/integration/models.spec.js | 56 ++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 cypress/integration/models.spec.js diff --git a/cypress.json b/cypress.json index 429656ca..f8777293 100644 --- a/cypress.json +++ b/cypress.json @@ -1,4 +1,5 @@ { + "apiUrl": "http://localhost:3000", "experimentalSessionSupport": true, "baseUrl": "http://localhost:9000", "fixturesFolder": false, diff --git a/cypress/integration/models.spec.js b/cypress/integration/models.spec.js new file mode 100644 index 00000000..f916db66 --- /dev/null +++ b/cypress/integration/models.spec.js @@ -0,0 +1,56 @@ +describe('Models', () => { + beforeEach(() => { + cy.intercept('get', '/models?userId=*').as('getUserModels') + cy.login() + cy.visit('/#!/main') + cy.cleanUpUserModels() + cy.contains('a', 'Nova Modelagem').click() + cy.get('create-model-modal').should('be.visible') + }) + + it('closes the "New Model" modal', () => { + cy.contains('button', 'Cancelar').click() + cy.get('create-model-modal').should('not.exist') + }) + + it('alerts when clicking SAVE without filling the title', () => { + cy.contains('button', 'Salvar').click() + cy.get('#name').should('have.class', 'error') + }) + + context('Model creation', () => { + const modelTitle = 'User' + + beforeEach(() => cy.get('#name').type(modelTitle)) + + it('creates a conceptual model', () => { + cy.contains('button', 'Salvar').click() + + cy.contains('h2', `Modelo conceitual de: ${modelTitle}`) + .should('be.visible') + }) + + it('creates a logical model', () => { + cy.get('.modelselect').click() + cy.contains('li span', 'Lógico').click() + cy.contains('button', 'Salvar').click() + + cy.contains('h2', `Modelo lógico de: ${modelTitle}`) + .should('be.visible') + }) + }) +}) + +Cypress.Commands.add('cleanUpUserModels', () => { + cy.wait('@getUserModels').then(userModels => { + cy.request('GET', userModels.response.url).then(userModelsResponse => { + userModelsResponse.body.forEach(model => { + cy.request( + 'DELETE', + `${Cypress.config('apiUrl')}/models/:modelId?modelId=${model._id}` + ) + }) + }) + }) + cy.reload() +}) From 85014730f9a36816189a005fdb468e7f8ee3daa9 Mon Sep 17 00:00:00 2001 From: Walmyr Date: Wed, 17 Nov 2021 12:22:32 +0100 Subject: [PATCH 3/4] Minor adjustments --- cypress/integration/login.spec.js | 2 +- cypress/integration/models.spec.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cypress/integration/login.spec.js b/cypress/integration/login.spec.js index 26e5c9fc..9b7b47e2 100644 --- a/cypress/integration/login.spec.js +++ b/cypress/integration/login.spec.js @@ -10,7 +10,7 @@ describe('Login', () => { const { user, password, useCachedSession } = userLoginData it('logs in successfully', () => { - cy.intercept('get', '/models?userId=*').as('getUserModels') + cy.intercept('GET', '/models?userId=*').as('getUserModels') cy.login(user, password, useCachedSession) diff --git a/cypress/integration/models.spec.js b/cypress/integration/models.spec.js index f916db66..12630db3 100644 --- a/cypress/integration/models.spec.js +++ b/cypress/integration/models.spec.js @@ -1,9 +1,9 @@ describe('Models', () => { beforeEach(() => { - cy.intercept('get', '/models?userId=*').as('getUserModels') + cy.intercept('GET', '/models?userId=*').as('getUserModels') cy.login() cy.visit('/#!/main') - cy.cleanUpUserModels() + cy.cleanUpUserModelsAndReoload() cy.contains('a', 'Nova Modelagem').click() cy.get('create-model-modal').should('be.visible') }) @@ -41,7 +41,7 @@ describe('Models', () => { }) }) -Cypress.Commands.add('cleanUpUserModels', () => { +Cypress.Commands.add('cleanUpUserModelsAndReoload', () => { cy.wait('@getUserModels').then(userModels => { cy.request('GET', userModels.response.url).then(userModelsResponse => { userModelsResponse.body.forEach(model => { From 02c5314f06e26d04daf4965b6b51138941ee4097 Mon Sep 17 00:00:00 2001 From: Walmyr Date: Wed, 17 Nov 2021 14:26:03 +0100 Subject: [PATCH 4/4] Overwrite cypress request timeout --- cypress.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cypress.json b/cypress.json index f8777293..62257e46 100644 --- a/cypress.json +++ b/cypress.json @@ -3,5 +3,6 @@ "experimentalSessionSupport": true, "baseUrl": "http://localhost:9000", "fixturesFolder": false, - "pluginsFile": false + "pluginsFile": false, + "requestTimeout": 10000 }