Skip to content

Commit

Permalink
tests game
Browse files Browse the repository at this point in the history
  • Loading branch information
uo283182 committed Apr 27, 2024
1 parent eeb71b7 commit 8d106b6
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 1 deletion.
11 changes: 11 additions & 0 deletions webapp/e2e/features/game.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Feature: A user playing a game

Scenario: The authenticated user plays a game and clicks the correct answer
Given An authenticated user
When I navigate to the game page and I click the correct answer
Then The button turns green and the number of questions answered correctly increments one

Scenario: The authenticated user plays a game and clicks an incorrect answer
Given An authenticated user
When I navigate to the game page and I click an incorrect answer
Then The button turns red and the number of questions answered correctly is 0
2 changes: 1 addition & 1 deletion webapp/e2e/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
testMatch: ["**/steps/*.js"],
testTimeout: 30000,
testTimeout: 50000,
setupFilesAfterEnv: ["expect-puppeteer"]
}
116 changes: 116 additions & 0 deletions webapp/e2e/steps/game-steps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
const puppeteer = require('puppeteer');
const { defineFeature, loadFeature }=require('jest-cucumber');
const setDefaultOptions = require('expect-puppeteer').setDefaultOptions
const feature = loadFeature('./features/game.feature');

let page;
let browser;

defineFeature(feature, test => {

beforeEach(async () => {
browser = process.env.GITHUB_ACTIONS
? await puppeteer.launch()
: await puppeteer.launch({ headless: false, slowMo: 20 });
page = await browser.newPage();
//Way of setting up the timeout
setDefaultOptions({ timeout: 10000 })

await page
.goto("http://localhost:3000", {
waitUntil: "networkidle0",
})
.catch(() => {});
});

test('The authenticated user plays a game and clicks the correct answer', ({given, when, then}) => {
let username;
let password;

given('An authenticated user', async () => {
username = "game1";
password = "game";
await expect(page).toClick('button', { text: 'SignUp' });
await expect(page).toFill('input[name="username"]', username);
await expect(page).toFill('input[name="password"]', password);
await expect(page).toClick('button', { text: 'Add User' });
await expect(page).toClick('a', { text: 'Already have an account? Login here.' });
await expect(page).toFill('input[name="username"]', username);
await expect(page).toFill('input[name="password"]', password);
await expect(page).toClick('button', { text: 'Login' });
});

when('I navigate to the game page and I click the correct answer', async () => {
await expect(page).toClick('button', { text: 'Jugar' });
await expect(page).toMatchElement("h1", { text: "Cargando..." });
await expect(page).toMatchElement("h1", { text: "Pregunta Nº1" });
await expect(page).toClick('label[data-iscorrect="true"]');
});

then('The button turns green and the number of questions answered correctly increments one', async () => {
const labelHasActiveClass = await page.$eval('label[data-iscorrect="true"]', label => {
return label.classList.contains('active');
});
// Compruebo que el boton se ha activado
expect(labelHasActiveClass).toBe(true);
const labelStyle = await page.$eval('label[data-iscorrect="true"]', label => {
const style = window.getComputedStyle(label);
return {
color: style.color,
backgroundColor: style.backgroundColor
};
});

// Compruebo que el color sea verde
expect(labelStyle.backgroundColor).toBe('rgb(0, 128, 0)');
await expect(page).toMatchElement("p", { text: "Preguntas acertadas: 1" });

});
});

test('The authenticated user plays a game and clicks an incorrect answer', ({given, when, then}) => {
let username;
let password;

given('An authenticated user', async () => {
username = "game1";
password = "game";
await expect(page).toClick('button', { text: 'Login' });
await expect(page).toFill('input[name="username"]', username);
await expect(page).toFill('input[name="password"]', password);
await expect(page).toClick('button', { text: 'Login' });
});

when('I navigate to the game page and I click an incorrect answer', async () => {
await page.waitForSelector('input[type="range"]');
await page.$eval('input[type="range"]', input => {
input.value = 1;
});
await expect(page).toClick('button', { text: 'Jugar' });
await expect(page).toMatchElement("h1", { text: "Cargando..." });
await expect(page).toMatchElement("h1", { text: "Pregunta Nº1" });
await expect(page).toClick('label[data-iscorrect="false"]');
});

then('The button turns red and the number of questions answered correctly is 0', async () => {
const labelsWithActiveClass = await page.$$eval('label[data-iscorrect="false"]', labels => {
return labels.map(label => {
const isActive = label.classList.contains('active');
const backgroundColor = window.getComputedStyle(label).backgroundColor;
return { isActive, backgroundColor };
});
});

// Comprueba si alguna etiqueta activa tiene el color rojo y está activada
const hasRedActiveLabel = labelsWithActiveClass.some(label => label.isActive && label.backgroundColor === 'rgb(255, 0, 0)');

expect(hasRedActiveLabel).toBe(true);
await expect(page).toMatchElement("p", { text: "Preguntas acertadas: 0" });
});
});

afterEach(async ()=>{
browser.close()
})

});
2 changes: 2 additions & 0 deletions webapp/e2e/test-environment-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ let userservice;
let authservice;
let gatewayservice;
let recordservice;
let questionservice;

async function startServer() {
console.log('Starting MongoDB memory server...');
Expand All @@ -16,6 +17,7 @@ async function startServer() {
authservice = await require("../../users/authservice/auth-service");
recordservice = await require("../../recordhistory/record-service");
gatewayservice = await require("../../gatewayservice/gateway-service");
questionservice = await require("../../questions/template-questions/question-service");
}

startServer();

0 comments on commit 8d106b6

Please sign in to comment.