csb00
csb00

Reputation: 1155

Trying to upload a file in Cypress using cypress-file-upload, but no luck

I was trying to use the following dependency (cypress-file-upload) to upload a file for a test case I am creating in Cypress. However, I came across a thread of comments on GitHub in which it states that the dependency is currently not working. Is there a workaround to this? I currently write my test cases in TypeScript, so any help is appreciated.

Upvotes: 0

Views: 1187

Answers (1)

Lucaspd
Lucaspd

Reputation: 48

There is a way to upload a file in cypress:

1st - In your cypress directory need to create a fixtures folder.

2nd - You need to add here the files that you will use in your tests (json, csv, ...)

3rd - In spec file you need to save the POST answer in a variable like this

beforeEach(() => {
cy.route('POST', '**migration**').as('importMyFile');

3rd - Then add the following code

cy.get('input[type="file"]').attachFile('myFile_cypress.json');

4rd - Depending on your REST service you will already have to handle the answer that you have below. In my case is an HttpResponseBase, which is handled as follows:

cy.wait('@importMyFile').then(($prop) => {
            expect($prop.status).to.equal(200);
            Cypress.env('idMyFile', $prop.response.body.id)
        });

I hope it could be works for you :)

Upvotes: 1

Related Questions