Skip to main content
Cypress应用

writeFile

向指定文件写入内容。

语法

cy.writeFile(filePath, contents)
cy.writeFile(filePath, contents, encoding)
cy.writeFile(filePath, contents, options)
cy.writeFile(filePath, contents, encoding, options)

用法

正确用法

cy.writeFile('menu.json')

参数

filePath (String)

项目根目录(包含Cypress配置文件的目录)中的文件路径。

contents (String, Array, Object 或 Buffer)

要写入文件的内容。

encoding (String)

写入文件时使用的编码。支持以下编码:

  • 'ascii'
  • 'base64'
  • 'binary'
  • 'hex'
  • 'latin1'
  • 'utf8'
  • 'utf-8'
  • 'ucs2'
  • 'ucs-2'
  • 'utf16le'
  • 'utf-16le'
  • null

显式使用null可以直接写入Buffer,而无需先将其编码为字符串。

options (Object)

传入一个选项对象以更改cy.writeFile()的默认行为。

选项默认值描述
logtrue命令日志中显示该命令
flagw文件系统标志,与fs.writeFile相同
encodingutf8写入文件时使用的编码
timeoutdefaultCommandTimeout等待.writeFile()解析的时长,超过则超时
info

要将编码与其他选项一起使用,请将选项对象作为第三个参数,并在其中包含编码。这与fs.writeFile的行为相同。

生成结果 了解主题管理

  • cy.writeFile()返回null

示例

文本

txt文件写入文本

如果文件路径不存在,将创建该文件及其路径。如果文件已存在,将被覆盖。

cy.writeFile('path/to/message.txt', 'Hello World')
cy.readFile('path/to/message.txt').then((text) => {
expect(text).to.equal('Hello World') // true
})

{projectRoot}/path/to/message.txt将创建并包含以下内容:

 "Hello World"

JSON

向文件写入JSON

JavaScript数组和对象将被字符串化并格式化为文本。

cy.writeFile('path/to/data.json', { name: 'Eliza', email: 'eliza@example.com' })
cy.readFile('path/to/data.json').then((user) => {
expect(user.name).to.equal('Eliza') // true
})

{projectRoot}/path/to/data.json将创建并包含以下内容:

{
"name": "Eliza",
"email": "eliza@example.com"
}

将响应数据写入fixture文件

cy.request('https://jsonplaceholder.typicode.com/users').then((response) => {
cy.writeFile('cypress/fixtures/users.json', response.body)
})

// 现在可以生成并使用我们的fixture文件
cy.fixture('users').then((users) => {
expect(users[0].name).to.exist
})

编码

将编码指定为字符串

cy.writeFile('path/to/ascii.txt', 'Hello World', 'ascii'))

{projectRoot}/path/to/message.txt将创建并包含以下内容:

Hello World

在选项对象中指定编码

cy.writeFile('path/to/ascii.txt', 'Hello World', {
encoding: 'ascii',
flag: 'a+',
})

标志

向文件末尾追加内容

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

注意,追加操作假定文件为纯文本文件。如果要合并JSON对象,需要先读取它,添加新属性,然后将合并后的结果写回。

const filename = '/path/to/file.json'

cy.readFile(filename).then((obj) => {
obj.id = '1234'
// 写入合并后的对象
cy.writeFile(filename, obj)
})

类似地,如果需要向数组推送新项:

const filename = '/path/to/list.json'

cy.readFile(filename).then((list) => {
list.push({ item: 'example' })
// 写入合并后的数组
cy.writeFile(filename, list)
})

Buffer

直接写入Buffer而不编码为字符串

const filename = '/path/to/file.png'

cy.readFile(filename, null).then((obj) => {
// <Buffer ef 3a bf ... >
cy.writeFile(filename, obj, null)
})

规则

要求 了解命令链

  • cy.writeFile()必须链接到cy
  • cy.writeFile()要求文件必须成功写入磁盘。任何阻止此操作的情况(如操作系统权限问题)都会导致失败。

断言 了解断言

  • cy.writeFile()只会运行一次链式断言,不会重试

超时设置 了解超时机制

  • cy.writeFile()在写入内容需要大量时间编码时可能会超时。

命令日志

向文件写入数组

cy.writeFile('info.log', ['foo', 'bar', 'baz'])

上述命令将在命令日志中显示为:

命令日志 writeFile

当点击命令日志中的writeFile命令时,控制台输出如下:

控制台日志 writeFile

历史

版本变更
9.2.0添加了timeout选项
4.0.0cy.writeFile()现在返回null而不是contents
3.1.1添加了flag选项和通过a+追加内容的功能
1.0.0添加了cy.writeFile()命令

另请参阅