【问题标题】:How to return a value from custom function in Cypress?如何从赛普拉斯的自定义函数中返回一个值?
【发布时间】:2022-02-08 21:38:48
【问题描述】:

由于我正在处理一个承诺,我一直在努力从我的自定义函数中返回一个值。

这是我的代码:

这是我的自定义函数:

Cypress.Commands.add("myFunction", () => {
   cy.get('#someID').then($container) => {
      const isHidden = $container().children('div:nth-child(3)').is(':hidden');
      console.log(isHidden); // This returns either true or false and that is good  
      return isHidden; // this returns $chainer but I want to return either true or false 
   }

});

这是我的测试套件:

context('some description', () => {
  before(function(){
      const result = cy.myFunction();
      console.log(result); // This is $chainer, but I want to get the value of true or false from isHidden variable 
  });

});

【问题讨论】:

标签: javascript promise cypress


【解决方案1】:

我正在使用cy.wrap(请参阅https://docs.cypress.io/api/commands/wrap/#Syntax)执行此操作,它返回一个 Cypress.Chainable,允许您使用then 处理结果。

Cypress.Commands.add('myFunction', () => {
    return cy.wrap('myResult');
})

cy.myFunction.then((text) => {
    console.log(text); // myResult
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多