From fe48ad4dfdf296b4d227a56bbfe2f4abe968ca46 Mon Sep 17 00:00:00 2001 From: Jakub Szajner <58347164+Jakshas@users.noreply.github.com> Date: Mon, 18 Sep 2023 20:56:21 +0200 Subject: [PATCH] SBVT-1849: (Cypress) DOM capture incorrect after fullpage capture (#158) * simple check * Fix Scroll before capture dom * change name of function * fix comparison * Fix * fix scroll * add some more fixes * Last fixes * SBVT-1849: final updates * SBVT-1849: removing ensureScrolledToTop to see what is failing the github actions * SBVT-1849: JS_SCROLL does not 'ensure' * SBVT-1849: Final touches --------- Co-authored-by: trevornelms --- commands.js | 26 ++++++++++++++++++++++---- test/package-lock.json | 16 ++++++++-------- test/package.json | 2 +- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/commands.js b/commands.js index 5d03a4b..7fb65f0 100644 --- a/commands.js +++ b/commands.js @@ -234,8 +234,8 @@ let takeScreenshot = (element, name, modifiedOptions, win) => { width: imageData.width } }; - // Translate to the top of the page and then capture the dom - win.eval(`document.body.style.transform="translateY(0)"`); + + ensureScrolledToTop(win) captureDom(win); // Read the new image base64 to blob to be sent to AWS @@ -264,8 +264,8 @@ let takeScreenshot = (element, name, modifiedOptions, win) => { modifiedOptions, ).then(() => { if (vtConfFile.debug) cy.task('copy', {path: picProps.path, imageName, imageType}); - // Translate to the top of the page and then capture the dom - win.eval(`document.body.style.transform="translateY(0)"`); + + // ensureScrolledToTop(win) //this creates issues, but this is the JS_SCROLL method captureDom(win); // Read the new image base64 to blob to be sent to AWS @@ -412,6 +412,24 @@ let captureDom = (win) => { cy.writeFile(`./${vtConfFile.debug}/${imageName}-${imageType}/${imageName}.json`, dom); } }; +let ensureScrolledToTop = (win) =>{ + let tries = 0; + let scrollOffset = win.eval(`window.scrollY`); + while (scrollOffset !== 0 && tries < 40){ + tries++; + cy.task('logger', {type: 'warn', message: `Page not scrolled to the top. Scroll offset is: ${scrollOffset}. Trying to scroll to the top again and waiting 250ms. Try #: ${tries}`}); + cy.scrollTo(0,0); + win.eval(`window.scrollTo(0, 0);`) + cy.wait(250); + scrollOffset = win.eval(`window.scrollY`); //check and update the scrolled position again + } + if (tries < 40 && scrollOffset === 0){ + cy.task('logger', {type: 'info', message: `Scroll offset is: ${scrollOffset}, after ${tries} tries`}); + }else{ + cy.task('logger', {type: 'error', message: `Couldn't scroll to the top of page after ${tries} tries. Scroll offset positon stuck at: ${scrollOffset}.`}); + throw new Error(`Couldn't scroll to the top of page after ${tries} tries. Scroll offset positon stuck at: ${scrollOffset}.`); + } +} let getComparisonMode = (comparisonMode, sensitivity) => { cy.task('logger', {type: 'info', message: `comparisonMode: ${comparisonMode}, sensitivity: ${sensitivity}`}); layoutData = {}; diff --git a/test/package-lock.json b/test/package-lock.json index 868be3a..37d27b2 100644 --- a/test/package-lock.json +++ b/test/package-lock.json @@ -10,7 +10,7 @@ "license": "ISC", "dependencies": { "@smartbear/visualtest-cypress": "file:..", - "cypress": "^13.1.0", + "cypress": "^13.2.0", "mochawesome": "^7.1.3", "mochawesome-merge": "^4.3.0" } @@ -92,9 +92,9 @@ "link": true }, "node_modules/@types/node": { - "version": "16.18.40", - "resolved": "https://registry.npmjs.org/@types/node/-/node-16.18.40.tgz", - "integrity": "sha512-+yno3ItTEwGxXiS/75Q/aHaa5srkpnJaH+kdkTVJ3DtJEwv92itpKbxU+FjPoh2m/5G9zmUQfrL4A4C13c+iGA==" + "version": "18.17.17", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.17.17.tgz", + "integrity": "sha512-cOxcXsQ2sxiwkykdJqvyFS+MLQPLvIdwh5l6gNg8qF6s+C7XSkEWOZjK+XhUZd+mYvHV/180g2cnCcIl4l06Pw==" }, "node_modules/@types/sinonjs__fake-timers": { "version": "8.1.1", @@ -616,14 +616,14 @@ } }, "node_modules/cypress": { - "version": "13.1.0", - "resolved": "https://registry.npmjs.org/cypress/-/cypress-13.1.0.tgz", - "integrity": "sha512-LUKxCYlB973QBFls1Up4FAE9QIYobT+2I8NvvAwMfQS2YwsWbr6yx7y9hmsk97iqbHkKwZW3MRjoK1RToBFVdQ==", + "version": "13.2.0", + "resolved": "https://registry.npmjs.org/cypress/-/cypress-13.2.0.tgz", + "integrity": "sha512-AvDQxBydE771GTq0TR4ZUBvv9m9ffXuB/ueEtpDF/6gOcvFR96amgwSJP16Yhqw6VhmwqspT5nAGzoxxB+D89g==", "hasInstallScript": true, "dependencies": { "@cypress/request": "^3.0.0", "@cypress/xvfb": "^1.2.4", - "@types/node": "^16.18.39", + "@types/node": "^18.17.5", "@types/sinonjs__fake-timers": "8.1.1", "@types/sizzle": "^2.3.2", "arch": "^2.2.0", diff --git a/test/package.json b/test/package.json index d92d06f..1d31e8e 100644 --- a/test/package.json +++ b/test/package.json @@ -12,7 +12,7 @@ "license": "ISC", "dependencies": { "@smartbear/visualtest-cypress": "file:..", - "cypress": "^13.1.0", + "cypress": "^13.2.0", "mochawesome": "^7.1.3", "mochawesome-merge": "^4.3.0" }