cypress E2e login to logout test done

This commit is contained in:
chiayin
2023-02-02 15:37:04 +08:00
parent 65755def27
commit e3bd32383e
6 changed files with 27 additions and 24 deletions

4
.gitignore vendored
View File

@@ -15,6 +15,8 @@ coverage
*.local *.local
/dist /dist
# Cypress
cypress.env.json
/cypress/videos/ /cypress/videos/
/cypress/screenshots/ /cypress/screenshots/
@@ -32,6 +34,6 @@ vscode
*.sw? *.sw?
# local env files # local env files
.env.demo
.env.local .env.local
.env.*.local .env.*.local
.env.demo

View File

@@ -4,6 +4,6 @@ const { defineConfig } = require("cypress");
module.exports = defineConfig({ module.exports = defineConfig({
e2e: { e2e: {
specPattern: "cypress/e2e/**/*.{cy,spec}.{js,jsx,ts,tsx}", specPattern: "cypress/e2e/**/*.{cy,spec}.{js,jsx,ts,tsx}",
baseUrl: '', // baseUrl: "" // 設定方式請看內部文件
}, },
}); });

View File

@@ -1,4 +1,4 @@
// 之後要優化: 加入生命週期、每一個測試步驟要分開寫,不寫在同一個 it 裡面、將測試寫成 report 輸出成 html(嘗試) // 之後要優化: 每一個測試步驟要分開寫,不寫在同一個 it 裡面、將測試寫成 report 輸出成 html(嘗試)
describe("Login to Logout", () => { describe("Login to Logout", () => {
before(() => { before(() => {
@@ -7,22 +7,24 @@ describe("Login to Logout", () => {
cy.url().should('include', 'login') // url path 要有 'Login',確定進入 login page cy.url().should('include', 'login') // url path 要有 'Login',確定進入 login page
}) })
it("login error username and password", () => { it("test login success and error", () => {
// 驗證帳密是否刪除前後空白、輸入錯誤是否彈跳驗證、Button display // 驗證帳密是否刪除前後空白、錯誤帳密是否顯示驗證、Button display
cy.get('.btn-lg').should('be.disabled'); cy.fixture('users/id-not-exists').then(({username, password}) => {
cy.get('#account').should('have.focus').type(' test '); cy.get('.btn-lg').should('be.disabled');
cy.get('.btn-lg').should('be.disabled'); cy.get('#account').should('have.focus').type(username);
cy.get('#password').type(' test '); cy.get('.btn-lg').should('be.disabled');
cy.get('.btn-lg').click(); cy.get('#password').type(password);
cy.get('#account').should('have.value', 'test'); cy.get('.btn-lg').click();
cy.get('#password').should('have.value', 'test'); cy.get('#account').should('have.value', 'test');
cy.get('form').submit(); cy.get('#password').should('have.value', 'test');
cy.contains("p", "Incorrect account or password."); cy.get('form').submit();
cy.url().should('include', 'login'); cy.contains("p", "Incorrect account or password.");
cy.url().should('include', 'login');
});
// 正確帳密登入 // 正確帳密登入
cy.get('#account').clear().type(''); cy.get('#account').clear().type(Cypress.env('user').username);
cy.get('#password').clear().type(''); cy.get('#password').clear().type(Cypress.env('user').password);
cy.get('.btn-lg').click(); cy.get('.btn-lg').click();
cy.get('form').submit(); // 選取 form 表單並發送 cy.get('form').submit(); // 選取 form 表單並發送

View File

@@ -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"
}

View File

@@ -0,0 +1,4 @@
{
"username": " test ",
"password": " test "
}

4
package-lock.json generated
View File

@@ -1,11 +1,11 @@
{ {
"name": "lucia-frontend", "name": "frontend",
"version": "0.2.0", "version": "0.2.0",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "lucia-frontend", "name": "frontend",
"version": "0.2.0", "version": "0.2.0",
"dependencies": { "dependencies": {
"autoprefixer": "^10.4.13", "autoprefixer": "^10.4.13",