From 5d794c99cc513da187b1c6af04239907c0898558 Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Sat, 12 Nov 2022 19:32:21 -0100 Subject: [PATCH] Bug 32131: Cypress tests are failing if ERMModule is off This patch solves this by enabling the system preference before each test, right after login. It also sets the system preference back to its original initial value after the tests finish running. To test: 1) Disable ERMModule system preference 2) Run cypess tests: yarn cypress open / yarn cypress run 3) Verify that tests run successfully (Optional): While tests are running, visit the system preferences panel and verify that the system preference is now "Enable". 4) Wait for the tests to finish and confirm that the system preference is set back to its original "Disable" value. Signed-off-by: Jonathan Druart Signed-off-by: Martin Renvoize Signed-off-by: Tomas Cohen Arazi --- t/cypress/integration/Agreements_spec.ts | 9 +++++++ t/cypress/integration/Licenses_spec.ts | 9 +++++++ t/cypress/integration/Packages_spec.ts | 9 +++++++ t/cypress/integration/Titles_spec.ts | 9 +++++++ t/cypress/support/commands.js | 30 ++++++++++++++++++++++++ 5 files changed, 66 insertions(+) diff --git a/t/cypress/integration/Agreements_spec.ts b/t/cypress/integration/Agreements_spec.ts index 5dd3b03846..22835c0bf3 100644 --- a/t/cypress/integration/Agreements_spec.ts +++ b/t/cypress/integration/Agreements_spec.ts @@ -117,9 +117,18 @@ function get_licenses_to_relate() { } describe("Agreement CRUD operations", () => { + before(() => { + cy.fetch_initial_ERM_sys_pref_value(); + }); + beforeEach(() => { cy.login("koha", "koha"); cy.title().should("eq", "Koha staff interface"); + cy.set_ERM_sys_pref_value(true); + }); + + after(() => { + cy.reset_initial_ERM_sys_pref_value(); }); it("List agreements", () => { diff --git a/t/cypress/integration/Licenses_spec.ts b/t/cypress/integration/Licenses_spec.ts index ef314bb0db..073a81b838 100644 --- a/t/cypress/integration/Licenses_spec.ts +++ b/t/cypress/integration/Licenses_spec.ts @@ -33,9 +33,18 @@ function get_license() { } describe("License CRUD operations", () => { + before(() => { + cy.fetch_initial_ERM_sys_pref_value(); + }); + beforeEach(() => { cy.login("koha", "koha"); cy.title().should("eq", "Koha staff interface"); + cy.set_ERM_sys_pref_value(true); + }); + + after(() => { + cy.reset_initial_ERM_sys_pref_value(); }); it("List license", () => { diff --git a/t/cypress/integration/Packages_spec.ts b/t/cypress/integration/Packages_spec.ts index 718f411d08..fb8cf3fe60 100644 --- a/t/cypress/integration/Packages_spec.ts +++ b/t/cypress/integration/Packages_spec.ts @@ -24,9 +24,18 @@ function get_package() { } describe("Package CRUD operations", () => { + before(() => { + cy.fetch_initial_ERM_sys_pref_value(); + }); + beforeEach(() => { cy.login("koha", "koha"); cy.title().should("eq", "Koha staff interface"); + cy.set_ERM_sys_pref_value(true); + }); + + after(() => { + cy.reset_initial_ERM_sys_pref_value(); }); it("List package", () => { diff --git a/t/cypress/integration/Titles_spec.ts b/t/cypress/integration/Titles_spec.ts index 6e46b23a48..14f7a9bc06 100644 --- a/t/cypress/integration/Titles_spec.ts +++ b/t/cypress/integration/Titles_spec.ts @@ -67,9 +67,18 @@ function get_packages_to_relate() { } describe("Title CRUD operations", () => { + before(() => { + cy.fetch_initial_ERM_sys_pref_value(); + }); + beforeEach(() => { cy.login("koha", "koha"); cy.title().should("eq", "Koha staff interface"); + cy.set_ERM_sys_pref_value(true); + }); + + after(() => { + cy.reset_initial_ERM_sys_pref_value(); }); it("Import titles", () => { diff --git a/t/cypress/support/commands.js b/t/cypress/support/commands.js index be71149f90..05e1b11240 100644 --- a/t/cypress/support/commands.js +++ b/t/cypress/support/commands.js @@ -30,4 +30,34 @@ Cypress.Commands.add('login', (username, password) => { cy.get("#userid").type(username) cy.get("#password").type(password) cy.get("#submit-button").click() +}) + +Cypress.Commands.add('set_ERM_sys_pref_value', (enable) => { + cy.visit('/cgi-bin/koha/admin/admin-home.pl') + cy.get("h4").contains("Global system preferences").click(); + cy.get("a[title^=E-resource]").contains("E-resource management").click(); + cy.get('#pref_ERMModule').then(($select) => { + // Only enable if currently disabled, or only disable if currently enabled + let sys_pref_value = $select.find(":selected").text().trim(); + if (enable && sys_pref_value == 'Disable' || !enable && sys_pref_value == 'Enable') { + cy.get("#pref_ERMModule").select(enable ? 'Enable' : 'Disable'); + cy.get(".save-all").first().click(); + Cypress.env("current_ERM_Module_sys_pref_value", enable); + cy.wait(500); // Cypress is too fast! + } + }) +}) + +Cypress.Commands.add('fetch_initial_ERM_sys_pref_value', () => { + cy.login("koha", "koha"); + cy.visit('/cgi-bin/koha/admin/admin-home.pl') + cy.get("h4").contains("Global system preferences").click(); + cy.get("a[title^=E-resource]").contains("E-resource management").click(); + cy.get('#pref_ERMModule').then(($select) => { + Cypress.env('initial_ERM_Module_sys_pref_value', $select.find(":selected").text().trim() == 'Enable'); + }) +}) + +Cypress.Commands.add('reset_initial_ERM_sys_pref_value', () => { + cy.set_ERM_sys_pref_value(Cypress.env("initial_ERM_Module_sys_pref_value")); }) \ No newline at end of file -- 2.39.5