Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error during screenshot capture: RangeError: Index out of range | The value of "offset" is out of range. #2034

Closed
rbayliss opened this issue Jun 26, 2018 · 38 comments · Fixed by #9281

Comments

@rbayliss
Copy link

Current behavior:

Executing cy.screenshot() on large pages causes error. Stack trace:

     RangeError: Index out of range
      at checkInt (buffer.js:1180:11)
      at Buffer.writeUInt32BE (buffer.js:1271:5)
      at new Jimp (/Users/rbayliss/Library/Caches/Cypress/3.0.1/Cypress.app/Contents/Resources/app/packages/server/node_modules/jimp/index.js:137:30)
      at stitchScreenshots (/Users/rbayliss/Library/Caches/Cypress/3.0.1/Cypress.app/Contents/Resources/app/packages/server/lib/screenshots.js:199:17)
      at /Users/rbayliss/Library/Caches/Cypress/3.0.1/Cypress.app/Contents/Resources/app/packages/server/lib/screenshots.js:295:21
      at tryCatcher (/Users/rbayliss/Library/Caches/Cypress/3.0.1/Cypress.app/Contents/Resources/app/packages/server/node_modules/bluebird/js/release/util.js:16:23)
      at Promise._settlePromiseFromHandler (/Users/rbayliss/Library/Caches/Cypress/3.0.1/Cypress.app/Contents/Resources/app/packages/server/node_modules/bluebird/js/release/promise.js:510:31)
      at Promise._settlePromise (/Users/rbayliss/Library/Caches/Cypress/3.0.1/Cypress.app/Contents/Resources/app/packages/server/node_modules/bluebird/js/release/promise.js:567:18)
      at Promise._settlePromise0 (/Users/rbayliss/Library/Caches/Cypress/3.0.1/Cypress.app/Contents/Resources/app/packages/server/node_modules/bluebird/js/release/promise.js:612:10)
      at Promise._settlePromises (/Users/rbayliss/Library/Caches/Cypress/3.0.1/Cypress.app/Contents/Resources/app/packages/server/node_modules/bluebird/js/release/promise.js:691:18)
      at Async._drainQueue (/Users/rbayliss/Library/Caches/Cypress/3.0.1/Cypress.app/Contents/Resources/app/packages/server/node_modules/bluebird/js/release/async.js:133:16)
      at Async._drainQueues (/Users/rbayliss/Library/Caches/Cypress/3.0.1/Cypress.app/Contents/Resources/app/packages/server/node_modules/bluebird/js/release/async.js:143:10)
      at Immediate.Async.drainQueues (/Users/rbayliss/Library/Caches/Cypress/3.0.1/Cypress.app/Contents/Resources/app/packages/server/node_modules/bluebird/js/release/async.js:17:14)
      at runCallback (timers.js:781:20)
      at tryOnImmediate (timers.js:743:5)
      at processImmediate [as _immediateCallback] (timers.js:714:5)

Desired behavior:

Screenshots are captured.

Steps to reproduce:

Call cy.screenshot() on a page that exceeds 16,000,000 square pixels (in my case, the element is 1195px by 13518px). Reproduction test case: https://github.com/rbayliss/cypress-test-tiny/tree/index_error

Versions

Cypress package version: 3.0.1
Cypress binary version: 3.0.1
Chrome 67.0.3396.87 on OSX 10.13
@jennifer-shehane jennifer-shehane added the stage: needs investigating Someone from Cypress needs to look at this label Jun 26, 2018
@rhoog
Copy link

rhoog commented Aug 24, 2018

I'm getting the same error when trying to create a screenshot of an area 1170px by 1317px.

My workaround is to increase the viewport from

cy.viewport(1200, 800);

to

cy.viewport(1200, 1500);

Edit: running Cypress v3.1.0

@wBingwei
Copy link

wBingwei commented Sep 4, 2018

I've met the same issue. Running Cypress v3.1.0

The same version works on my workstation, but not works on my laptop.
Only happening with test runner with headed mode.

@wBingwei
Copy link

This seems to be an issue of dependency "jimp" package.
My laptop is Windows 10, 14' size, and the screen resolution is 1080P. At first, I use 125% text size, then the range error happened. But when I changed the text size to 100%, the error disappear.

@guigonc
Copy link

guigonc commented Sep 21, 2018

I am have the same problem running cypress open

Cypress: 3.1.0
SO: OS X 10.13.6
Node: 8.11.3

The strange is that I have a lot of other tests that runs ok. The test that fails looks no different from the others

it('renders form section', () => {
   cy.get('.checkbox label').click()
   cy.get('.form-section').screenshot('form-section-opened')
})

If I remove the command that does the click the screenshot works.

@mattbgold
Copy link

mattbgold commented Sep 23, 2018

I have the same issue on 3.1.0, both when running locally on Windows and in a docker container using chrome.

Every call to cy.screenshot() in my spec file would produce the same error, and this was across three different viewport sizes. (1700x900, 375x812, and 768x1024).

The issue disappeared after I changed the viewports to slightly different sizes (1700x800, and 375x667), and now I seem to be able to use the original viewport sizes again without any issue.

@leeuwd
Copy link

leeuwd commented Oct 15, 2018

Same here. Latest Cypress, Vue CLI 3 project, macOS Mojave.

@loicsalou
Copy link

same issue here. Cypress 3.1.4,
All my screenshots were working so far but now some of them crash with this RangeError "Index out of range". This issue is annoying, I can't build the report I need without these screenshots.
I tried the following:

  • move the place of the screenshot in the code
  • add some wait before
  • end previous cy... command before
  • take the screenshot in its own step (it(...))
  • change viewport size

nothing changes for these screenshots in error, though they have nothing noticeable different from those working.

@loicsalou
Copy link

loicsalou commented Jan 27, 2019

just found a workaround which seems to work in any case (at least for all of my cases).

Never call cy.screenshot('file') directly, always use some dom element and chain with .root().screenshot('file')

  • simple ex. cy.get('[data-cy=myTag]').root().screenshot('file')
  • different use case, to see the result of an action like opening a popup, cy.get('[data-cy=myPopupButton]').click().root().screenshot('result-screen-file' works fine.

Hope this helps.

@loicsalou
Copy link

unfortunately my workaround does not seem to always work. Some of my screenshots which were working 2 days ago are crashing today, no change on my side.
This feature is really important for my users, we intend to deliver them a "book" for each release so they can check small issues with desing like CSS bugs etc.

Thanks for not letting this down !

@lukeapage
Copy link
Contributor

We are suddenly getting this issue now - though it wasn’t happening last week??!

@lukeapage
Copy link
Contributor

This could well be fixed when master is released since jimp is upgraded and last time I looked at the code, a lot of differences exist in the upgrade.
#3415

@bluehenry
Copy link

setResolution() works for me. Windows 10 + Chrome.
cy.setResolution([2560, 1440]);

@lukeapage
Copy link
Contributor

@bluehenry is that a custom command?

@bluehenry
Copy link

bluehenry commented Mar 14, 2019

@bluehenry is that a custom command?

the code looks like:

context('demo', () => {    
    beforeEach(() => {
        cy.setResolution([2560, 1440]);
        cy.visit('https://google.com');
        cy.wait(8000);
    })    

    it('demo', function() {
     cy.matchImageSnapshot('dashboard');
    })
});

@chisma
Copy link

chisma commented Mar 4, 2020

@bluehenry , this works for me too. On a headless Chrome browser, though I don't understand how.

@nitzanashi
Copy link

This did not happen to me on Cypress 3.8.0 trying to upgrade to Cypress 4+for a while now and this Error keep poping out...

@maamounapprise
Copy link

The issue is still happening, @jennifer-shehane what is the priority for fixing this or if there is a work around?

@gael-ian
Copy link

gael-ian commented May 26, 2020

setResolution() works for me. Windows 10 + Chrome.
cy.setResolution([2560, 1440]);

cy.setResolution does not exist (and seems to never exist before as the Cypress changelog doesn't mention it at all) so it can't be a valid workaround.

I have the same problem and it seems to come from the packages/server/lib/screenshots#crop method. Sometime dimensions ends with a negative value as y and jimp does'nt like it at all.

cypress:server:screenshot (s345) dimensions for cropping are { x: 200, y: -0.859375, width: 1065, height: 666 } +0ms
cypress:server:screenshot (s345) crop: from 200, -0.859375 +0ms
cypress:server:screenshot (s345)         to 1065 x 666 +0ms
[…]
cypress:server:reporter got mocha event 'fail' with args: [ {
  […]
  err: {
    message: 'The value of "offset" is out of range. It must be >= 0 and <= 3686396. Received -4320',
    name:
      'RangeError [ERR_OUT_OF_RANGE] [ERR_OUT_OF_RANGE]: The value of "offset" is out of range. It must be >= 0 and <= 3686396. Received -4320\n' +
      'at boundsError (internal/buffer.js:72:9)\n' +
      'at Buffer.readUInt32BE (internal/buffer.js:295:5)\n' +
      'at Jimp.<anonymous> (/[…]/4.5.0/[…]/server/node_modules/@jimp/plugin-crop/dist/index.js:45:37)\n' +
      'at scan (/[…]/4.5.0/[…]/server/node_modules/@jimp/utils/dist/index.js:55:9)\n' +
      'at Jimp.scanQuiet (/[…]/4.5.0/[…]/server/node_modules/@jimp/core/dist/index.js:1352:32)\n' +
      'at Jimp.cropQuiet (/[…]/4.5.0/[…]/server/node_modules/@jimp/plugin-crop/dist/index.js:44:12)\n' +
      'at Jimp.<computed> [as crop] (/[…]/4.5.0/[…]/server/node_modules/@jimp/core/dist/index.js:1266:23)\n' +
      'at crop (/[…]/4.5.0/[…]/server/lib/screenshots.js:145:26)\n' +
      'at /[…]/4.5.0/[…]/server/lib/screenshots.js:209:22\n' +
      'at arrayEach (/[…]/4.5.0/[…]/server/node_modules/lodash/lodash.js:516:11)\n' +
      'at Function.forEach (/[…]/4.5.0/[…]/server/node_modules/lodash/lodash.js:9342:14)\n' +
      'at stitchScreenshots (/[…]/4.5.0/[…]/server/lib/screenshots.js:206:7)\n' +
      'at /[…]/4.5.0/[…]/server/lib/screenshots.js:350:21\n'
    },
    […]
  }
] +6s

You already check if calculated dimensions do not exceed image dimensions (Line 139-140). A solution may be to add a second check on dimensions not being negative.

[Edit]
Sometime, Buffer.allocUnsafe receive a negative size and raise another kind of RangeError:

RangeError: The value "-73440" is invalid for option "size"

Buffer size is calculated from width and height, so these two should also be checked to prevent negative values.

This sounds like #6099 but, at least in my test cases, nothing is resized while screenshots are captured.

@iGoog
Copy link

iGoog commented Jun 17, 2020

I was running into the same error when trying to do matchImageSnapshot of an element. I was able to work around the issue by chaining a "then" statement with this function like this passed to it.

const clipWorkaround = ($el) => {
	const rect =  $el[0].getBoundingClientRect();
	const opts = {clip: {x : rect.x, y: rect.y, width: rect.width, height: rect.height } };
	cy.screenshot(opts);
};

The opts probably should also be using the capture: 'fullPage' to work correctly. It might be viable to select the body of the page as your element. I was originally using 'viewport' if you're digging into my code.

@bahmutov
Copy link
Contributor

bahmutov commented Jun 18, 2020

Another reproducible example: https://github.com/masakudamatsu/line-height-picker/blob/visual-testing/cypress/e2e/visual-tests.js

Cypress v4.8.0

Screen Shot 2020-06-18 at 9 23 42 AM

The element to be screenshotted has SVG inside

Screen Shot 2020-06-18 at 9 26 03 AM

Screen Shot 2020-06-18 at 9 26 26 AM

Seems this only happens when there is vertical scroll necessary to get the full image, even if the element itself is small

@MatissAndersons
Copy link

Quite new to the cypress and automated testing, and for me the same issue occurred. However, at least for me the issue is that if the element before has height that is of float value, the cypress tries to crop with a negative y value (-0.78125 in my case). If I force the y value to have a minimum value zero, everything proceeds smoothly.
I did in Cypress/resources/app/packages/server/lib/automation/screenshot.js that by adding
data.clip.y = Math.max(data.clip.y, 0);
But couldn't find the cause for it calculating the y value to be negative.

@askirmas
Copy link

Issue appears if body is scrollable means bigger than viewport. Don't know exactly how screenshots are made but somethings is opened with the same body state except scroll position.
Despite discussed issue, scrollable body sounds rather wrong, especially in 2020 with display: grid and responsive layout

@jennifer-shehane
Copy link
Member

jennifer-shehane commented Jul 28, 2020

Narrowed this issue down to a smaller repro so we don't have to rely on outside repos/URLs. This is reproducible outside of any snapshot plugins.

If I open the DevTools attached to the window then the test passes without this error which is super fun. 🤦

index.html

<html>
<body>
  <svg width="778pt" height="308pt" style="background-color: green;"></svg>
  <svg id="blue" width="778pt" height="308pt" style="background-color: blue;"></svg>
  <div style="height: 300px; background-color: red;"></div>
</body>
</html>

spec.js

it('works', () => {
  cy.visit('index.html')
  cy.get('#blue').screenshot()
})
RangeError: The value of "offset" is out of range. It must be >= 0 and <= 20553340. Received -11304
    at boundsError (internal/buffer.js:72:9)
    at Buffer.readUInt32BE (internal/buffer.js:295:5)
    at Jimp.<anonymous> (/Users/jennifer/Library/Caches/Cypress/4.11.0/Cypress.app/Contents/Resources/app/packages/server/node_modules/@jimp/plugin-crop/dist/index.js:43:37)
    at scan (/Users/jennifer/Library/Caches/Cypress/4.11.0/Cypress.app/Contents/Resources/app/packages/server/node_modules/@jimp/utils/dist/index.js:53:9)
    at Jimp.scanQuiet (/Users/jennifer/Library/Caches/Cypress/4.11.0/Cypress.app/Contents/Resources/app/packages/server/node_modules/@jimp/core/dist/index.js:1262:32)
    at Jimp.cropQuiet (/Users/jennifer/Library/Caches/Cypress/4.11.0/Cypress.app/Contents/Resources/app/packages/server/node_modules/@jimp/plugin-crop/dist/index.js:42:12)
    at Jimp.<computed> [as crop] (/Users/jennifer/Library/Caches/Cypress/4.11.0/Cypress.app/Contents/Resources/app/packages/server/node_modules/@jimp/core/dist/index.js:1176:23)
    at crop (/Users/jennifer/Library/Caches/Cypress/4.11.0/Cypress.app/Contents/Resources/app/packages/server/lib/screenshots.js:161:24)
    at /Users/jennifer/Library/Caches/Cypress/4.11.0/Cypress.app/Contents/Resources/app/packages/server/lib/screenshots.js:435:17
    at processImmediate (internal/timers.js:439:21)
From previous event:
    at $Cypress.automation (http://localhost:51135/__cypress/runner/cypress_runner.js:164254:14)
    at automate (http://localhost:51135/__cypress/runner/cypress_runner.js:157822:20)
    at automateScreenshot (http://localhost:51135/__cypress/runner/cypress_runner.js:157832:10)
    at scrollAndTake (http://localhost:51135/__cypress/runner/cypress_runner.js:157901:12)
From previous event:
    at takeScrollingScreenshots (http://localhost:51135/__cypress/runner/cypress_runner.js:157904:18)
    at takeElementScreenshot (http://localhost:51135/__cypress/runner/cypress_runner.js:158019:10)
    at http://localhost:51135/__cypress/runner/cypress_runner.js:158123:14
From previous event:
    at takeScreenshot (http://localhost:51135/__cypress/runner/cypress_runner.js:158115:19)
    at Context.screenshot (http://localhost:51135/__cypress/runner/cypress_runner.js:158229:14)
    at Context.<anonymous> (http://localhost:51135/__cypress/runner/cypress_runner.js:167810:21)
    at http://localhost:51135/__cypress/runner/cypress_runner.js:167243:15
From previous event:
    at runCommand (http://localhost:51135/__cypress/runner/cypress_runner.js:167222:8)
    at next (http://localhost:51135/__cypress/runner/cypress_runner.js:167368:14)
From previous event:
    at next (http://localhost:51135/__cypress/runner/cypress_runner.js:167368:34)
From previous event:
    at http://localhost:51135/__cypress/runner/cypress_runner.js:167409:37
From previous event:
    at run (http://localhost:51135/__cypress/runner/cypress_runner.js:167402:19)
    at $Cy.cy.<computed> [as visit] (http://localhost:51135/__cypress/runner/cypress_runner.js:167854:11)
    at Context.runnable.fn (http://localhost:51135/__cypress/runner/cypress_runner.js:168079:21)
    at callFn (http://localhost:51135/__cypress/runner/cypress_runner.js:103224:21)
    at Test.../driver/node_modules/mocha/lib/runnable.js.Runnable.run (http://localhost:51135/__cypress/runner/cypress_runner.js:103211:7)
    at http://localhost:51135/__cypress/runner/cypress_runner.js:173313:28
From previous event:
    at Object.onRunnableRun (http://localhost:51135/__cypress/runner/cypress_runner.js:173301:20)
    at $Cypress.action (http://localhost:51135/__cypress/runner/cypress_runner.js:164070:61)
    at Test.Runnable.run (http://localhost:51135/__cypress/runner/cypress_runner.js:171914:13)
    at Runner.../driver/node_modules/mocha/lib/runner.js.Runner.runTest (http://localhost:51135/__cypress/runner/cypress_runner.js:103883:10)
    at http://localhost:51135/__cypress/runner/cypress_runner.js:104009:12
    at next (http://localhost:51135/__cypress/runner/cypress_runner.js:103792:14)
    at http://localhost:51135/__cypress/runner/cypress_runner.js:103802:7
    at next (http://localhost:51135/__cypress/runner/cypress_runner.js:103704:14)
    at http://localhost:51135/__cypress/runner/cypress_runner.js:103770:5
    at timeslice (http://localhost:51135/__cypress/runner/cypress_runner.js:97696:27)

Screen Shot 2020-07-28 at 3 15 20 PM

DEBUG=cypress:server:screenshot logs

  cypress:server:screenshot (s4) capturing screenshot { titles: [ 'works' ], testId: 'r2', capture: 'viewport', clip: { x: 8, y: -0.34375, width: 992, height: 410.65625 }, viewport: { width: 1025, height: 894 }, scaled: false, blackout: [], startTime: '2020-07-28T09:06:53.193Z', current: 1, total: 1, specName: 'spec.js' } +0ms
  cypress:server:screenshot (s4) capture and check { tries: 1, totalDuration: 0 } +2ms
  cypress:server:screenshot (s4) received screenshot data from automation layer data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAACAIAAAb8CAYAAACEPaEyAAABKWlDQ1BTa2lhAAAokX2RP0jDQBTGfx +416ms
  cypress:server:screenshot (s4) read buffer to image 2050 x 1788 +200ms
  cypress:server:screenshot (s4) helper pixels
  cypress:server:screenshot  {
  cypress:server:screenshot   topLeft: { r: 255, g: 255, b: 255, a: 255, isNotWhite: false },
  cypress:server:screenshot   topLeftRight: { r: 255, g: 255, b: 255, a: 255, isWhite: true },
  cypress:server:screenshot   topLeftDown: { r: 255, g: 255, b: 255, a: 255, isWhite: true },
  cypress:server:screenshot   bottomLeft: { r: 255, g: 255, b: 255, a: 255, isWhite: true },
  cypress:server:screenshot   topRight: { r: 255, g: 255, b: 255, a: 255, isWhite: true },
  cypress:server:screenshot   bottomRight: { r: 255, g: 255, b: 255, a: 255, isBlack: false }
  cypress:server:screenshot } +0ms
  cypress:server:screenshot (s4) pixelConditionFn { pixelRatio: 2, subject: 'app', hasPixels: false, expectedPixels: false } +1ms
  cypress:server:screenshot (s4) no previous image to compare +0ms
  cypress:server:screenshot (s4) resolving with image { tries: 1, totalDuration: 0 } +0ms
  cypress:server:screenshot (s4) pixel ratio is 2 +0ms
  cypress:server:screenshot (s4) multi-part 1/1 +0ms
  cypress:server:screenshot (s4) dimensions before are { x: 8, y: -0.34375, width: 992, height: 410.65625 } +0ms
  cypress:server:screenshot (s4) dimensions for cropping are { x: 16, y: -0.6875, width: 1984, height: 821.3125 } +0ms
  cypress:server:screenshot (s4) crop: from 16, -0.6875 +1ms
  cypress:server:screenshot (s4)         to 1984 x 821.3125 +0ms
  cypress:server:screenshot (s4) clearing 0 cached multipart images +9ms

@cypress-bot cypress-bot bot added stage: ready for work The issue is reproducible and in scope and removed stage: needs investigating Someone from Cypress needs to look at this labels Jul 28, 2020
@melibe23
Copy link

I am experiencing the same issue, waiting for this fix to finish my mobile e2e testing. It only happens to me when I set a viewport. Thank you in advance.

@jennifer-shehane jennifer-shehane changed the title Error during screenshot capture: RangeError: Index out of range Error during screenshot capture: RangeError: Index out of range | The value of "offset" is out of range. Aug 27, 2020
@santam85
Copy link
Contributor

Also running into this. Seems to happen only with or elements, in Blink/Webkit browsers.

paviad pushed a commit to valor-software/ng2-charts that referenced this issue Sep 13, 2020
@j1000
Copy link

j1000 commented Sep 22, 2020

I'm able to work around this issue by using Electron instead of Chrome. (So far.)

Part of what seems to trigger this issue is setting the viewport before taking a screenshot. It can be very inconsistent. I have a custom command to take a series of screenshots at various browser widths, and the same dimensions will work with one page but fail with another. Fiddling with the viewport X and Y can get it working in one place but will likely break another.

@chisma
Copy link

chisma commented Oct 14, 2020

This bug is really coming in the way of my success with visual testing with Cypress(cypress-image-snapshot/plugin). I get the out of range error only when I run with headless chrome, inside a container! I setup the viewport to (1600,900) in cypress.json. Dont use cy.viewport() anywhere in the test. I get the snapshots with cy.matchImageSnapshot() for the test- locally, in both headed and headless chrome. Also headless, locally. But not on headless-inside container!
image
I am on 3.8.3

@vivekbwaj
Copy link

is there any update on this issue?

@alexbjorlig
Copy link

I hoped that I could workaround this issue by using scrollIntoView() on the element, before calling .screenshot(). But that does not help - still get the index out of range. Any ideas how to avoid this issue, until an official fix arrives?

@wesselvanderlinden
Copy link

I'm also getting this error frequently. I've tried several workarounds suggested in the comments above, but none of them worked. It's a real pain since our build pipeline depends on the screenshot functionality, and they are failing randomly now.

Are there any plans to fix this issue? We now either have to disable all our tests, or retry until they succeed.

@aleksandryackovlev
Copy link

aleksandryackovlev commented Nov 18, 2020

In my case this error was caused by the behavior of the dom.getElementPositioning (https://github.com/cypress-io/cypress/blob/develop/packages/driver/src/dom/coordinates.js#L27).
In some cases it returns negative values between -1 and 0 in fromElViewport.top for elements that are being captured.

I suppose changing https://github.com/cypress-io/cypress/blob/develop/packages/driver/src/cy/commands/screenshot.js#L235 to y: Math.max(0, elPosition.fromElViewport.top), will fix the problem with negative offsets.

Unfortunatelly, I'm not deeply familiar with the code of cypress, so I can't predict whether any other behavior of the system will be affected or not by this change.

Can anyone tell me where else this code is used? And is this the right place in code to fix the problem?

For now I just overwrote the screenshot command as a workaround and it works for me:

Cypress.Commands.overwrite(
  'screenshot',
  (originalFn, subject, name, options) => {
    if (subject) {
      let scrollY = null;
      const win = Cypress.dom.getWindowByElement(subject[0]);

      return originalFn(subject, name, {
        ...options,
        onBeforeScreenshot(...args) {
          if (options.onBeforeScreenshot) {
            options.onBeforeScreenshot(...args);
          }

          scrollY = Object.getOwnPropertyDescriptor(win, 'scrollY');

          Cypress.log({
            name: 'stub scrollY',
            displayName: 'stub sY',
            message: `${win.scrollY} => ${
              win.scrollY ? win.scrollY - 1 : win.scrollY
            }`,
          });

          Object.defineProperty(win, 'scrollY', {
            value: win.scrollY ? win.scrollY - 1 : win.scrollY,
          });
        },
        onAfterScreenshot(...args) {
          Object.defineProperty(win, 'scrollY', scrollY);

          if (options.onAfterScreenshot) {
            options.onAfterScreenshot(...args);
          }

          Cypress.log({
            name: 'restore scrollY',
            displayName: 'restore sY',
            message: `${win.scrollY}`,
          });
        },
      });
    }

    return originalFn(subject, name, options);
  }
);

@jfhector
Copy link

I'm getting this error too. It's preventing me from being able to use Cypress to accomplish our goals.

@jarrietas
Copy link

In my case this error was caused by the behavior of the dom.getElementPositioning (https://github.com/cypress-io/cypress/blob/develop/packages/driver/src/dom/coordinates.js#L27).
In some cases it returns negative values between -1 and 0 in fromElViewport.top for elements that are being captured.

I suppose changing https://github.com/cypress-io/cypress/blob/develop/packages/driver/src/cy/commands/screenshot.js#L235 to y: Math.max(0, elPosition.fromElViewport.top), will fix the problem with negative offsets.

Unfortunatelly, I'm not deeply familiar with the code of cypress, so I can't predict whether any other behavior of the system will be affected or not by this change.

Can anyone tell me where else this code is used? And is this the right place in code to fix the problem?

For now I just overwrote the screenshot command as a workaround and it works for me:

Cypress.Commands.overwrite(
  'screenshot',
  (originalFn, subject, name, options) => {
    if (subject) {
      let scrollY = null;
      const win = Cypress.dom.getWindowByElement(subject[0]);

      return originalFn(subject, name, {
        ...options,
        onBeforeScreenshot(...args) {
          if (options.onBeforeScreenshot) {
            options.onBeforeScreenshot(...args);
          }

          scrollY = Object.getOwnPropertyDescriptor(win, 'scrollY');

          Cypress.log({
            name: 'stub scrollY',
            displayName: 'stub sY',
            message: `${win.scrollY} => ${
              win.scrollY ? win.scrollY - 1 : win.scrollY
            }`,
          });

          Object.defineProperty(win, 'scrollY', {
            value: win.scrollY ? win.scrollY - 1 : win.scrollY,
          });
        },
        onAfterScreenshot(...args) {
          Object.defineProperty(win, 'scrollY', scrollY);

          if (options.onAfterScreenshot) {
            options.onAfterScreenshot(...args);
          }

          Cypress.log({
            name: 'restore scrollY',
            displayName: 'restore sY',
            message: `${win.scrollY}`,
          });
        },
      });
    }

    return originalFn(subject, name, options);
  }
);

Just tested this and seems to be working OK! Fixed the issue I was having.

@aleksandryackovlev
Copy link

I'm getting this error too. It's preventing me from being able to use Cypress to accomplish our goals.

@jfhector try to put into cypress/support/commands.js that Cypress.Commands.overwrite(... code I wrote above. Maybe it would fix the issue.

@jfhector
Copy link

jfhector commented Nov 18, 2020

@aleksandryackovlev I've tried the workaround you've suggested, and I'm still getting the same issue.

Thanks for trying to help though!

For now I'm getting ok results from the workaround that @iGoog suggested above. But with that workaround, the image files are not clipped very precisely, so I expect some inconsistent results when my colleagues (or the pipeline) run those tests.

@cypress-bot cypress-bot bot added stage: needs review The PR code is done & tested, needs review and removed stage: ready for work The issue is reproducible and in scope labels Nov 30, 2020
@jennifer-shehane
Copy link
Member

There's a PR open to fix this here: #9281

@cypress-bot cypress-bot bot added stage: pending release and removed stage: needs review The PR code is done & tested, needs review labels Nov 30, 2020
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Nov 30, 2020

The code for this is done in cypress-io/cypress#9281, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Nov 30, 2020

Released in 6.0.1.

This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v6.0.1, please open a new issue.

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Nov 30, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.