Cypress系列(99)- cypress-downloadfile 插件, downloadFile() 命令詳解


如果想從頭學起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 同級目錄)下生成一個文件夾


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM