比较赛普拉斯中的两个会话 ID



我是自动化和编码的新手,我想用以下步骤比较两个会话ID值:

  1. 登录后立即获取第一个值
  2. 刷新页面
  3. 获取第二个值并进行断言

为了简化事情,我制作了一个自定义命令:

Cypress.Commands.add('getSessionId', () => {
let sessionId
cy.getCookie('development')
.its('value').then(($value) => {
sessionId = String($value)
})    
})

我希望测试脚本看起来像这样:

let firstSessionId = cy.getSessionId()
cy.reload()
let secondSessionId = cy.getSessionId()
expect(firstSessionId).to.eq(secondSessionId)

这有两个问题:

  1. 在这种情况下,我无法将值作为字符串访问
  2. expect在获得ID之前运行(我想是因为柏树的异步性?(

如果有任何提示我做错了什么,我将不胜感激。感谢

这是执行测试的最简单方法,在这种情况下不需要自定义命令。

cy.getCookie('development').its('value')
.then(sessionId1 => {
cy.reload()
cy.getCookie('development').its('value')
.then(sessionId2 => {
expect(sessionId1).to.eq(sessionId2)
})
})

如果您出于其他原因想要自定义命令,

Cypress.Commands.add('getSessionId', () => {
cy.getCookie('development').its('value')    // last command is returned
})
cy.getSessionId().then(sessionId1 => {
cy.reload()
cy.getSessionId().then(sessionId2 => {
expect(sessionId1).to.eq(sessionId2)
})
})

您可以从自定义命令返回值,如下所示:

Cypress.Commands.add('getSessionId', () => {
cy.getCookie('development')
.its('value')
.then((val) => {
return cy.wrap(val)
})
})

然后在你的测试中,你可以这样做:

//Get First session Id
cy.getSessionId.then((sessionId1) => {
cy.wrap(sessionId1).as('sessionId1')
})
//Refresh Page
cy.reload()
//Get Second session Id
cy.getSessionId.then((sessionId2) => {
cy.wrap(sessionId2).as('sessionId2')
})
//Assert both
cy.get('@sessionId1').then((sessionId1) => {
cy.get('@sessionId2').then((sessionId2) => {
expect(sessionId1).to.eq(sessionId2)
})
})

最新更新