自动化测试,让你上班拥有一杯咖啡的时间 | Day 19 - 如何写入档案和读取档案

此系列文章会同步发文到个人部落格,有兴趣的读者可以前往观看喔。

  1. writeFile() 语法

    cy.writeFile(filePath, contents)
    cy.writeFile(filePath, contents, encoding)
    cy.writeFile(filePath, contents, options)
    cy.writeFile(filePath, contents, encoding, options)
    
  2. 可以写成 txt 档或是 json 档,档案会放在 cypress/fixtures 底下

    cy.writeFile('path/to/message.txt', 'Hello Cypress')
    cy.writeFile('path/to/data.json', { name: 'Jennifer', email: '[email protected]' })
    
  3. 在档案的最後附加文字可以用 { flag: 'a+' }

    cy.writeFile('path/to/message.txt', 'Hello World', { flag: 'a+' })
    
  4. readFile() 语法

    cy.readFile(filePath)
    cy.readFile(filePath, encoding)
    cy.readFile(filePath, options)
    cy.readFile(filePath, encoding, options)
    
  5. 动手写程序

    describe('测试写档案', function() {
        it('应该可以在test1档案写东西', function() {
            cy.writeFile('cypress/fixtures/test1.txt', 'Hello Cypress!\n')
        })
    
        it('应该可以在test1档案上附加文字', function() {
            cy.writeFile('cypress/fixtures/test1.txt', 'Hello Cypress!!',  {flag: 'a+',})
        })
    
        it('应该可以在test2档案写东西', function() {
            cy.writeFile('cypress/fixtures/test2.json', { name: 'Jennifer', email: '[email protected]' })
        })
    })
    
    describe('测试读档案', function() {
        it('应该可以在test1档案读档案', function() {
            cy.readFile('cypress/fixtures/test1.txt').should('eq','Hello Cypress!\nHello Cypress!!')
        })
    
        it('应该可以在test2档案读档案', function() {
            cy.readFile('cypress/fixtures/test2.json').its('name').should('eq', 'Jennifer')
        })
    })
    
  6. 看结果
    https://ithelp.ithome.com.tw/upload/images/20211003/20140883wXOHWxB5Ok.png
    https://ithelp.ithome.com.tw/upload/images/20211003/20140883FybRgzUp5h.png
    https://ithelp.ithome.com.tw/upload/images/20211003/20140883YwQaaiSgT1.png

参考资料


<<:  实战操作 - 鸿海 2317

>>:  【设计+切版30天实作】|Day19 - 切版 - Heroheader - 怎麽切出满版heroheader?

OpenStack Glance 介绍

本系列文章同步发布於笔者网站 在经过了好几篇的 Neutron 之後我们终於来到下一个 OpenSt...

Day15 简易资料库RealmSwift小实作2

继续昨天~ 拉取三个Label,这是往後会从资料库回传资料到我们在主画面拉的tableview里显示...

DAY2:离职率预测(上)

人工智慧共创平台-离职率预测(上) 资料来源 因为工作偶尔也会有闲暇时间,尤其我们是业务单位提出需求...

Day24 - 将台湾证券交易所的除权除息计算结果表存入 DB

前言 前面已经知道如何抓「台湾证券交易所」的除权除息计算结果表 CSV 档,接下来要处理资料,并存入...

应用系统的防护基准-开发过程的程序与记录(SSDLC)

从源头降低风险(X),可归究责任(O) 适用人员: 技术人员(开发人员)。 适用法规: 资通安全责任...