Skip to content

Commit

Permalink
add login Specs && GitHub Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Oct 1, 2024
1 parent 56a1141 commit 140fb6b
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 81 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/cypress.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Cypress Tests

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
cypress-run:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [14.x]

steps:
- uses: actions/checkout@v3

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}

- name: Install dependencies
run: npm ci

- name: Run Cypress tests
run: npx cypress run
29 changes: 13 additions & 16 deletions cypress.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { defineConfig } = require("cypress");
const baseUrl = process.env.BASE_URL || "https://www.saucedemo.com/"
const apiURL = process.env.API_URL || "http://localhost:3000/api/"
const dbHost = process.env.DBHOST || "localhost"
const dbPassword = process.env.DBPASSWORD || "password"
const baseUrl = process.env.BASE_URL || "https://opensource-demo.orangehrmlive.com/web/index.php/auth/"
const apiURL = process.env.API_URL || "https://opensource-demo.orangehrmlive.com/web/index.php/auth/"
// const dbHost = process.env.DBHOST || "localhost"
// const dbPassword = process.env.DBPASSWORD || "password"


module.exports = defineConfig({
Expand All @@ -19,26 +19,23 @@ module.exports = defineConfig({
viewportWidth: 1920,
baseUrl: baseUrl,
reporter: 'junit',
video: true,
reporterOptions: {
mochaFile: 'results/my-test-output-[hash].xml',
toConsole: false,
},
video: false,
retries: {
runMode: 2,
openMode: 0
},
//numTestsKeptInMemory: 0
testIsolation: false,
experimentalMemoryManagement: true
},
env: {
apiUrl: apiURL,
db: {
user: "user",
host: dbHost,
database: "postgres",
password: dbPassword,
port: 5432
}
// db: {
// user: "user",
// host: dbHost,
// database: "postgres",
// password: dbPassword,
// port: 5432
// }
},
})
17 changes: 10 additions & 7 deletions cypress/e2e/login.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ describe('Login Functionality', () => {
beforeEach(() => {
Cypress.on("uncaught:exception", () => {
return false;
})
})
})


it('Should login successfully going to home', () => {
const homeTittle = 'Swag Labs'
cy.login(loginData.user.userName, loginData.user.password)
cy.url().should('include', '/inventory.html')
cy.get('div.app_logo').invoke('text').then((text)=>{
expect(text).to.equal(homeTittle)
})
cy.get('h6.oxd-text--h6').as('TitlePageHome')
.get('@TitlePageHome').should('have.text', 'Dashboard')
})

it('Should show an error message when entering invalid credentials', () => {
const errorMessage = 'Epic sadface: Username and password do not match any user in this service'
cy.login('invalidUser', 'invalidPass', true)
cy.get('div.oxd-alert-content--error').as('messageError')
.get('@messageError').should('have.text', 'Invalid credentials')
})
})
4 changes: 2 additions & 2 deletions cypress/fixtures/login.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"user": {
"userName": "standard_user",
"password": "secret_sauce"
"userName": "Admin",
"password": "admin123"
}
}

65 changes: 53 additions & 12 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,55 @@
Cypress.Commands.add("login", (userName, password) => {
cy.session([userName, password], () => {
cy.visit('/', { failOnStatusCode: false })
cy.get('#user-name')
.should('be.visible')
.type(userName)
cy.get('#password')
.should('be.visible')
.type(password)
cy.get('#login-button')
.click()

Cypress.Commands.add("loginSession", (userName, password) => {
cy.session([userName, password], () => {
cy.visit('/', { failOnStatusCode: false })
cy.get('input[name="username"]')
.should('be.visible')
.type(userName)
cy.get('input[name="password"]')
.should('be.visible')
.type(password)
cy.get('button[type="submit"]')
.contains('Login')
.click()
})
})

Cypress.Commands.add("login", (userName, password, noWait) => {
//cy.intercept("POST", "/auth/validate").as('postLogin')
cy.clearCookies();
cy.clearLocalStorage();
cy.visit('/login', { failOnStatusCode: false })
cy.get('input[name="username"]')
.should('be.visible')
.type(userName)
cy.get('input[name="password"]')
.should('be.visible')
.type(password)
cy.get('button[type="submit"]')
.contains('Login')
.click()
// if (!noWait) {
// return cy.wait('@postLogin')
// }
})


Cypress.Commands.add("logoutRequest", () => {
cy.getUserToken().then((token) => {
cy.request({
method: "GET",
url: `${Cypress.env("apiUrl")}logout`,
// body: { "accessToken": token },
}).then((response) => {
expect(response.status).to.equal(200)
})
})
})


Cypress.Commands.add('getUserToken', () => {
cy.window().then((win) => {
const userToken = win.localStorage.getItem('/core/i18n/messages');
cy.log(userToken)
return cy.wrap(userToken)
});
});
Loading

0 comments on commit 140fb6b

Please sign in to comment.