soc*_*way 7 javascript cypress
Cypress.io 中的 cy.readFile 和 cy.fixture 有什么区别?我们应该在什么情况下使用 cy.readFile 和 cy.fixture ?
cy.readFile('menu.json')
cy.fixture('users/admin.json') // Get data from {fixturesFolder}/users/admin.json
Run Code Online (Sandbox Code Playgroud)
Jos*_*ica 14
有两个主要区别。
cy.readFile()从您的 cypress 项目文件夹开始,即您所在的文件夹cypress.json。换句话说,cy.readFile("test.txt")将从(path-to-project)\test.txt.
cy.fixture()在fixtures文件夹中启动。cy.fixture("test.txt")将从(path-to-project)\cypress\fixtures\test.txt. 请注意,如果您在cypress.json.
此处似乎不支持绝对文件路径。
cy.fixture()尝试猜测文件的编码。cy.fixture()假定某些文件扩展名的编码,但cy.readFile()不假定,至少在一种特殊情况下除外(见下文)。
例如,cy.readFile('somefile.png')将其解释为文本文档,只是盲目地将其读入字符串。这会在打印到控制台时产生垃圾输出。但是,cy.fixture('somefile.png')将改为读取 PNG 文件并将其转换为 base64 编码的字符串。
这种差异不在于两个函数的能力,而在于默认行为;如果您指定编码,则两个函数的作用相同:
cy.readFile('path/to/test.png', 'base64').then(text => {
console.log(text); // Outputs a base64 string to the console
});
cy.fixture('path/to/test.png', 'base64').then(text => {
console.log(text); // Outputs the same base64 string to the console
});
Run Code Online (Sandbox Code Playgroud)
cy.readFile()并不总是以纯文本形式阅读。cy.readFile() 读取 JSON 文件时返回一个 Javascript 对象:
cy.readFile('test.json').then(obj => {
// prints an object to console
console.log(obj);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9003 次 |
| 最近记录: |