cypress icon indicating copy to clipboard operation
cypress copied to clipboard

cy.selectFile within cy.origin not working correctly

Open mduft opened this issue 1 year ago • 16 comments

Current behavior

cy.selectFile behaves wrong as soon as wrapped in a cy.origin call. I had multiple different kinds of errors while playing with it, but did not get it to work properly somehow. I tried with the filename shortcut and the fixture syntax, both to no avail. The fixture syntax does look like it works, but somehow the file content is not properly attached to the input, e.g. an image is not shown correctly when uploading an image.

Desired behavior

File should be attached to selectFile properly.

Test code to reproduce

https://github.com/mduft/cypress-test-tiny/tree/master

Cypress Version

12.2.0

Node version

16.17.2

Operating System

Windows 11

Debug Logs

No response

Other

No response

mduft avatar Dec 23 '22 09:12 mduft

Also, in my real world use-case I get this (additionally to the path being undefined as in the tiny test case): image Which I did not manage to reproduce with the tiny test case, and I have NO idea why...?

xml.js:30 Uncaught TypeError: Cannot destructure property 'subject' of 'data' as it is null.
    at SpecBridgeCommunicator.handleSubjectAndErr (xml.js:30:2)
    at SpecBridgeCommunicator.toPrimary (xml.js:30:2)
    at SpecBridgeCommunicator.<anonymous> (xml.js:30:2)
    at SpecBridgeCommunicator.emit (xml.js:30:2)
    at SpecBridgeCommunicator.onMessage (xml.js:30:2)
    at xml.js:30:2

mduft avatar Dec 23 '22 09:12 mduft

@mduft Interesting! Thank you for the reproduction! I am able to reproduce the issue you are seeing with the example you provided and verifying it works without the use of cy.origin(). I also tried reading from the fixture like our documentation suggests and am seeing:

      cy.origin('https://www.w3schools.com', () => {
        cy.visit('/howto/howto_html_file_upload_button.asp');
        cy.fixture('example.json', { encoding: null }).as('myFixture')
        cy.get('input[type=file]').selectFile('@myFixture', { force: true });
      });

and am seeing:

Image

Something is def acting wonky with .selectFile(), inside of the .origin() callback. Marking this as a bug.

emilyrohrbough avatar Dec 23 '22 21:12 emilyrohrbough

Are there any workarounds for this in the meantime?

Danc9520 avatar Jan 06 '23 15:01 Danc9520

Are there any workarounds for this in the meantime?

None that I know of. My 12.x update is completely blocked by this ATM.

mduft avatar Jan 09 '23 06:01 mduft

Any news on this? I'm stuck on Cypress 11.x without any way to update to 12.x due to this issue.

mduft avatar Jan 18 '23 06:01 mduft

We still cannot move forward with Cypress 12 due to this BLOCKER. Any plans to fix this anytime soon?

mduft avatar Feb 27 '23 08:02 mduft

This issue is also blocking one of my tests, so +1

georgwindhaber avatar Apr 11 '23 12:04 georgwindhaber

Just wanted to chime in saying this issue is blocking the writing of our tests as well, would be great to have any updates.

fredbh avatar May 04 '23 18:05 fredbh

I was able to find a workaround for this issue by reading a fixture and stringifying it into an alias:

  cy.origin('https://www.w3schools.com', () => {
    cy.visit('/howto/howto_html_file_upload_button.asp');
    cy.fixture('example.json').then(JSON.stringify).as('myFixture'); // <= read the fixture with default encoding, then stringify it before saving it as an alias
    cy.get('input[type=file]').selectFile('@myFixture');
  });

chrisbreiding avatar May 08 '23 14:05 chrisbreiding

Unfortunately that does not help with binaries (ZIP files, pictures, etc.), right? We're still blocked on this issue, and still are forced to stay on 11, even though it has been quite many 12 releases now - any chance to get this properly fixed?

mduft avatar Jun 21 '23 05:06 mduft

Please, any news on it?=(( it is a blocker indeed And can anyone tell which cypress version doesn't have this bug? I switched to 11.2.0 and it's not working as well((

DariaBor avatar Jul 12 '23 09:07 DariaBor

Hi, is there any solution about that. I have a problem same

SemaEfe avatar Oct 19 '23 10:10 SemaEfe

Thanks to @chrisbreiding, By referring your code sample I tweaked a bit and tried below and it worked for me, thank you so much.

cy.origin('https://example.com', () => {
    cy.visit('/upload');
     cy.fixture('user.csv').as('myFixture');
        cy.get('[data-testid="file-input"]').selectFile('@myFixture')
  });

nageshcrest avatar Jan 31 '24 13:01 nageshcrest