作用
语法格式
cy.readFile(filePath)
cy.readFile(filePath, encoding)
cy.readFile(filePath, options)
cy.readFile(filePath, encoding, options)
filePath
项目根目录(包含默认 cypress.json 配置文件的目录)中需要读取的文件的路径
encoding
读取时需要使用的编码
- ascii
- base64
- binary
- hex
- latin1
- utf8
- utf-8
- ucs2
- ucs-2
- utf16le
- utf-16le
options
- log:是否将命令显示到命令日志中,默认 true
- timeout:命令超时时间
正确用法
cy.readFile('users.json')
命令返回结果
读取 txt 文件的栗子
测试代码
运行结果
读取 json 文件的栗子
json 文件数据
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
}
}
测试代码
运行结果
读取 yaml 文件的栗子
yaml 文件
- 1
- 2
- 3
测试代码
运行结果
yaml 扩展使用
YAML = require('yamljs');
// 解析 YAML 文件
nativeObject = YAML.parse(yamlString);
// 生成 YAML 字符串
yamlString = YAML.stringify(nativeObject, 4);
// 加载 YAML 文件
nativeObject = YAML.load('myfile.yml');