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.

Not able to upload a png file in cypress

See original GitHub issue

I am trying to upload a png file using cypress and this is that i have tried so far

Cypress.Commands.add('upload_file', (fileName, selector) => {
  return cy.get(selector).then(subject => {
    return cy.fixture(fileName, 'base64')
      .then(Cypress.Blob.base64StringToBlob)
      .then(blob => {
        const el = subject[0]
        const testFile = new File([blob], fileName, { type: 'image/png' })
        const dataTransfer = new DataTransfer()
        dataTransfer.items.add(testFile)
        el.files = dataTransfer.files
        return subject;
      })
  })
})

calling this in my test as below:

cy.upload_file("logo.png", ".jss378");

Have also tried different locators but no luck

it does not give any error but also it does not upload anything. note that the selector(.jss378) is dynamic so i want to use something that is not dynamic

Here is my HTML code

Screen Shot 2019-04-17 at 2 52 15 PM

I am not getting any error but it not uploading anything

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
mbilalbakhtawarcommented, Jan 2, 2020
    it('Uploading png image', () => {
        cy.fixture('testPicture.png').then(fileContent => {
            cy.get('input[type="file"]').upload({
                fileContent: fileContent.toString(),
                fileName: 'testPicture.png',
                mimeType: 'image/png'
            });
        });
    });

Use cypress file upload package: https://www.npmjs.com/package/cypress-file-upload Note: testPicture.png must be in fixture folder of cypress

0reactions
chirag-jogogocommented, Apr 17, 2019

@must-git-good - thank you, it worked

Read more comments on GitHub >

github_iconTop Results From Across the Web

Not able to upload the png image(logo) using cypress
Got it working by using cypress upload plugin. here are the steps: 1) adding the following in the command.js: import 'cypress-file-upload';.
Read more >
How to upload a file in cypress - TestersDock
1. Go to https://the-internet.herokuapp.com/upload. · 2. Select a file and upload it. · 3. After upload, validate that the file was successfully ...
Read more >
How to upload a file in Cypress? - YouTube
cypress # upload #automationbroIn this tutorial, we will learn how to upload a file in Cypress. ... Your browser can't play this video....
Read more >
Uploading files made easy with the .selectFile command
selectFile() command, new in Cypress 9.3.0. Selecting files in an HTML5 input elementWith the new .selectFile() command, you.
Read more >
How to upload files with Cypress - DEV Community ‍ ‍
png file, checks that the previous file has been replaced by the new one. Finally, it deletes the note and verifies that it...
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