@ cypress/ snapshot (英语)
向 Cypress 测试跑车添加值/ 对象/ DOM 元素快照测试支持
说明
请查看 Cypress 其他几个快照插件 : cypress-plugin-snapshots (英语), (中文(简体) ), cypress-image-snapshot (简体中文)。 。 。 .
安装
要求数 节点 6或以上版本.
npm install --save-dev @cypress/snapshot
使用
安装后,在您的 cypress/support/commands.js 文件
require('@cypress/snapshot').register()
此注册新命令以创建新快照或比较旧快照的值
describe('my tests', () => {
it('works', () => {
cy.log('first snapshot')
cy.wrap({ foo: 42 }).snapshot()
cy.log('second snapshot')
cy.wrap({ bar: 101 }).snapshot()
})
})
describe('focused input field', () => {
it('is empty and then typed into', () => {
cy.visit('http://todomvc.com/examples/react/')
cy
.focused()
.snapshot('initial')
.type('eat healthy breakfast')
.snapshot('after typing')
})
})
默认情况下, 快照对象可以在文件中找到 snapshots.js。在上述情况下,它会看起来像这样
module.exports = {
"focused input field": {
"is empty and then typed into": {
"initial": {
"tagName": "input",
"attributes": {
"class": "new-todo",
"placeholder": "What needs to be done?",
"value": ""
}
},
"after typing": {
"tagName": "input",
"attributes": {
"class": "new-todo",
"placeholder": "What needs to be done?",
"value": "eat healthy breakfast"
}
}
}
},
"my tests": {
"works": {
"1": {
"foo": 42
},
"2": {
"bar": 101
}
}
}
}
如果您更改站点值, 保存的快照将不再匹配, 丢出错误

点击 SNAPSHOT 在命令日志中步骤可看到DevTools中打印的预期值和当前值.
选项
你可以通过几个选项来控制快照比较和行为.
cy.get(...).snapshot({
name: 'human snapshot name', // to use in the snapshot file
json: false // convert DOM elements into JSON
// when storing in the snapshot file
})
配置
此模块提供了一些配置选项 :
使用RelativeSnapshots (缩写)
设定为真, 以存储您每次测试运行时的快照 而不是基础工作目录.
说明: 要求 readFileMaybe 要配置的插件 https://on.cypress.io/task#Read-a-file- That-might-no-except 互联网档案馆的存檔,存档日期2013-12-11
快取文件Name
设定为指定您快照的字符串
使用量
设定配置选项为 Cypress 配置的一部分 . 见 https://docs.cypress.io/ guides/references/configulation.html 页面存档备份,存于互联网档案馆
调试
用环境变量调试此模块 DEBUG=@cypress/snapshot
小印数
作者:格莱布·巴赫穆托夫 <[email protected]> © Cypress.io 2017
授权:麻省理工学院 - 做任何与代码有关的事情,但如果无效就不要责备你.
支持: 如果您发现此模块有任何问题, 电子邮件 / 微博 / 未决问题 在基图布
MIT 许可证
版权 (c) 2017 Cypress.io <[email protected]>
特此免费准许任何人 获取该软件和相关文件的副本 文件(“软件”),在软件中处理,不包含 限制,包括不限制使用权, 复制、修改、合并、出版、发行、分版和/或销售 软件的复制本,并允许向 为此提供了软件,但须符合下列条件: 条件:
上述版权通知和本许可通知应为: 列入软件的所有副本或大部分.
软件是提供的"是", 没有任何种类的恐惧, 或监禁,包括但不限制于 特别目的和目的的证明 无所不入。 绝对不能让警察或警察 B. 任何索赔、救济或其他负债的持有者, 条约、条约或其他条约行动 来源、来源或与溶剂或使用有关的 其他交易 在软件.