question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

selectFile from fixture will omit file name if used in two consequent tests

See original GitHub issue

Current behavior

I have two tests uploading pdf file like this

cy.fixture('PDF/ThisIsTest.pdf', { encoding: null }).as('file')
    cy.get(personalDetails.tbModal.id).should('be.visible')
    cy.get(personalDetails.tbModal.fileInput).selectFile( '@file', { force: true })

If I run only one test it works fine, but if I run for example absolutely the same test twice (for example just by duplicating its code) then for the second test file is attached without name and endpoint returns error file not found. Workaround is to specify file name directly or use different fixure files and you can run as many tests as you want.

cy.fixture('PDF/ThisIsTest.pdf', { encoding: null }).as('file')
    cy.get(personalDetails.tbModal.id).should('be.visible')
    cy.get(personalDetails.tbModal.fileInput).selectFile({ contents: '@file', fileName: 'file.pdf' }, { force: true })

Desired behavior

It should be possible to just use the same fixture without specifying file name.

Test code to reproduce

cy.fixture('PDF/ThisIsTest.pdf', { encoding: null }).as('file')
    cy.get(personalDetails.tbModal.id).should('be.visible')
    cy.get(personalDetails.tbModal.fileInput).selectFile( '@file', { force: true })

Cypress Version

9.7.0

Other

No response

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
BlueWindscommented, Oct 18, 2022

From the opening post in this issue:

Workaround is to specify file name directly or use different fixure files and you can run as many tests as you want.

cy.fixture('PDF/ThisIsTest.pdf', { encoding: null }).as('file')
    cy.get(personalDetails.tbModal.id).should('be.visible')
    cy.get(personalDetails.tbModal.fileInput).selectFile({ contents: '@file', fileName: 'file.pdf' }, { force: true })

Specifying the file name explicitly always works.

1reaction
mschilecommented, May 31, 2022

I was able to reproduce the issue using the following test project: https://github.com/mschile/cypress-test-tiny-issue-21936.

/// <reference types="cypress" />
describe('page', () => {
  it('works', () => {
    cy.visit('/cypress/fixtures/index.html')

    cy.fixture('example.json', { encoding: null }).as('file')
    cy.get('#file').selectFile('@file')

    cy.get('#file')
      .then((input) => {
        expect(input[0].files.length).to.eq(1)
        expect(input[0].files[0].name).to.eq('example.json')
        expect(input[0].files[0].type).to.eq('application/json')
      })
  })

  it('works on subsequent test', () => {
    cy.visit('/cypress/fixtures/index.html')

    cy.fixture('example.json', { encoding: null }).as('file')
    cy.get('#file').selectFile('@file')

    cy.get('#file')
      .then((input) => {
        expect(input[0].files.length).to.eq(1)
        expect(input[0].files[0].name).to.eq('example.json')
        expect(input[0].files[0].type).to.eq('application/json')
      })
  })
})
Screen Shot 2022-05-31 at 2 30 39 PM
Read more comments on GitHub >

github_iconTop Results From Across the Web

Cypress- SelectFile() Not working as expected in Chrome
The code when running the test through FF runs as expected and the file is uploaded with a 200 on the post request....
Read more >
Easy-Wire™ Software Manual - Cirris
4.4.2 Test Process options . ... Click the text box and enter a station name used to ... There is no undo function...
Read more >
Changelog - Cypress Documentation
Fixed an issue with Angular Component Testing where urls within SASS/SCSS files were not being correctly resolved which could result in incomplete styling....
Read more >
Touchstone Docs
More information about Fixtures can be found in the FHIR Testing ... /_reference/resources/filename.json” as the reference value. ... and select File :.
Read more >
MTS TestSuite™ TW Essential User Guide
MTS TestSuite Folders and Files Management Overview ... The left section contains the test definition tree, which is used to guide your progress...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found