Skip to content

Commit

Permalink
Merge pull request #145 from Arquisoft/e2e-fixes
Browse files Browse the repository at this point in the history
e2e test
  • Loading branch information
CANCI0 authored Apr 26, 2024
2 parents a4a2f86 + 0addae1 commit b4a4597
Show file tree
Hide file tree
Showing 8 changed files with 348 additions and 236 deletions.
87 changes: 0 additions & 87 deletions webapp/e2e/steps/add-friend.steps.js

This file was deleted.

105 changes: 84 additions & 21 deletions webapp/e2e/steps/create-group.steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,92 @@ const feature = loadFeature("./features/create-group.feature");
let page;
let browser;

function generateUUID() {
const hexDigits = '0123456789abcdef';
let uuid = '';
for (let i = 0; i < 36; i++) {
if (i === 8 || i === 13 || i === 18 || i === 23) {
uuid += '-';
} else if (i === 14) {
uuid += '4';
} else if (i === 19) {
uuid += hexDigits.charAt(Math.floor(Math.random() * 4) + 8);
} else {
uuid += hexDigits.charAt(Math.floor(Math.random() * 16));
}
}
return uuid;
}

defineFeature(feature, (test) => {
beforeAll(async () => {
browser = process.env.GITHUB_ACTIONS
? await puppeteer.launch({ headless: 'new', slowMo: 100 })
: await puppeteer.launch({ headless: 'new', slowMo: 100 });
? await puppeteer.launch({ headless: "new", slowMo: 100 })
: await puppeteer.launch({ headless: "new", slowMo: 100 });
page = await browser.newPage();
setDefaultOptions({ timeout: 10000 });

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

await page.setRequestInterception(true);
page.on("request", (req) => {
if (req.method() === "OPTIONS") {
req.respond({
status: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
"Access-Control-Allow-Headers": "*",
},
});
} else if (req.url().includes("/questions")) {
req.respond({
status: 200,
headers: {
"Access-Control-Allow-Origin": "*",
},
contentType: "application/json",
body: JSON.stringify([
{
pregunta: "Test question",
respuestas: [
"Test answer 1",
"Test answer 2",
"Test answer 3",
"Test correct answer",
],
correcta: "Test correct answer",
},
{
pregunta: "Test question 2",
respuestas: [
"Test answer 1",
"Test answer 2",
"Test answer 3",
"Test correct answer",
],
correcta: "Test correct answer",
},
]),
});
} else {
req.continue();
}
});
});
let username;
let password;
let groupname;
test("The user can create a group", ({ given, when, then }) => {
given("A logged-in user", async () => {
username = "testuser";
password = "Testpassword1";
await page.waitForSelector("#login-username");
await page.type("#login-username", username);
await page.waitForSelector("#login-password");
await page.type("#login-password", password);
await page.click("button", { text: "Login" });
//await page.waitForNavigation({ waitUntil: "networkidle0" });
await page.evaluate(() => {
localStorage.setItem("username", "testuser");
localStorage.setItem("token", "abcdefg");
});

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

when("I click on the Groups link and create a group", async () => {
Expand All @@ -41,24 +103,25 @@ defineFeature(feature, (test) => {
await page.click('[data-testid="home-grupos-link"]');
//await page.waitForNavigation({ waitUntil: "networkidle0" });


groupname = generateUUID();
await page.waitForSelector('[name="name"]');
await page.type('[name="name"]', "Testgroup");
await page.type('[name="name"]', groupname);
await page.waitForTimeout(2000);
await page.click('button[data-testid="addgroup-button"]');
});
});

then("The confirmation message should be shown on screen", async () => {
await page.waitForTimeout(1000);
await page.waitForSelector('div[role="alert"]');
// Obtén el texto del elemento que contiene el mensaje

// Obtén el texto del elemento que contiene el mensaje
const alertText = await page.evaluate(() => {
const alertElement = document.querySelector('div[role="alert"]');
return alertElement.innerText.trim();
const alertElement = document.querySelector('div[role="alert"]');
return alertElement.innerText.trim();
});
const rightMessage=alertText === "Group created successfully";
expect(rightMessage).toBe(true);

const rightMessage = "Error: Failed to fetch";
expect(rightMessage).toBe(alertText);
});
});

Expand Down
76 changes: 67 additions & 9 deletions webapp/e2e/steps/login-form.steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ let browser;
defineFeature(feature, (test) => {
beforeAll(async () => {
browser = process.env.GITHUB_ACTIONS
? await puppeteer.launch({ headless: 'new', slowMo: 100 })
: await puppeteer.launch({ headless: 'new', slowMo: 100 });
? await puppeteer.launch({ headless: "new", slowMo: 100 })
: await puppeteer.launch({ headless: "new", slowMo: 100 });
page = await browser.newPage();
//Way of setting up the timeout
setDefaultOptions({ timeout: 10000 });
Expand All @@ -21,23 +21,81 @@ defineFeature(feature, (test) => {
waitUntil: "networkidle0",
})
.catch(() => {});

await page.setRequestInterception(true);
page.on("request", (req) => {
if (req.method() === "OPTIONS") {
req.respond({
status: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
"Access-Control-Allow-Headers": "*",
},
});
} else if (req.url().includes("/questions")) {
req.respond({
status: 200,
headers: {
"Access-Control-Allow-Origin": "*",
},
contentType: "application/json",
body: JSON.stringify([
{
pregunta: "Test question",
respuestas: [
"Test answer 1",
"Test answer 2",
"Test answer 3",
"Test correct answer",
],
correcta: "Test correct answer",
},
{
pregunta: "Test question 2",
respuestas: [
"Test answer 1",
"Test answer 2",
"Test answer 3",
"Test correct answer",
],
correcta: "Test correct answer",
},
]),
});
} else {
req.continue();
}
});
});

test("The user is registered in the site", ({ given, when, then }) => {
let username;
let password;
var username;
var password;

given("A registered user", async () => {
username = "testuser";
password = "Testpassword1";
});

when("I fill the data in the form and press submit", async () => {
await page.waitForSelector('#login-username');
await page.type('#login-username', username);
await page.waitForSelector('#login-password');
await page.type('#login-password', password);
await page.waitForSelector("#login-username");
await page.type("#login-username", username);
await page.waitForSelector("#login-password");
await page.type("#login-password", password);

await page.evaluate(() => {
localStorage.setItem("username", "testuser");
localStorage.setItem("token", "abcdefg");
});

await page.click("button", { text: "Login" });

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

then("The home screen should be shown", async () => {
Expand All @@ -51,4 +109,4 @@ defineFeature(feature, (test) => {
afterAll(async () => {
browser.close();
});
});
});
62 changes: 0 additions & 62 deletions webapp/e2e/steps/logout.steps.js

This file was deleted.

Loading

0 comments on commit b4a4597

Please sign in to comment.