From 2efb929a0b1654723016a5163d25c6263034e1cc Mon Sep 17 00:00:00 2001 From: plumrx Date: Thu, 5 Nov 2020 23:16:31 +0800 Subject: [PATCH] test: add test case for login and register --- cypress.json | 4 ++- cypress/fixtures/register.json | 12 +++++++++ cypress/fixtures/users.json | 12 +++++++++ cypress/integration/like-follow.spec.js | 4 +-- cypress/integration/login.spec.js | 21 +++++++++++++++ cypress/integration/register.spec.js | 23 ++++++++++++++++ cypress/integration/tag.spec.js | 2 +- cypress/support/commands.js | 36 +++++++++++++++++++++++++ 8 files changed, 110 insertions(+), 4 deletions(-) create mode 100644 cypress/fixtures/register.json create mode 100644 cypress/fixtures/users.json create mode 100644 cypress/integration/login.spec.js create mode 100644 cypress/integration/register.spec.js diff --git a/cypress.json b/cypress.json index 158b080..ddb2b52 100644 --- a/cypress.json +++ b/cypress.json @@ -1,5 +1,7 @@ { - "baseUrl": "http://localhost:3000", + "baseUrl": "https://mutoe.github.io/vue3-realworld-example-app", "experimentalNetworkStubbing": true, "chromeWebSecurity": false + + } diff --git a/cypress/fixtures/register.json b/cypress/fixtures/register.json new file mode 100644 index 0000000..e50415e --- /dev/null +++ b/cypress/fixtures/register.json @@ -0,0 +1,12 @@ +{ + "user": { + "id": 122907, + "email": "plumrx@qq.com", + "createdAt": "2020-11-04T14:43:31.622Z", + "updatedAt": "2020-11-04T14:43:31.628Z", + "username": "plumrx", + "bio": null, + "image": null, + "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MTIyOTA3LCJ1c2VybmFtZSI6InBsdW1yeDIiLCJleHAiOjE2MDk2ODUwMTF9.A-gAjGYC9u7_mwjNK61HqeR3jwrFPN-UDvUnMjyJfBo" + } +} \ No newline at end of file diff --git a/cypress/fixtures/users.json b/cypress/fixtures/users.json new file mode 100644 index 0000000..8e50855 --- /dev/null +++ b/cypress/fixtures/users.json @@ -0,0 +1,12 @@ +{ + "loginPass": { + "username": "plumrx1", + "email": "plumrx1@qq.com", + "password": "12345678" + }, + "loginFailed": { + "username": "plumrx2", + "email": "plumrx2@qq.com", + "password": "12345678" + } +} \ No newline at end of file diff --git a/cypress/integration/like-follow.spec.js b/cypress/integration/like-follow.spec.js index f80e0a2..0577ebc 100644 --- a/cypress/integration/like-follow.spec.js +++ b/cypress/integration/like-follow.spec.js @@ -1,4 +1,4 @@ -describe('test for like-follow mode', () => { +describe('test for like-follow', () => { beforeEach(() => { cy.server() cy.route2('GET', /articles\?tag=butt/, { fixture: 'article_of_tag' }).as('article_of_tag') @@ -20,7 +20,7 @@ describe('test for like-follow mode', () => { .should('contain.text', ' Sign in ') }) - it.only('no-login:follow author', () => { + it('no-login:follow author', () => { cy.get('.article-preview:first') .click() diff --git a/cypress/integration/login.spec.js b/cypress/integration/login.spec.js new file mode 100644 index 0000000..68a8f75 --- /dev/null +++ b/cypress/integration/login.spec.js @@ -0,0 +1,21 @@ +describe('test for login', () => { + beforeEach(() => { + cy.server() + cy.route2('GET', /articles\?tag=butt/, { fixture: 'article_of_tag' }).as('article_of_tag') + cy.route2('GET', /articles\?/, { fixture: 'articles.json' }).as('getArticles') + cy.route2('GET', /articles\//, { fixture: 'article.json' }).as('getArticle') + cy.route2('GET', /tags/, { fixture: 'tags.json' }).as('getTags') + + cy.visit('/') + }) + + it('login in home page', () => { + // const usrname = 'plumrx1' + // const email = 'plumrx1@qq.com' + // const password = '12345678' + + cy.fixture('users.json').then(users => { + cy.login(users.loginPass) + }) + }) +}) diff --git a/cypress/integration/register.spec.js b/cypress/integration/register.spec.js new file mode 100644 index 0000000..31194f4 --- /dev/null +++ b/cypress/integration/register.spec.js @@ -0,0 +1,23 @@ +describe('test for register', () => { + beforeEach(() => { + cy.server() + cy.route2('POST', /users/, { fixture: 'register.json' }).as('register') + cy.route2('GET', /articles\?tag=butt/, { fixture: 'article_of_tag' }).as('article_of_tag') + cy.route2('GET', /articles\?/, { fixture: 'articles.json' }).as('getArticles') + cy.route2('GET', /articles\//, { fixture: 'article.json' }).as('getArticle') + cy.route2('GET', /tags/, { fixture: 'tags.json' }).as('getTags') + + cy.visit('/') + }) + + it('ligin in home page', () => { + // click logup button in home page + const usrname = 'plumrx' + const email = 'plumrx@qq.com' + const password = '12345678' + cy.register(usrname, email, password) + + cy.get('.navbar') + .should('contain', usrname) + }) +}) diff --git a/cypress/integration/tag.spec.js b/cypress/integration/tag.spec.js index ce1fe64..ecf785c 100644 --- a/cypress/integration/tag.spec.js +++ b/cypress/integration/tag.spec.js @@ -1,4 +1,4 @@ -describe('test for tag mode', () => { +describe('test for tag', () => { beforeEach(() => { cy.server() cy.route2('GET', /articles\?tag=butt/, { fixture: 'article_of_tag' }).as('article_of_tag') diff --git a/cypress/support/commands.js b/cypress/support/commands.js index ca4d256..961f6a0 100644 --- a/cypress/support/commands.js +++ b/cypress/support/commands.js @@ -23,3 +23,39 @@ // // -- This will overwrite an existing command -- // Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... }) + +Cypress.Commands.add('login', (user) => { + // click sign in button in home page + cy.get('li.nav-item a.nav-link') + .contains(' Sign in') + .click() + + cy.get('[type="email"]') + .type(user.email) + cy.get('[type="password"]') + .type(user.password) + cy.get('[type="submit"]') + .contains(' Sign in ') + .click() + + cy.get('h1.logo-font') + .should('contain', ' conduit ') + cy.get('li.nav-item:last') + .should('contain.text', user.username) +}) + +Cypress.Commands.add('register', (usrname, email, password) => { + // click sign up button in home page + cy.get('li.nav-item a.nav-link') + .contains(' Sign up') + .click() + // []属性选择器 + cy.get('[placeholder="Your Name"]') + .type(usrname) + cy.get('[placeholder="Email"]') + .type(email) + cy.get('[placeholder="Password"]') + .type(password) + cy.get('[type="submit"]') + .click() +})