我正在使用赛普拉斯(https://www.cypress.io/ 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()
的。因此语法将如下所示:
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)
})
})
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)