首页 > 解决方案 > 赛普拉斯:让赛普拉斯检测到您在自定义命令中调用了一个或多个 cy 命令,但返回了不同的值

问题描述

从函数(来自 command.js /Page 对象类)返回任何单个/一组值时,出现错误,因为“赛普拉斯检测到您在自定义命令中调用了一个或多个 cy 命令,但返回了不同的值。”。我搜索了不同的选项,例如“Warp”、“.then”,但没有运气。

以下是详细信息:

我的 Command.js 文件:

Cypress.Commands.add("getconstantvalue", () => {
    const todaysDateTime = Cypress.moment().format('MMMDDYYYYSS')
    cy.log(todaysDateTime)
    return todaysDateTime
})

我想在我的驱动程序/描述西装脚本中使用这个“todaysDateTime”:

describe('The Home Page', function() {
  it('successfully loads', function() { 
    var data = cy.getconstantvalue()
    cy.log(data)       
  })
})

标签: cypress

解决方案


这应该有效。您只需要将它链接到您正在使用的命令。

命令.js 文件

Cypress.Commands.add("getconstantvalue", () => {
    const todaysDateTime = Cypress.moment().format('MMMDDYYYYSS')
    cy.log(todaysDateTime)
    return cy.wrap(todaysDateTime)
})

测试文件

describe('The Home Page', function() {
    it('successfully loads', function() {
        cy.getconstantvalue().then(data => {
            cy.log(data);
        })
    })
})

推荐阅读