fixture
加载位于文件中的固定数据集。
语法
cy.fixture(filePath)
cy.fixture(filePath, encoding)
cy.fixture(filePath, options)
cy.fixture(filePath, encoding, options)
用法
正确用法
cy.fixture('users').as('usersJson') // 从users.json加载数据
cy.fixture('logo.png').then((logo) => {
// 从logo.png加载数据
})
参数
filePath (String)
指向fixturesFolder
内文件的路径,默认为cypress/fixtures
。
您可以将fixture嵌套在文件夹中,并通过定义从fixturesFolder开始的路径来引用它们:
cy.fixture('users/admin.json') // 从{fixturesFolder}/users/admin.json获取数据
encoding (String)
读取文件时使用的编码。支持以下编码:
'ascii'
'base64'
'binary'
'hex'
'latin1'
'utf8'
'utf-8'
'ucs2'
'ucs-2'
'utf16le'
'utf-16le'
'null'
显式使用null
将返回一个Cypress.Buffer
实例,无论文件扩展名是什么。
options (Object)
传入一个选项对象以更改cy.fixture()
的默认行为。
选项 | 默认值 | 描述 |
---|---|---|
timeout | responseTimeout | 在超时之前等待cy.fixture() 解析的时间 |
生成结果
cy.fixture()
返回文件的内容。格式由其文件扩展名决定。- 如果磁盘上的内容发生变化,返回的主题不会更新。
示例
JSON
加载users.json
fixture
cy.fixture('users.json').as('usersData')
省略fixture文件的扩展名
当没有扩展名传递给cy.fixture()
时,Cypress将在fixturesFolder
(默认为cypress/fixtures
)中搜索具有指定名称的文件,并解析第一个找到的文件。
cy.fixture('admin').as('adminJSON')
上面的示例将按以下顺序解析:
cypress/fixtures/admin.json
cypress/fixtures/admin.js
cypress/fixtures/admin.coffee
cypress/fixtures/admin.html
cypress/fixtures/admin.txt
cypress/fixtures/admin.csv
cypress/fixtures/admin.png
cypress/fixtures/admin.jpg
cypress/fixtures/admin.jpeg
cypress/fixtures/admin.gif
cypress/fixtures/admin.tif
cypress/fixtures/admin.tiff
cypress/fixtures/admin.zip
使用import语句
如果您正在加载JSON fixture,可以简单地使用import
语句并让打包器加载它:
// cypress/e2e/spec.cy.js
import user from '../fixtures/user.json'
it('loads the same object', () => {
cy.fixture('user').then((userFixture) => {
expect(user, 'the same data').to.deep.equal(userFixture)
})
})
图片
图片fixture默认以base64
格式发送
cy.fixture('images/logo.png').then((logo) => {
// logo将以base64编码
// 看起来像这样:
// aIJKnwxydrB10NVWqhlmmC+ZiWs7otHotSAAAOw==...
})
更改图片fixture的编码
cy.fixture('images/logo.png', null).then((logo) => {
// logo将作为缓冲区读取
// 看起来像这样:
// Buffer([0, 0, ...])
expect(Cypress.Buffer.isBuffer(logo)).to.be.true
})
播放MP3文件
cy.fixture('audio/sound.mp3', 'base64').then((mp3) => {
const uri = 'data:audio/mp3;base64,' + mp3
const audio = new Audio(uri)
audio.play()
})
访问Fixture数据
使用.then()
访问fixture数据
cy.fixture('users').then((json) => {
cy.intercept('GET', '/users/**', json)
})
使用fixture引导数据
在使用前修改fixture数据
您可以直接修改fixture数据,然后访问URL或挂载一个向该URL发出网络请求的组件。
- 端到端测试
- 组件测试
cy.fixture('user').then((user) => {
user.firstName = 'Jane'
cy.intercept('GET', '/users/1', user).as('getUser')
})
cy.visit('/users')
cy.wait('@getUser').then(({ request }) => {
expect(request.body.firstName).to.eq('Jane')
})
cy.fixture('user').then((user) => {
user.firstName = 'Jane'
cy.intercept('GET', '/users/1', user).as('getUser')
})
cy.mount(<Users />)
cy.wait('@getUser').then(({ request }) => {
expect(request.body.firstName).to.eq('Jane')
})
注意事项
快捷方式
使用fixture
StaticResponse
属性
Fixture也可以通过直接在cy.intercept()
的StaticResponse
对象上使用特殊属性fixture
来引用,而无需使用.fixture()
命令。
cy.intercept('GET', '/users/**', { fixture: 'users' })
验证
自动文件验证
Cypress会自动验证您的fixture。如果您的.json
、.js
或.coffee
文件包含语法错误,它们将显示在命令日志中。
编码
默认编码
Cypress自动确定以下文件类型的编码:
.json
.js
.coffee
.html
.txt
.csv
.png
.jpg
.jpeg
.gif
.tif
.tiff
.zip
对于其他类型的文件,默认情况下将作为utf8
读取,除非在cy.fixture()
的第二个参数中指定。您可以指定null
作为编码,以便将文件作为Cypress.Buffer
实例读取。
this
上下文
如果您使用this
测试上下文对象存储和访问fixture数据,请确保使用function () { ... }
回调。否则,测试引擎将不会将this
指向测试上下文。
describe('User page', () => {
beforeEach(function () {
// "this"指向测试上下文对象
cy.fixture('user').then((user) => {
// "this"仍然是测试上下文对象
this.user = user
})
})
// 测试回调采用"function () { ... }"形式
it('has user', function () {
// this.user存在
expect(this.user.firstName).to.equal('Jane')
})
})
仅加载一次
请注意,fixture文件在测试期间假定为不变,因此Cypress仅加载它们一次。即使您覆盖fixture文件本身,已加载的fixture数据仍保持不变。
如果您希望在测试期间动态更改文件内容,请考虑使用cy.readFile()
。
例如,如果您想用不同的对象回复网络请求,以下不起作用:
// 🚨 不起作用
cy.intercept('GET', '/todos/1', { fixture: 'todo' }).as('todo')
// 应用程序请求/todos/1资源
// 拦截器用todo.json文件中的对象回复
cy.wait('@todo').then(() => {
cy.writeFile('/cypress/fixtures/todo.json', { title: 'New data' })
})
// 应用程序再次请求/todos/1资源
// 拦截器用最初加载的todo.json文件中的对象回复
// 而不是{ "title": "New data" }
在这种情况下,避免使用fixture文件,而是用对象回复网络请求:
// ✅ 用对象回复
cy.fixture('todo.json').then((todo) => {
cy.intercept('GET', '/todos/1', { body: todo }).as('todo')
// 应用程序请求/todos/1资源
// 拦截器用初始对象回复
cy.wait('@todo').then(() => {
// 修改响应对象
todo.title = 'New data'
// 并覆盖拦截器
cy.intercept('GET', '/todos/1', { body: todo })
})
})
规则
要求
cy.fixture()
需要链接到cy
。
断言
cy.fixture()
只会运行您链接的断言一次,并且不会重试。
超时设置
cy.fixture()
不应该超时。
因为cy.fixture()
是异步的,所以在技术上与内部Cypress自动化API通信时可能会发生超时。但出于实际目的,这不应该发生。
命令日志
cy.fixture()
不在命令日志中记录
另请参阅
- 指南:变量和别名
cy.intercept()
.then()
.readFile()
用于类似的命令,但没有缓存并具有内置的重试能力- 配方:引导应用程序测试数据