cypress - 在多个“it”语句中使用变量来跟踪赛普拉斯的变化
问题描述
我正在使用 Cypress ( https://www.cypress.io/ ) 来测试一个应用程序,该应用程序跟踪在用户限制内完成的 API 调用量。该测试在 API 调用之前和调用之后再次检查限制。测试的目的是查看调用后限制是否发生变化。
限制在屏幕上呈现。我尝试将值存储在一个变量中。在进行 API 调用之后,我想比较之前和之后的值。
已经尝试使用 const 和 let 将其存储在变量中,但两者都不能在 'it' 语句之外工作。
it('should get the limit value before doing an api call', ()=> {
cy.get('.bar__legend')
.contains('used')
.then(($usage) => {
let usageTxt = $usage.text()
let words = usageTxt.split(' ')
let usageBefore = words[0]
})
});
it('should do an API call twice', () => {
// do a API call twice
}
it('should get the limit value after doing an api call', ()=> {
cy.get('.bar__legend')
.contains('used')
.then(($usage) => {
let usageTxt = $usage.text()
let words = usageTxt.split(' ')
let usageAfter = words[0]
cy.log(usageBefore)
cy.log(usageAfter)
})
});
我尝试的另一种方法
it('should increase the limit after an api call', ()=> {
cy.get('.bar__legend')
.contains('used')
.then(($usage) => {
let usageTxt = $usage.text()
let words = usageTxt.split(' ')
let usageBefore = words[0]
})
cy.visit('apilink')
cy.wait(2000)
cy.visit('apilink')
cy.wait(2000)
cy.get('.bar__legend')
.contains('used')
.then(($usage) => {
let usageTxt = $usage.text()
let words = usageTxt.split(' ')
let usageAfter = words[0]
cy.log(usageBefore)
cy.log(usageAfter)
})
})
我希望这两个变量都有一个值,但测试失败,因为“usageBefore”变量不存在。
解决方案
我相信诀窍是在it()
's 之外定义变量。因此语法看起来像这样:
var usageBefore
var usageAfter
describe('description of the test', () =>{
it('This uses the variable', () =>{
cy.get('.bar__legend')
.contains('used')
.then(($usage) => {
let usageTxt = $usage.text()
let words = usageTxt.split(' ')
usageBefore = words[0]
})
})
it('should get the limit value after doing an api call', ()=> {
cy.get('.bar__legend')
.contains('used')
.then(($usage) => {
let usageTxt = $usage.text()
let words = usageTxt.split(' ')
usageAfter = words[0]
cy.log(usageBefore)
cy.log(usageAfter)
})
})
推荐阅读
- python - Scikit-learn:从文件夹加载图像以创建用于 KNN 分类的标记数据集
- excel - 具有 2 个标准的 AVERAGEIFS
- c# - 从代码上传大文件到 shrepoint
- php - Laravel 从数据库中获取数据的问题
- ios - 在 Swift 上有点卡住 MVC 模式
- python - 熊猫向现有数据框添加新行
- sql-server - 由 SQL 代理运行时 SSIS 包失败 - “强制转换规范的字符值无效”
- hibernate - 无法确定类型:java.util.List,表:test_4_config,列:[org.hibernate.mapping.Column(button)
- java - 如何通过 POST 在 Angular / Spring 中注销
- python - pandas dataframe中`.value_counts()`的反向操作是什么?