I want to upload a .txt file using Cypress. Is there a way to make Cypress choose the file or do I have to manually select the file every time?Clicking the 'Choose file' buttonwindow pops upThe 'window pops up' is where I want cypress to select a file, is it possible?

Thanks for all the help

html of the button

2

Best Answer


You can use the selectFile method from cypress to upload the file.

cy.get('[title="Choose File"]').selectFile('path/to/txt') //Selects the Filecy.get('input[title="Upload"]').click() //Clicks upload button

You need to target the input[type="file"] to add the file to the input

cy.get('input[type="file"]').selectFile(fileName).trigger('input'); // may also be required cy,get('input[type="submit"]).click();

or by finding the label and using sibling() to move to the input

cy.contains('label', 'Choose file').sibling('input[type="file"]').selectFile(fileName).trigger('input'); // may also be required cy,get('input[type="submit"]).click();