Skip to content

Instantly share code, notes, and snippets.

@bhubr
Last active March 26, 2024 09:43
Show Gist options
  • Save bhubr/01ea49b1f150643095f5f3716665f0b6 to your computer and use it in GitHub Desktop.
Save bhubr/01ea49b1f150643095f5f3716665f0b6 to your computer and use it in GitHub Desktop.
Tests e2e cours CI/CD
// test/specs/cart.e2e.ts
import { expect, browser, $ } from "@wdio/globals";
import registerUser from "../helpers/register-user";
describe("Cart", () => {
// Ce test comporte plusieurs problèmes
// 1. bug de l'app qui fait qu'on ne peut ajouter des items au panier que connecté
// CORRIGÉ si vous avez mergé la dernière version de main
// 2. composante aléatoire qui fait qu'on est pas sûr qu'un produit soit dispo
it("user connected - should add items to cart", async () => {
// ARRANGE => Setup avant les actions
// Inscription/connexion
await registerUser("johndoe@" + Date.now() + "example.com");
// Attente de l'affichage des div .card
// (dans le signup on était dans un formulaire, quand on le valide
// on est redirigés vers l'accueil)
const firstCard = await $(".card");
await firstCard.waitForDisplayed({ timeout: 1000 });
// $ => document.querySelector
// $$ => document.querySelectorAll
// Récupération de toutes les .card
const cards = await $$(".card");
// Pour l'instant on n'a ajouté aucun produit
// Variable qui permet de tracer le nombre de produits qu'on a vraiment réussi
// à ajouter au panier
let numInCart = 0;
for (let i = 0; i < 3; i++) {
const card = cards[i];
const cardButton = await card.$("button");
// intercepter une erreur au cas où le produit ne serait pas disponible
// bouton Panier désactivé et donc non cliquable
try {
await cardButton.click();
numInCart++;
} catch (err) {}
}
// Cibler le dropdown panier (2ème dropdown);
const dropdowns = await $$(".dropdown");
const cartDropdown = dropdowns[1];
const dropdownToggle = await cartDropdown.$(".dropdown-toggle");
// await dropdownToggle.click();
const dropdownToggleText = await dropdownToggle.getText();
console.log(">> dropdown text", dropdownToggleText);
// expect(dropdownToggleText).toContain(numInCart.toString());
expect(dropdownToggleText).toEqual(`Panier${numInCart}`);
});
});
// test/specs/signup.e2e.ts
import { expect, browser, $ } from "@wdio/globals";
describe("Register", () => {
it("should register with valid information", async () => {
await browser.url(`http://localhost:5173/compte/inscription`);
const inputFullName = await $("#inputFullName");
await inputFullName.waitForDisplayed();
await $("#inputFullName").setValue("John Doe");
await $("#inputEmail").setValue("johndoe@" + Date.now() + "example.com");
await $("#inputPassword").setValue("Abcd1234!");
await $('button[type="submit"]').click();
// On utilise l'id plutôt que la classe .dropdown
// pour être sûr de récupérer le dropdown authentifié et pas le "invité"
const authenticatedDropdown = await $("#authenticated-dropdown");
await authenticatedDropdown.waitForDisplayed();
// On récupère le .dropdown-toggle, pas n'importe lequel,
// mais celui à l'intérieur de authenticatedDropdown
const authenticatedDropdownToggle = await authenticatedDropdown.$(
".dropdown-toggle"
);
const toggleText = await authenticatedDropdownToggle.getText();
expect(toggleText).toContain("John Doe");
// Déconnexion après le test
// Permet d'ouvrir le dropdown
await authenticatedDropdownToggle.click();
// const dropdownButtons = await $$(".dropdown .dropdown-menu button");
// const logoutButton = dropdownButtons[dropdownButtons.length - 1];
const logoutButton = await $(".signout-btn");
await logoutButton.click();
});
it("should not register with invalid information", async () => {
await browser.url(`http://localhost:5173/compte/inscription`);
await $("#inputFullName").setValue("John Doe");
await $("#inputPassword").setValue("Abcd1234!");
await $('button[type="submit"]').click();
$(".alert-danger").waitForExist({ timeout: 2000 });
const alertDanger = await $(".alert-danger");
const alertDangerText = await alertDanger.getText();
expect(alertDangerText).toContain(
"L'e-mail et le mot de passe sont requis"
);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment