diff --git a/test/cypress/integration/vnComponent/vnLocation.spec.js b/test/cypress/integration/vnComponent/vnLocation.spec.js index c1b0cf929..924b16adc 100644 --- a/test/cypress/integration/vnComponent/vnLocation.spec.js +++ b/test/cypress/integration/vnComponent/vnLocation.spec.js @@ -12,7 +12,7 @@ describe('VnLocation', () => { cy.viewport(1280, 720); cy.login('developer'); cy.visit('/#/supplier/567/fiscal-data', { timeout: 7000 }); - cy.waitForElement('.q-card'); + cy.domContentLoad(); cy.get(createLocationButton).click(); }); it('should filter provinces based on selected country', () => { diff --git a/test/cypress/support/commands.js b/test/cypress/support/commands.js index 76bdefd27..5bb89ecf9 100755 --- a/test/cypress/support/commands.js +++ b/test/cypress/support/commands.js @@ -27,6 +27,10 @@ // DO NOT REMOVE // Imports Quasar Cypress AE predefined commands // import { registerCommands } from '@quasar/quasar-app-extension-testing-e2e-cypress'; +Cypress.Commands.add('waitUntil', { prevSubject: 'optional' }, require('./waitUntil')); +Cypress.Commands.add('resetDB', () => { + cy.exec('pnpm run resetDatabase'); +}); Cypress.Commands.add('login', (user) => { //cy.visit('/#/login'); cy.request({ @@ -50,10 +54,12 @@ Cypress.Commands.add('login', (user) => { }); }); -Cypress.Commands.add('waitForElement', (element, timeout = 5000) => { - cy.get(element, { timeout }).should('be.visible'); +Cypress.Commands.add('domContentLoad', (element, timeout = 5000) => { + cy.waitUntil(() => cy.document().then((doc) => doc.readyState === 'complete')); +}); +Cypress.Commands.add('waitForElement', (element, timeout = 5000) => { + cy.waitUntil(() => cy.get(element).then(($el) => $el.is(':visible'))); }); - Cypress.Commands.add('getValue', (selector) => { cy.get(selector).then(($el) => { if ($el.find('.q-checkbox__inner').length > 0) { diff --git a/test/cypress/support/waitUntil.js b/test/cypress/support/waitUntil.js new file mode 100644 index 000000000..5fb47a2d8 --- /dev/null +++ b/test/cypress/support/waitUntil.js @@ -0,0 +1,59 @@ +const waitUntil = (subject, checkFunction, originalOptions = {}) => { + if (!(checkFunction instanceof Function)) { + throw new Error( + '`checkFunction` parameter should be a function. Found: ' + checkFunction + ); + } + + const defaultOptions = { + // base options + interval: 200, + timeout: 5000, + errorMsg: 'Timed out retrying', + + // log options + description: 'waitUntil', + log: true, + customMessage: undefined, + logger: Cypress.log, + verbose: false, + customCheckMessage: undefined, + }; + const options = { ...defaultOptions, ...originalOptions }; + + // filter out a falsy passed "customMessage" value + options.customMessage = [options.customMessage, originalOptions].filter(Boolean); + + const endTime = Date.now() + options.timeout; + + const check = (result) => { + if (result) { + return result; + } + if (Date.now() >= endTime) { + const msg = + options.errorMsg instanceof Function + ? options.errorMsg(result, options) + : options.errorMsg; + throw new Error(msg); + } + cy.wait(options.interval, { log: false }).then(() => { + return resolveValue(); + }); + }; + + const resolveValue = () => { + const result = checkFunction(subject); + + const isAPromise = Boolean(result && result.then); + if (isAPromise) { + return result.then(check); + } else { + return check(result); + } + }; + + return resolveValue(); +}; + +export default waitUntil;