Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cypress and test login and recently played games #1

Merged
merged 31 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
490464d
Add cypress and test login and recently played games
gastongaiduk Dec 18, 2024
9233d65
Running tests after each pr push
gastongaiduk Dec 18, 2024
d585f8e
Fix tests.yml
gastongaiduk Dec 18, 2024
5c9e20e
Change tests.yml
gastongaiduk Dec 18, 2024
e129431
test
gastongaiduk Dec 18, 2024
12296f8
Tests adjustments
gastongaiduk Dec 18, 2024
a4822ea
Remove step
gastongaiduk Dec 18, 2024
eaff4d2
More adjustments
gastongaiduk Dec 18, 2024
3c9ab3c
Pass test encryption key
gastongaiduk Dec 18, 2024
69b5765
Try next approach
gastongaiduk Dec 18, 2024
b9d43ef
New approach
gastongaiduk Dec 18, 2024
a134135
Another approach?
gastongaiduk Dec 18, 2024
19068a9
Last approach
gastongaiduk Dec 18, 2024
c44ec39
Test now without using a default value on crypto.ts
gastongaiduk Dec 18, 2024
cea000b
Another approach
gastongaiduk Dec 18, 2024
cca03ae
New approach using local cypress install
gastongaiduk Dec 18, 2024
8989830
New approach
gastongaiduk Dec 18, 2024
b90ab2a
Final approach without using cypress-io/github-action
gastongaiduk Dec 18, 2024
f715af0
Final approach
gastongaiduk Dec 18, 2024
428fdf6
Let's try this one
gastongaiduk Dec 18, 2024
4a5100f
New approach
gastongaiduk Dec 18, 2024
bdd35d6
Fix tests.yml
gastongaiduk Dec 18, 2024
099b9ba
Remove wait on
gastongaiduk Dec 18, 2024
c1fb7cb
This is going to be it
gastongaiduk Dec 18, 2024
28526ae
Maybe now?
gastongaiduk Dec 18, 2024
3947bae
Maybe now
gastongaiduk Dec 18, 2024
a94f0b0
Small change
gastongaiduk Dec 18, 2024
6c9a9d6
Another approach
gastongaiduk Dec 18, 2024
986bbce
Another one
gastongaiduk Dec 18, 2024
60c6e3b
Another try
gastongaiduk Dec 18, 2024
355d0c8
Final changes before adding cypress
gastongaiduk Dec 18, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Tests

on:
workflow_dispatch:
pull_request:
types: [ opened, synchronize, reopened ]

jobs:
cypress_run:
runs-on: ubuntu-24.04
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Cypress run
uses: cypress-io/github-action@v6
with:
start: npm run dev:cypress
8 changes: 8 additions & 0 deletions cypress.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineConfig } from "cypress";

export default defineConfig({
e2e: {
baseUrl: 'http://localhost:5173/retroleaderboards',
defaultCommandTimeout: 10000,
},
});
48 changes: 48 additions & 0 deletions cypress/e2e/login_page.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
describe('login page', () => {
beforeEach(() => {
cy.clearAllLocalStorage()
})

it('unauthorized', () => {
cy.visit('/')

cy.url().should('include', '/login')
cy.contains('Authenticate')

cy.get('input[id="username"]').type('unauthorized')
cy.get('input[id="key"]').type('unauthorized-secret')

cy.intercept('GET', '**/API/API_GetUserRecentlyPlayedGames.php*', { statusCode: 401 }).as('getRecentlyPlayedGames')

cy.contains('Submit').click()

cy.wait('@getRecentlyPlayedGames').its('response.statusCode')
.should('equal', 401)

cy.on('window:alert', (txt) => {
expect(txt).to.contains('Bad credentials');
})

cy.url().should('include', '/login')
cy.contains('Authenticate')
})

it('success', () => {
cy.visit('/')

cy.url().should('include', '/login')
cy.contains('Authenticate')

cy.get('input[id="username"]').type('player')
cy.get('input[id="key"]').type('player-secret')

cy.intercept('GET', '**/API/API_GetUserRecentlyPlayedGames.php*', { fixture: 'no-played-games.json' }).as('getRecentlyPlayedGames')

cy.contains('Submit').click()

cy.wait('@getRecentlyPlayedGames').its('response.body')
.should('deep.equal', [])

cy.url().should('include', '/games')
})
})
80 changes: 80 additions & 0 deletions cypress/e2e/recently_played_games_page.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
describe('recently played games page', () => {
beforeEach(() => {
cy.clearAllLocalStorage()
})

it('no games played', () => {
cy.visit('/')

cy.url().should('include', '/login')
cy.contains('Authenticate')

cy.get('input[id="username"]').type('player')
cy.get('input[id="key"]').type('player-secret')

cy.intercept('GET', '**/API/API_GetUserRecentlyPlayedGames.php*', { fixture: 'no-played-games.json' }).as('getRecentlyPlayedGames')

cy.contains('Submit').click()

cy.wait('@getRecentlyPlayedGames').its('response.body')
.should('deep.equal', [])

cy.url().should('include', '/games')
cy.contains('No games played yet')
})

it('recently played games list with refresh', () => {
cy.visit('/')

cy.url().should('include', '/login')
cy.contains('Authenticate')

cy.get('input[id="username"]').type('example')
cy.get('input[id="key"]').type('example-secret')

cy.intercept('GET', '**/API/API_GetUserRecentlyPlayedGames.php*', { fixture: 'recently-played-games-1.json' }).as('getRecentlyPlayedGames')

cy.contains('Submit').click()

cy.wait('@getRecentlyPlayedGames').its('response.body')
.should('have.length', 1)

cy.url().should('include', '/games')
cy.contains('Colin McRae Rally')

cy.intercept('GET', '**/API/API_GetUserRecentlyPlayedGames.php*', { fixture: 'recently-played-games-2.json' }).as('getRecentlyPlayedGames')

cy.get('.refresh-button').click()

cy.wait('@getRecentlyPlayedGames').its('response.body')
.should('have.length', 2)

cy.get('.game-list li').eq(0).contains('Colin McRae Rally')
cy.get('.game-list li').eq(1).contains('Kirby & The Amazing Mirror')
})

it('logout', () => {
cy.visit('/')

cy.url().should('include', '/login')
cy.contains('Authenticate')

cy.get('input[id="username"]').type('player')
cy.get('input[id="key"]').type('player-secret')

cy.intercept('GET', '**/API/API_GetUserRecentlyPlayedGames.php*', { fixture: 'no-played-games.json' }).as('getRecentlyPlayedGames')

cy.contains('Submit').click()

cy.wait('@getRecentlyPlayedGames').its('response.body')
.should('deep.equal', [])

cy.contains('Logout').click()

cy.get('.confirm-button').click()

cy.getAllLocalStorage().should('be.empty')

cy.url().should('include', '/login')
})
})
1 change: 1 addition & 0 deletions cypress/fixtures/no-played-games.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
20 changes: 20 additions & 0 deletions cypress/fixtures/recently-played-games-1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[
{
"GameID": 16557,
"ConsoleID": 12,
"ConsoleName": "PlayStation",
"Title": "Colin McRae Rally",
"ImageIcon": "/Images/036840.png",
"ImageTitle": "/Images/036689.png",
"ImageIngame": "/Images/036690.png",
"ImageBoxArt": "/Images/065054.png",
"LastPlayed": "2024-12-16 22:22:54",
"AchievementsTotal": 104,
"NumPossibleAchievements": 104,
"PossibleScore": 436,
"NumAchieved": 53,
"ScoreAchieved": 173,
"NumAchievedHardcore": 53,
"ScoreAchievedHardcore": 173
}
]
38 changes: 38 additions & 0 deletions cypress/fixtures/recently-played-games-2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[
{
"GameID": 16557,
"ConsoleID": 12,
"ConsoleName": "PlayStation",
"Title": "Colin McRae Rally",
"ImageIcon": "/Images/036840.png",
"ImageTitle": "/Images/036689.png",
"ImageIngame": "/Images/036690.png",
"ImageBoxArt": "/Images/065054.png",
"LastPlayed": "2024-12-16 22:22:54",
"AchievementsTotal": 104,
"NumPossibleAchievements": 104,
"PossibleScore": 436,
"NumAchieved": 53,
"ScoreAchieved": 173,
"NumAchievedHardcore": 53,
"ScoreAchievedHardcore": 173
},
{
"GameID": 769,
"ConsoleID": 5,
"ConsoleName": "Game Boy Advance",
"Title": "Kirby & The Amazing Mirror",
"ImageIcon": "/Images/042405.png",
"ImageTitle": "/Images/033746.png",
"ImageIngame": "/Images/002643.png",
"ImageBoxArt": "/Images/014483.png",
"LastPlayed": "2024-11-24 21:31:43",
"AchievementsTotal": 55,
"NumPossibleAchievements": 55,
"PossibleScore": 311,
"NumAchieved": 21,
"ScoreAchieved": 31,
"NumAchievedHardcore": 21,
"ScoreAchievedHardcore": 31
}
]
37 changes: 37 additions & 0 deletions cypress/support/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/// <reference types="cypress" />
// ***********************************************
// This example commands.ts shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
//
//
// -- This is a parent command --
// Cypress.Commands.add('login', (email, password) => { ... })
//
//
// -- This is a child command --
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
//
//
// -- This is a dual command --
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
//
//
// -- This will overwrite an existing command --
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
//
// declare global {
// namespace Cypress {
// interface Chainable {
// login(email: string, password: string): Chainable<void>
// drag(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
// dismiss(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
// visit(originalFn: CommandOriginalFn, url: string, options: Partial<VisitOptions>): Chainable<Element>
// }
// }
// }
20 changes: 20 additions & 0 deletions cypress/support/e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// ***********************************************************
// This example support/e2e.ts is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************

// Import commands.js using ES2015 syntax:
import './commands'

// Alternatively you can use CommonJS syntax:
// require('./commands')
8 changes: 8 additions & 0 deletions cypress/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["es5", "dom"],
"types": ["cypress", "node"]
},
"include": ["**/*.ts"]
}
Loading