From 6cf3d136c61a58134afb213c8d824268e4c3bb08 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 28 Nov 2024 12:23:36 +0100 Subject: [PATCH] Bug 38461: Bypass the clipboard for tests There are known bugs with Cypress trying to play with the clipboard, and I don't manage to make the tests pass consistently (also tried realClick from cypress-real-events) Here I am suggesting to not click on the button, but simply retrieve the url (which needs a bit of work in the existing code). We don't test the "Copied!" tooltip and the behaviour of the button, but at least the tests pass! --- .../prog/en/modules/admin/branches.tt | 3 +- koha-tmpl/intranet-tmpl/prog/js/datatables.js | 25 +++-- t/cypress/integration/KohaTable_spec.ts | 96 ++++++++----------- 3 files changed, 59 insertions(+), 65 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/branches.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/branches.tt index cdf73a5b864..6c6ef20c66e 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/branches.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/branches.tt @@ -656,10 +656,11 @@ var table_settings = [% TablesSettings.GetTableSettings( 'admin', 'libraries', 'libraries', 'json' ) | $raw %]; var calendarFirstDayOfWeek = '[% Koha.Preference('CalendarFirstDayOfWeek') | html %]'; + var libraries_table; $(document).ready(function() { var libraries_url = '/api/v1/libraries'; - var libraries = $("#libraries").kohaTable({ + libraries_table = $("#libraries").kohaTable({ "ajax": { "url": libraries_url }, diff --git a/koha-tmpl/intranet-tmpl/prog/js/datatables.js b/koha-tmpl/intranet-tmpl/prog/js/datatables.js index 1fd348d9336..0c7917a4348 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/datatables.js +++ b/koha-tmpl/intranet-tmpl/prog/js/datatables.js @@ -595,6 +595,20 @@ function _dt_default_ajax (params){ } } +function build_url_with_state(dt, table_settings){ + let table_key = 'DataTables_%s_%s_%s'.format( + table_settings.module, + table_settings.page, + table_settings.table); + + let state = JSON.stringify(dt.state()); + delete state.time; + let searchParams = new URLSearchParams(window.location.search); + searchParams.set(table_key + '_state', btoa(state)); + + return window.location.origin + window.location.pathname + '?' + searchParams.toString() + window.location.hash; +} + function _dt_buttons(params){ let settings = params.settings || {}; let table_settings = params.table_settings; @@ -723,17 +737,8 @@ function _dt_buttons(params){ titleAttr: __("Copy shareable link"), text: ' ' + __("Copy shareable link") + '', action: function (e, dt, node, config) { - let table_key = 'DataTables_%s_%s_%s'.format( - table_settings.module, - table_settings.page, - table_settings.table); - - let state = JSON.stringify(dt.state()); - delete state.time; - let searchParams = new URLSearchParams(window.location.search); - searchParams.set(table_key + '_state', btoa(state)); + const url = build_url_with_state(dt, table_settings); - let url = window.location.origin + window.location.pathname + '?' + searchParams.toString() + window.location.hash; if( navigator.clipboard && navigator.clipboard.writeText){ writeToClipboard(url, node); } diff --git a/t/cypress/integration/KohaTable_spec.ts b/t/cypress/integration/KohaTable_spec.ts index a9b973be66a..6803edf9fa5 100644 --- a/t/cypress/integration/KohaTable_spec.ts +++ b/t/cypress/integration/KohaTable_spec.ts @@ -264,65 +264,53 @@ describe("kohaTable (using REST API)", () => { ); cy.get(`#${table_id} th`).contains("Name"); cy.get(`#${table_id} th`).contains("Code"); - - // Close the 'Columns' list - cy.get(".dt-button-background").click(); - cy.get(".dt-button-background").should("not.exist"); - cy.wait(500); // ensure the animation completes, random failures? - - // Copy the shareable link (Name and Code shown) - cy.window().focus(); - cy.get( - `#${table_id}_wrapper .copyConditions_controls` - ).click({ force: true }); - cy.get(".tooltip").contains("Copied!"); }); cy.window().then(win => { - // Retrieve the content of the clipboard - win.navigator.clipboard.readText().then(url => { - expect(url).to.match( - /branches.pl\?DataTables_admin_libraries_libraries_state=/ + // Copy the shareable link (Name and Code shown) + const url = win.build_url_with_state( + win.libraries_table.DataTable(), + win.table_settings + ); + expect(url).to.match( + /branches.pl\?DataTables_admin_libraries_libraries_state=/ + ); + + // Remove localStorage + win.localStorage.clear(); + + // Use it + cy.visit(url); + + // Code is shown whereas it is hidden in the config + cy.get("@columns").then(columns => { + cy.get(`#${table_id} th`).should( + "have.length", + columns.length + ); + cy.get(`#${table_id} th`).contains("Name"); + cy.get(`#${table_id} th`).contains("Code"); + + // Hide "Name" + cy.get(`#${table_id}_wrapper .buttons-colvis`).click(); + cy.get(`#${table_id}_wrapper .dt-button-collection`) + .contains("Name") + .click(); + }); + + // Go to the shareable link + // but do not remove localStorage! + cy.visit(url); + + // Name is hidden and Code is shown + cy.get("@columns").then(columns => { + cy.get(`#${table_id} th`).should( + "have.length", + columns.length ); - // Remove localStorage - win.localStorage.clear(); - - // Use it - cy.visit(url); - - // Code is shown whereas it is hidden in the config - cy.get("@columns").then(columns => { - cy.get(`#${table_id} th`).should( - "have.length", - columns.length - ); - cy.get(`#${table_id} th`).contains("Name"); - cy.get(`#${table_id} th`).contains("Code"); - - // Hide "Name" - cy.get( - `#${table_id}_wrapper .buttons-colvis` - ).click(); - cy.get(`#${table_id}_wrapper .dt-button-collection`) - .contains("Name") - .click(); - }); - - // Go to the shareable link - // but do not remove localStorage! - cy.visit(url); - - // Name is hidden and Code is shown - cy.get("@columns").then(columns => { - cy.get(`#${table_id} th`).should( - "have.length", - columns.length - ); - - cy.get(`#${table_id} th`).contains("Name"); - cy.get(`#${table_id} th`).contains("Code"); - }); + cy.get(`#${table_id} th`).contains("Name"); + cy.get(`#${table_id} th`).contains("Code"); }); }); }); -- 2.34.1