如何检查上传和下载的文件是否与 cypress 具有相同的内容?
How can I check if uploaded and downloaded file have same content with cypress?
我想测试上传文件和下载文件的内容是否相同。所以,以下是我通过赛普拉斯尝试的:
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 步,即 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:我在控制台中遇到了一堆我无法理解的错误。这是控制台的屏幕截图:
夹具读取是异步的,所以需要使用.then()
,同cy.readFile()
使用 path.join(downloadsFolder, _fileName)
可能无法正常工作,因为它是一个 Node 命令,请替换为字符串模板
如果您有 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)
})
})
我想测试上传文件和下载文件的内容是否相同。所以,以下是我通过赛普拉斯尝试的:
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 步,即 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:我在控制台中遇到了一堆我无法理解的错误。这是控制台的屏幕截图:
夹具读取是异步的,所以需要使用.then()
,同cy.readFile()
使用 path.join(downloadsFolder, _fileName)
可能无法正常工作,因为它是一个 Node 命令,请替换为字符串模板
如果您有 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)
})
})