From b577a6bf2fc8c54cb07fa8a2886338fab85031b2 Mon Sep 17 00:00:00 2001 From: helenanull Date: Thu, 4 Mar 2021 22:36:03 +0200 Subject: [PATCH] more improvements --- .../{settings.spec.js => article.spec.js} | 4 +- cypress/integration/register.spec.js | 44 +++++++++++++++++++ cypress/integration/register_aliases.spec.js | 43 ++++++++++++++++++ cypress/selectors/header.sel.js | 3 ++ cypress/selectors/register.sel.js | 6 +++ cypress/support/index.js | 3 -- cypress/support/register.cmd.js | 1 - 7 files changed, 99 insertions(+), 5 deletions(-) rename cypress/integration/{settings.spec.js => article.spec.js} (88%) create mode 100644 cypress/integration/register.spec.js create mode 100644 cypress/integration/register_aliases.spec.js create mode 100644 cypress/selectors/header.sel.js create mode 100644 cypress/selectors/register.sel.js diff --git a/cypress/integration/settings.spec.js b/cypress/integration/article.spec.js similarity index 88% rename from cypress/integration/settings.spec.js rename to cypress/integration/article.spec.js index 0e2de88..f740d89 100644 --- a/cypress/integration/settings.spec.js +++ b/cypress/integration/article.spec.js @@ -3,7 +3,9 @@ import article from '../selectors/article.sel' describe('Article', () => { beforeEach(() => { - cy.login() + cy.register().then((email) => { + cy.login(email) + }) cy.visit('/editor/') }) diff --git a/cypress/integration/register.spec.js b/cypress/integration/register.spec.js new file mode 100644 index 0000000..e17df69 --- /dev/null +++ b/cypress/integration/register.spec.js @@ -0,0 +1,44 @@ +import registration from '../selectors/register.sel' +import header from '../selectors/header.sel' + +describe('Register', () => { + // actually we should not use let here, check register_aliases.spec + // https://docs.cypress.io/guides/core-concepts/variables-and-aliases.html#Aliases + let username + let email + + beforeEach(() => { + // we need random username and email each test + username = `cy${Math.random().toString().slice(2, 8)}` + email = `${username}@mailinator.com` + cy.visit('/register') + }) + + it('can register a new account', () => { + // added delay as sometimes it can make tests flaky if typing too fast (default is 10) + cy.get(registration.usernameField).type(username, { delay: 50 }) + cy.get(registration.emailField).type(email) + cy.get(registration.passwordField).type('Testtest1') + cy.get(registration.signUpButton).click() + cy.get(header.settingsLink).should('be.visible') + }) + + it('check registration request body and response', () => { + cy.intercept('/api/users').as('loginRequest') + cy.get(registration.usernameField).type(username) + cy.get(registration.emailField).type(email) + cy.get(registration.passwordField).type('Testtest1{enter}') + + cy.wait('@loginRequest').then((xhr) => { + // check request body + expect(xhr.request.body.user.email).to.eq(email) + expect(xhr.request.body.user.password).to.eq('Testtest1') + expect(xhr.request.body.user.username).to.eq(username) + // check response body + expect(xhr.response.body.user.email).to.eq(email) + expect(xhr.response.body.user.id).not.to.eq(null) + expect(xhr.response.body.user.token).not.to.eq(null) + }) + cy.get(header.settingsLink).should('be.visible') + }) +}) diff --git a/cypress/integration/register_aliases.spec.js b/cypress/integration/register_aliases.spec.js new file mode 100644 index 0000000..664e641 --- /dev/null +++ b/cypress/integration/register_aliases.spec.js @@ -0,0 +1,43 @@ +import registration from '../selectors/register.sel' +import header from '../selectors/header.sel' + +describe('Register', () => { + beforeEach(() => { + // we need random username and email each test + const random = `cy${Math.random().toString().slice(2, 8)}` + // use alias instead of let + cy.wrap(random).as('username') + cy.wrap(`${random}@mailinator.com`).as('email') + cy.visit('/register') + }) + + // this is not an arrow function anymore => + // https://docs.cypress.io/guides/core-concepts/variables-and-aliases.html#Avoiding-the-use-of-this + it('can register a new account', function () { + // added delay as sometimes it can make tests flaky if typing too fast (default is 10) + cy.get(registration.usernameField).type(this.username, { delay: 50 }) + cy.get(registration.emailField).type(this.email) + cy.get(registration.passwordField).type('Testtest1') + cy.get(registration.signUpButton).click() + cy.get(header.settingsLink).should('be.visible') + }) + + it('check registration request body and response', function () { + cy.intercept('/api/users').as('loginRequest') + cy.get(registration.usernameField).type(this.username) + cy.get(registration.emailField).type(this.email) + cy.get(registration.passwordField).type('Testtest1{enter}') + + cy.wait('@loginRequest').then((xhr) => { + // check request body + expect(xhr.request.body.user.email).to.eq(this.email) + expect(xhr.request.body.user.password).to.eq('Testtest1') + expect(xhr.request.body.user.username).to.eq(this.username) + // check response body + expect(xhr.response.body.user.email).to.eq(this.email) + expect(xhr.response.body.user.id).not.to.eq(null) + expect(xhr.response.body.user.token).not.to.eq(null) + }) + cy.get(header.settingsLink).should('be.visible') + }) +}) diff --git a/cypress/selectors/header.sel.js b/cypress/selectors/header.sel.js new file mode 100644 index 0000000..b9dfa6a --- /dev/null +++ b/cypress/selectors/header.sel.js @@ -0,0 +1,3 @@ +module.exports = { + settingsLink: '[ui-sref*=settings]' +} diff --git a/cypress/selectors/register.sel.js b/cypress/selectors/register.sel.js new file mode 100644 index 0000000..87cbfe7 --- /dev/null +++ b/cypress/selectors/register.sel.js @@ -0,0 +1,6 @@ +module.exports = { + usernameField: '[ng-model*=username]', + emailField: '[ng-model*=email]', + passwordField: '[ng-model*=password]', + signUpButton: '.btn' +} diff --git a/cypress/support/index.js b/cypress/support/index.js index 2886cf2..bc17ddf 100644 --- a/cypress/support/index.js +++ b/cypress/support/index.js @@ -19,6 +19,3 @@ import './register.cmd' // Alternatively you can use CommonJS syntax: // require('./commands') -before(() => { - cy.register() -}) diff --git a/cypress/support/register.cmd.js b/cypress/support/register.cmd.js index 97d3007..1228b54 100644 --- a/cypress/support/register.cmd.js +++ b/cypress/support/register.cmd.js @@ -14,7 +14,6 @@ Cypress.Commands.add('register', () => { }) .then((response) => { expect(response.status).to.eq(200) - Cypress.env('email', email) return email }) })