- Cypress 教程
- Cypress - 首页
- Cypress - 简介
- Cypress - 架构和设置环境
- Cypress - 测试运行器
- Cypress - 构建第一个测试
- Cypress - 支持的浏览器
- Cypress - 基本命令
- Cypress - 变量
- Cypress - 别名
- Cypress - 定位符
- Cypress - 断言
- Cypress - 文本验证
- Cypress - 异步行为
- Cypress - 使用 XHR
- Cypress - jQuery
- Cypress - 复选框
- Cypress - 选项卡
- Cypress - 下拉列表
- Cypress - 警报
- Cypress - 子窗口
- Cypress - 隐藏的元素
- Cypress - 框架
- Cypress - Web 表格
- Cypress - 鼠标操作
- Cypress - Cookie
- Cypress - GET 和 Post
- Cypress - 文件上传
- Cypress - 数据驱动测试
- Cypress - 提示弹出窗口
- Cypress - 仪表板
- Cypress - 屏幕截图和视频
- Cypress - 调试
- Cypress - 自定义命令
- Cypress - 装置
- Cypress - 环境变量
- Cypress - 挂钩
- Cypress - JSON 文件设置
- Cypress - 报告
- Cypress - 插件
- Cypress - GitHub
- Cypress 有用资源
- Cypress - 快速指南
- Cypress - 有用资源
- Cypress - 讨论
Cypress - 变量
在 Cypress 中,使用 var、let 和 const 等变量。在使用闭包时,我们可以使用在没有赋值的情况下获取的对象。但是,在我们使用可变对象时,情况并非如此。
当对象修改其特征时,我们可能需要将它的旧值与它的新值进行比较。
代码实现
我们可以通过使用以下命令来进行代码实现 −
cy.get('.btn').then(($span) => {
// value capture before button click and stored in const
const n = parseInt($span.text())
cy.get('b').click().then(() => {
// value capture after button click and stored in const
const m = parseInt($span.text())
// comparison
expect(n).to.eq(m)
})
})
在以上情况下,我们使用 const 变量,因为对象 $span 正在发生变化。在处理可变对象及其值时,建议使用 const 类型的变量。
广告