无法使用cypress上传png图片(徽标)

时间:2019-04-16 18:51:38

标签: typescript cypress

我正在尝试使用cypress上传png文件,这是到目前为止我已经尝试过的

Cypress.Commands.add('upload_file', (fileName, selector) => {
  return cy.get(selector).then(subject => {
    return cy.fixture(fileName, 'base64')
      .then(Cypress.Blob.base64StringToBlob)
      .then(blob => {
        const el = subject[0]
        const testFile = new File([blob], fileName, { type: 'image/png' })
        const dataTransfer = new DataTransfer()
        dataTransfer.items.add(testFile)
        el.files = dataTransfer.files
        return subject;
      })
  })
})

在我的测试中称呼它如下:

 cy.upload_file("logo.png", ".jss378");

它没有给出任何错误,但是也没有上传任何内容。请注意,选择器(.jss378)是动态的,所以我想使用非动态的东西

这是我的HTML代码: enter image description here

1 个答案:

答案 0 :(得分:0)

使用cypress上载插件使其正常工作

步骤如下:

1)在command.js中添加以下内容:

bool isFirstOutput = true;
for_each(element in list) {
    if(not isFirstOutput) {
        print delimiter;
    }
    print element;
    isFirstOutput = false;
}

2)并在测试中使用它:

import 'cypress-file-upload';

文档: https://docs.cypress.io/plugins/index.html#content