cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
46.87k stars 3.17k forks source link

cy.selectFile() must only be invoked from the spec file or support file. #29800

Open gmaggiodev opened 3 months ago

gmaggiodev commented 3 months ago

Current behavior

After upgrading to latest version of Cypress, when I try to upload a file using cy.selectFile() I got an error (cy.selectFile() must only be invoked from the spec file or support file.). The same code works with Cypress 11.14.0.

Desired behavior

Should work as version 11.14.0

Test code to reproduce

Using cucumber preprocessor I have this rule that is not wotking anymore:

When('the user upload the file {string}', (fileName: string) => {
    cy.get('tf-uploader input[type="file"]').selectFile(`cypress/files/${fileName}`);
});

Cypress Version

13.13.0

Node version

v18.19.0

Operating System

Windows 11

Debug Logs

at <unknown> (https://<###removed###>/__cypress/runner/cypress_runner.js:114038:70)
    at tryCatcher (https://<### removed ###>/__cypress/runner/cypress_runner.js:1807:23)
    at Promise._settlePromiseFromHandler (https://<### removed ###>/__cypress/runner/cypress_runner.js:1519:31)
    at Promise._settlePromise (https://<### removed ###>/__cypress/runner/cypress_runner.js:1576:18)
    at Promise._settlePromise0 (https://<### removed ###>/__cypress/runner/cypress_runner.js:1621:10)
    at Promise._settlePromises (https://<### removed ###>/__cypress/runner/cypress_runner.js:1697:18)
    at <unknown> (https://<### removed ###>/__cypress/runner/cypress_runner.js:2392:25)
From previous event:
    at Promise.longStackTracesCaptureStackTrace [as _captureStackTrace] (https://<### removed ###>/__cypress/runner/cypress_runner.js:3486:19)
    at Promise._then (https://<### removed ###>/__cypress/runner/cypress_runner.js:1239:17)
    at Promise.then (https://<### removed ###>/__cypress/runner/cypress_runner.js:1132:17)
    at next (https://<### removed ###>/__cypress/runner/cypress_runner.js:144081:64)
    at <unknown> (https://<### removed ###>/__cypress/runner/cypress_runner.js:144102:16)
    at tryCatcher (https://<### removed ###>/__cypress/runner/cypress_runner.js:1807:23)
    at Promise._settlePromiseFromHandler (https://<### removed ###>/__cypress/runner/cypress_runner.js:1519:31)
    at Promise._settlePromise (https://<### removed ###>/__cypress/runner/cypress_runner.js:1576:18)
    at Promise._settlePromise0 (https://<### removed ###>/__cypress/runner/cypress_runner.js:1621:10)
    at Promise._settlePromises (https://<### removed ###>/__cypress/runner/cypress_runner.js:1701:18)
    at <unknown> (https://<### removed ###>/__cypress/runner/cypress_runner.js:2392:25)
From Your Spec Code:
    at Context.eval (https://<### removed ###>/__cypress/tests?p=cypress\features\7-provisioning%20tools\Marketplace%20Packages'%20Test\PT0009-Package%20Installation%20Job%20Vacancy%20Report.feature:21076:50)
    at Registry.runStepDefininition (https://<### removed ###>/__cypress/tests?p=cypress\features\7-provisioning%20tools\Marketplace%20Packages'%20Test\PT0009-Package%20Installation%20Job%20Vacancy%20Report.feature:8508:48)
    at Object.fn (https://<### removed ###>/__cypress/tests?p=cypress\features\7-provisioning%20tools\Marketplace%20Packages'%20Test\PT0009-Package%20Installation%20Job%20Vacancy%20Report.feature:15707:43)
    at runStepWithLogGroup (https://<### removed ###>/__cypress/tests?p=cypress\features\7-provisioning%20tools\Marketplace%20Packages'%20Test\PT0009-Package%20Installation%20Job%20Vacancy%20Report.feature:15180:29)
    at Context.eval (https://<### removed ###>/__cypress/tests?p=cypress\features\7-provisioning%20tools\Marketplace%20Packages'%20Test\PT0009-Package%20Installation%20Job%20Vacancy%20Report.feature:15703:62)

Other

No response

sofrlachapelle commented 2 months ago

I am also getting this regularly. It's incredibly frustrating as it is preventing me from deploying any automation that includes attachments. This wasn't such a big deal a few versions ago but now it happens frequently, but not every time. This was also happening with cy.task() but I have since converted my task to a custom command to avoid the issue. I can't seem to find a way to resolve the selectFile() problem though. For some reason this isn't as frequent on macOS (M1 MacBook) but happens on Windows constantly. I've gone through many iterations of functions and custom commands to try to resolve the issue. Also tried running it from the spec file directly and from my commands support file. Here is my latest iteration that is still failing at about a 15% rate of failure. The referenced fixture is a JSON object containing the relative paths for 40 images.

cy.fixture("images").then((images) => {
  const keys = Object.keys(images);
  const randomCount = Cypress._.random(3, 5); // Select 3 to 5 images
  const selectedImages = Cypress._.sampleSize(keys, randomCount);
  cy.get("[notes='Upload Images'] ~ input").selectFile(
      selectedImages.map((key) => images[key]),
      { force: true }
  );
});