0

我想测试上传文件和下载文件的内容是否相同。所以,以下是我通过柏树尝试过的:

it.only(cfg.testname, () => {

    // 1. Login to website, navigate to desired webapge
    // 2. Upload the file
    // 3. Download the file

    // cy.wait(5000)
    
    // 4. Read the uploaded file:
    const fileContent = cy.fixture(_filePath)
    console.log('fixture file path', '======>', _filePath)
    console.log('fixture file content', '=====>', fileContent)
    
    // 5. Read the downloaded file:
    const downloadsFolder = Cypress.config("downloadsFolder")
    const downloadedFileContent = cy.readFile(path.join(downloadsFolder, _fileName))
    console.log('downloaded file path', '======>', path.join(downloadsFolder, fileName))
    console.log('downloaded file content','====>', downloadedFileContent)
    
    // 6. Check if they are equal:
    expect(downloadedFileContent).equals(fileContent)            
}) 

但是,当我运行此测试时,它甚至没有完成登录步骤并立即给出断言错误第 6 步,即 on expect()...

AssertionError: expected { Object (userInvocationStack, specWindow, ...) } to equal { 
Object (userInvocationStack, specWindow, ...) }
    at Context.eval (VM753 tests:224)

当我评论第 6 步时expect()...,它会正确登录、上传文件和下载文件。所以,我觉得我应该让这个过程等到下载完成之前expect()...。所以我尝试取消注释cy.wait(5000),但没有帮助。它仍然给我以上错误(当然没有expect()...注释)。

Q1。为什么会有这种行为?
Q2。我应该如何解决这个问题?

PS:我在控制台中遇到了一堆我无法理解的错误。这是控制台的屏幕截图:

在此处输入图像描述

4

1 回答 1

1

夹具读取是异步的,因此您需要使用.then(), 与cy.readFile()

使用path.join(downloadsFolder, _fileName)可能不起作用,因为它是一个节点命令,而是替换一个字符串模板

如果您有 JSON 格式的复杂文件,也请尝试.to.deep.eq

cy.fixture(_filePath).then(fileContent => {
  const downloadsFolder = Cypress.config("downloadsFolder")
  const downloadPath = `${downloadsFolder}/${_fileName}`
  cy.readFile(downloadPath).then(downloadedFileContent => { 
    expect(downloadedFileContent).equals(fileContent)   
    // or may need deep
    // expect(downloadedFileContent).to.deep.eq(fileContent)   
  })
})
于 2021-09-28T07:54:07.820 回答