我正在测试文件上传,但在Cypress.io测试中无法正常工作,刷新网站后,看不到'avatar.JPG'(位于../fixtures/avatar下.JPG )没有连接。我在测试中调用uploadFile(),该测试写在commands.js文件中。有人请在这里告知潜在的问题。
cy.get('.qq-upload-button span').contains("Upload New File").click({force: true})
const fileName = '../fixtures/avatar.JPG';
const fileType = 'JPG|JPEG|PNG';
const fileInput = 'input[type=file]';
cy.uploadFile(fileName, fileType, fileInput);
cy.wait(2000)
cy.get("#save-edit-button").click()
../ support / commands.js
Cypress.Commands.add('uploadFile', (fileName, fileType = ' ', selector) => {
cy.get('.qq-upload-button span').contains("Upload New File").then(subject => {
cy.fixture("avatar.JPG", 'base64')
.then(Cypress.Blob.base64StringToBlob)
.then(blob => {
const el = subject[0]
const testFile = new File([blob], fileName, { type: fileType })
const dataTransfer = new DataTransfer()
dataTransfer.items.add(testFile)
el.files = dataTransfer.files
})
})
})