There's some async stuff going on here, since cy.exec
spawns a child process.
Normally you would await the async call, but Cypress commands only return chainers (they don't really follow the promise pattern), so you will need to add a Promise into the mix and await it.
Helper
module.exports = function() {
this.returnStatus = "FAILURE";
this.SuccessMsg = function() {
return new Cypress.Promise(resolve => {
cy.exec('echo hello', { log: true, failOnNonZeroExit: false }).then((output) => {
this.returnValue = "SUCCESS";
resolve(this.returnValue)
});
})
}
}
Test
let Status = require('../support/returnStatus');
let helper = new Status();
describe("Check return value", function(){
it("should check latest value", async function(){ // NOTE async test
let reply = await helper.SuccessMsg();
console.log('reply', reply); // now it prints /*SUCCESS*/
})
});
If you turned your helper function into a custom command, likely the test will not need to be made async since Cypress automatically waits for promises to resolve.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…