【问题标题】:Wrong API on file-upload using Cypress.io for E2E testing使用 Cypress.io 进行 E2E 测试的文件上传错误 API
【发布时间】:2021-03-19 08:27:56
【问题描述】:

我正在使用 Cypress.io 测试一个门户,它具有文件上传功能。

但是我的文件总是上传失败,因为 API 调用出错了。

正确的 API 调用:
**

POST 200 /etl/v1.0.0/datauploaderetl/spaces/etl_jyddc0tx/data-files

**

但是通过 Cypress 上传时,URL 如下: **

POST 404 /etl/v1.0.0/datauploaderetl/data-files

** 如您所见,API 不正确。我在这里添加了等待,但它仍然不起作用。 以下是一段代码:

cy.fixture(fileName1).then(fileContent => {
        cy.get('input[type="file"]').attachFile({
            fileContent: fileContent.toString(),
            fileName: fileName1,
            mimeType: fileType
        })
    });
    cy.waitUntil(() => cy.get(":nth-child(98) > .modal > .modal-lg > .modal-content > .modal-body")
        .should('contain.text', 'Status: completed')
    );

请帮忙!

【问题讨论】:

    标签: node.js cypress ui-automation e2e-testing browser-automation


    【解决方案1】:

    在 Command.js 中,添加以下代码:

    let LOCAL_STORAGE_MEMORY = {};
    
    Cypress.Commands.add("saveLocalStorage", () => {
        Object.keys(localStorage).forEach(key => {
            LOCAL_STORAGE_MEMORY[key] = localStorage[key];
        });
    });
    
    Cypress.Commands.add("restoreLocalStorage", () => {
        Object.keys(LOCAL_STORAGE_MEMORY).forEach(key => {
            localStorage.setItem(key, LOCAL_STORAGE_MEMORY[key]);
        });
    });
    

    然后在测试用例文件中,分别在下面添加 beforeEach 和 afterEach 块:

     beforeEach(() => {
            cy.restoreLocalStorage();
        })
    
        afterEach(() => {
            cy.saveLocalStorage();
        })
    

    这将解决赛普拉斯在浏览器中清除“本地存储”的问题。

    【讨论】:

      【解决方案2】:

      根据文档,这是上传文件的方式:

      cy.fixture('filepath').as('filetoupload')
      cy.get('input[type=file]').then(function($input) {
        // convert the logo base64 string to a blob
        const blob = Cypress.Blob.base64StringToBlob(this.filetoupload, fileType)
        $input.fileupload('add', { files: blob })
      })
      

      cy.fixture('filepath').as('filetoupload')

      cy.get('input[type=file]').then(function(el) {
        // convert the logo base64 string to a blob
        const blob = Cypress.Blob.base64StringToBlob(this.filetoupload,fileType )
      
        const file = new File([blob], '<path>', { type: fileType })
        const list = new DataTransfer()
      
        list.items.add(file)
        const myFileList = list.files
      
        el[0].files = myFileList
        el[0].dispatchEvent(new Event('change', { bubbles: true }))
      })
      

      https://docs.cypress.io/api/utilities/blob.html#Image-Fixture

      【讨论】:

      • 感谢您的回答。但是,文件上传没有问题。问题是柏树在运行之前清除了浏览器的本地存储。因此,在执行过程中所有权限都丢失了。解决方法是在 beforeEach() 和 afterEach() 处分别添加代码来 restoreLocalStorage 和 saveLocalStorage。
      • 你可以添加那个答案
      • 我上述问题的答案如下:
      • 我的意思是将其添加为单独的答案,这对其他人会有所帮助,您可以在一天后接受自己的答案
      • 是的,我赞成你能解决它的好地方
      猜你喜欢
      • 2012-10-30
      • 2022-12-16
      • 1970-01-01
      • 1970-01-01
      • 2020-03-24
      • 2015-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多