1. 程式人生 > 其它 >Cypress系列(99)- cypress-downloadfile 外掛, downloadFile() 命令詳解

Cypress系列(99)- cypress-downloadfile 外掛, downloadFile() 命令詳解

技術標籤:linuxpythonnodejsgithubcentos

如果想從頭學起Cypress,可以看下面的系列文章哦

https://www.cnblogs.com/poloyy/category/1768839.html

官方地址

https://github.com/Xvier/cypress-downloadfile

安裝方式

npm

npm install cypress-downloadfile

專案匯入外掛

cypress/support/commands.js檔案下新增下面語句

require('cypress-downloadfile/lib/downloadFileCommand')

cypress/plugins/index.js檔案下寫下面語句即可

const {downloadFile} = require('cypress-downloadfile/lib/addPlugin')
module.exports = (on, config) => {
  on('task', {downloadFile})
}

注意事項

如果測試檔案中自動補全程式碼功能無法立即使用,可以在檔案上方新增下面語句

/// <reference types="cypress-downloadfile"/>

命令原始碼

  Cypress.Commands.add('downloadFile', (url, dir, fileName, userAgent) => {
    return cy.getCookies().then(cookies => {
        return cy.task('downloadFile', {
            url: url,
            directory: dir,
            cookies: cookies,
            fileName: fileName,
            userAgent: userAgent,
        })
    })
})
  • 通過cy.task完成下載檔案的操作
  • 暫時沒搞懂這個 Cookie 有啥用

實際栗子

測試程式碼

執行結果

Console 檢視命令

檔案下載目錄

如果資料夾不存在,則在 cypress 安裝目錄(和cypress.json同級目錄)下生成一個資料夾