I am using cypress fixture and intercept to upload a file and also validate if the upload endpoint (post) was successful. here is the code:
describe('something', () => {
let projectID;
before(() => {
projectID = localStorage.getItem('projectID');
cy.intercept('POST', '/graphql', (req) => {
cy.log(req.body);
if (req.body.operationName === 'postRequest') {
req.alias = 'postRequest';
}
});
});
it('should validate file upload', () => {
const fileName = 'test_file_upload.xlsx';
cy.get('.file-uploader').should('exist');
cy.get('.upload-file-label').first().click({ force: true });
cy.fixture(fileName, 'binary').then((fileContent) => {
cy.get('input[type=file]')
.upload(
{
fileContent,
fileName,
mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
encoding: 'utf-8',
},
{ subjectType: 'input' }
)
.wait('@postRequest')
.then((interception) => {
cy.log(interception.response.body);
});
});
});
});
I am not exactly sure what is error refer to, but I am guessing it might be async issue.
thank you for your help
The .wait() must be chained off a cy when you pass an alias.
// code looks good until here
cy.get('input[type=file]')
.upload(
{
fileContent,
fileName,
mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
encoding: 'utf-8',
},
{ subjectType: 'input' }
)
cy.wait('@postRequest')
.then((interception) => {
cy.log(interception.response.body);
});
When passed a time agrument (ie. 500) .wait() can be chained off a cy or any other command.
cy.get('.selector')
.wait(1000)
cy.wait(500)