From e3bd32383e1631b744d350e887c120b0a201be23 Mon Sep 17 00:00:00 2001 From: chiayin Date: Thu, 2 Feb 2023 15:37:04 +0800 Subject: [PATCH] cypress E2e login to logout test done --- .gitignore | 4 ++- cypress.config.js | 2 +- cypress/e2e/loginToLogout.cy.js | 32 ++++++++++++----------- cypress/fixtures/example.json | 5 ---- cypress/fixtures/users/id-not-exists.json | 4 +++ package-lock.json | 4 +-- 6 files changed, 27 insertions(+), 24 deletions(-) delete mode 100644 cypress/fixtures/example.json create mode 100644 cypress/fixtures/users/id-not-exists.json diff --git a/.gitignore b/.gitignore index 78c0931..eb5c088 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,8 @@ coverage *.local /dist +# Cypress +cypress.env.json /cypress/videos/ /cypress/screenshots/ @@ -32,6 +34,6 @@ vscode *.sw? # local env files +.env.demo .env.local .env.*.local -.env.demo diff --git a/cypress.config.js b/cypress.config.js index 7064c99..10fbd84 100644 --- a/cypress.config.js +++ b/cypress.config.js @@ -4,6 +4,6 @@ const { defineConfig } = require("cypress"); module.exports = defineConfig({ e2e: { specPattern: "cypress/e2e/**/*.{cy,spec}.{js,jsx,ts,tsx}", - baseUrl: '', + // baseUrl: "" // 設定方式請看內部文件 }, }); diff --git a/cypress/e2e/loginToLogout.cy.js b/cypress/e2e/loginToLogout.cy.js index 32ef55c..ca4da0e 100644 --- a/cypress/e2e/loginToLogout.cy.js +++ b/cypress/e2e/loginToLogout.cy.js @@ -1,4 +1,4 @@ -// 之後要優化: 加入生命週期、每一個測試步驟要分開寫,不寫在同一個 it 裡面、將測試寫成 report 輸出成 html(嘗試) +// 之後要優化: 每一個測試步驟要分開寫,不寫在同一個 it 裡面、將測試寫成 report 輸出成 html(嘗試) describe("Login to Logout", () => { before(() => { @@ -7,22 +7,24 @@ describe("Login to Logout", () => { cy.url().should('include', 'login') // url path 要有 'Login',確定進入 login page }) - it("login error username and password", () => { - // 驗證帳密是否刪除前後空白、輸入錯誤是否彈跳驗證、Button display - cy.get('.btn-lg').should('be.disabled'); - cy.get('#account').should('have.focus').type(' test '); - cy.get('.btn-lg').should('be.disabled'); - cy.get('#password').type(' test '); - cy.get('.btn-lg').click(); - cy.get('#account').should('have.value', 'test'); - cy.get('#password').should('have.value', 'test'); - cy.get('form').submit(); - cy.contains("p", "Incorrect account or password."); - cy.url().should('include', 'login'); + it("test login success and error", () => { + // 驗證帳密是否刪除前後空白、錯誤帳密是否顯示驗證、Button display + cy.fixture('users/id-not-exists').then(({username, password}) => { + cy.get('.btn-lg').should('be.disabled'); + cy.get('#account').should('have.focus').type(username); + cy.get('.btn-lg').should('be.disabled'); + cy.get('#password').type(password); + cy.get('.btn-lg').click(); + cy.get('#account').should('have.value', 'test'); + cy.get('#password').should('have.value', 'test'); + cy.get('form').submit(); + cy.contains("p", "Incorrect account or password."); + cy.url().should('include', 'login'); + }); // 正確帳密登入 - cy.get('#account').clear().type(''); - cy.get('#password').clear().type(''); + cy.get('#account').clear().type(Cypress.env('user').username); + cy.get('#password').clear().type(Cypress.env('user').password); cy.get('.btn-lg').click(); cy.get('form').submit(); // 選取 form 表單並發送 diff --git a/cypress/fixtures/example.json b/cypress/fixtures/example.json deleted file mode 100644 index 02e4254..0000000 --- a/cypress/fixtures/example.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "name": "Using fixtures to represent data", - "email": "hello@cypress.io", - "body": "Fixtures are a great way to mock data for responses to routes" -} diff --git a/cypress/fixtures/users/id-not-exists.json b/cypress/fixtures/users/id-not-exists.json new file mode 100644 index 0000000..c0b5e31 --- /dev/null +++ b/cypress/fixtures/users/id-not-exists.json @@ -0,0 +1,4 @@ +{ + "username": " test ", + "password": " test " +} diff --git a/package-lock.json b/package-lock.json index 10ea8bb..d0232f2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { - "name": "lucia-frontend", + "name": "frontend", "version": "0.2.0", "lockfileVersion": 2, "requires": true, "packages": { "": { - "name": "lucia-frontend", + "name": "frontend", "version": "0.2.0", "dependencies": { "autoprefixer": "^10.4.13",