javascriptjquerycypressjquery-file-upload

How to upload an image in cypress?


This is my code

I have install cypress-file-upload

describe('TEST', function(){

    it('File upload', function(){

        cy.visit('https://tinypng.com/')


    })

    it('File Upload using cypress-file-upload package', () => {
        const filepath = 'train.jpg'
        cy.get('.icon').attachFile(filepath)
        cy.wait(5000)
        
    })
})

Solution

  • The figure.icon element on tinypng.com requires a file to be dragged onto it, but with cypress-file-upload the drag-and-drop option is not the default.

    This will work,

    cy.get('.icon')
      .attachFile(filepath, { subjectType: 'drag-n-drop' })
    

    If you prefer, there is an input on the page

    cy.get('input[type="file"]')
      .attachFile(filepath, { subjectType: 'input' })  // input is the default type, 
                                                       // so not strictly needed