Javascript 如何从cypress框架设置变量和访问权限

Javascript 如何从cypress框架设置变量和访问权限,javascript,node.js,cypress,Javascript,Node.js,Cypress,我正在使用cypress框架测试节点应用程序。我想检查变量是否设置为“成功”。最初设置为“失败”,但从cypress exec设置为“成功”。但当我调用函数时,它仍然是“失败的”。有人能帮我理解我哪里做错了吗 cypress\support\returnStatus.js module.exports = function() { this.returnStatus = "FAILURE"; this.SuccessMsg = function() {

我正在使用cypress框架测试节点应用程序。我想检查变量是否设置为“成功”。最初设置为“失败”,但从cypress exec设置为“成功”。但当我调用函数时,它仍然是“失败的”。有人能帮我理解我哪里做错了吗

cypress\support\returnStatus.js

module.exports = function() {

    this.returnStatus = "FAILURE";

   this.SuccessMsg =  function() {
      cy.exec('echo hello',  { log: true, failOnNonZeroExit: false }).then((output) => {
                    this.returnValue = "SUCCESS";
                  
          });

   
   }
}
let Status = require('../support/returnStatus');
let helper = new Status();

describe("Check return value", function(){
    it("should check latest value", function(){
        let reply =  helper.SuccessMsg();
        console.log(reply);//still it prints /*FAILURE*/
        
    })
});
cypress\integration\checkReturnValue.spec.js

module.exports = function() {

    this.returnStatus = "FAILURE";

   this.SuccessMsg =  function() {
      cy.exec('echo hello',  { log: true, failOnNonZeroExit: false }).then((output) => {
                    this.returnValue = "SUCCESS";
                  
          });

   
   }
}
let Status = require('../support/returnStatus');
let helper = new Status();

describe("Check return value", function(){
    it("should check latest value", function(){
        let reply =  helper.SuccessMsg();
        console.log(reply);//still it prints /*FAILURE*/
        
    })
});

这里有一些异步的东西,因为
cy.exec
生成了一个子进程

通常,您会等待异步调用,但Cypress命令只返回链表(它们并不真正遵循承诺模式),因此您需要在混合中添加承诺并等待它

助手

module.exports=函数(){
this.returnStatus=“失败”;
this.successsg=函数(){
返回新的Cypress.Promise(解析=>{
cy.exec('echo hello',{log:true,failOnNonZeroExit:false})。然后((输出)=>{
this.returnValue=“成功”;
解析(this.returnValue)
});
})
}
}
测试

let Status=require('../support/returnStatus');
让helper=newstatus();
描述(“检查返回值”,函数(){
它(“应检查最新值”,异步函数(){//注意异步测试
let reply=wait helper.successsg();
console.log('reply',reply);//现在打印/*成功*/
})
});

如果将助手函数转换为自定义命令,则可能不需要将测试设置为异步,因为Cypress会自动等待承诺的解决。

这无法从您提供的代码中复制
this.successsg=…
不公开导出上的函数,因此未定义
helper.successsg()
。请发布说明问题的代码。你好@AloysiusParker,谢谢你指出,我已经修改了代码