【问题标题】:returning stuff from custom commands cypress从自定义命令 cypress 返回内容
【发布时间】:2021-08-17 12:48:04
【问题描述】:

我创建了以下函数,但我很难从最后一个 if 块 (elArr[3]) 中获取返回的内容。

如果我删除所有返回语句并在 if 块中说 put: cy.get(el).click() ,它工作正常,但我不想在此函数中使用该命令,而是在测试中执行,而不是像这样:

cy.traverseIFrame(['1', '2', '3'], 'myelement').click()

有人可以帮我解决这个问题,这让我快疯了。

Cypress.Commands.add('traverseIFrame', (elArr, searchEl) => {
 return cy.iframe(elArr[0]).within((res) => {
    if (res.find(searchEl).length > 0) {
      console.log('true1');
      cy.get(searchEl).click(150, 220, {
        force: true
      });
    }
    return cy.iframe(elArr[1]).within((res) => {
      if (res.find(searchEl).length > 0) {
        console.log('true2');
        cy.get(searchEl).click(150, 220, {
          force: true
        });
      }
      return cy.iframe(elArr[2]).within((res) => {
        if (res.find(searchEl).length > 0) {
          console.log('true3');
          cy.get(searchEl).click(150, 220, {
            force: true
          });
        }
        return cy.iframe(elArr[3]).within((res) => {
          if (res.find(searchEl).length > 0) {
            console.log('true4');
            return cy.get(searchEl);
          }
        });
      });
    });
  });
});

【问题讨论】:

    标签: cypress


    【解决方案1】:

    这是因为.within()命令不关注内部return,而是返回之前的主题

    within#Yields

    .within() 产生与上一个命令相同的主题。
    尝试返回不同的元素时 .within 回调无效

    相反,等效项应该是 .then()cy.wrap(...).find(...) 组合,

    Cypress.Commands.add('traverseIFrame', (elArr, searchEl) => {
      return cy.iframe(elArr[0]).then(res => {
    
        // this find sb ok since it's jquery
        if (res.find(searchEl).length > 0) {
          console.log('true1');
    
          // cy.wrap().find() here equates to .within(() => cy.get()
          cy.wrap(res).find(searchEl).click(150, 220, { force: true });    
        }
    
        // this nested cy.iframe may be ok - depends on internals of this command
        return cy.iframe(elArr[1]).then((res) => {
          ...
    
          // inner-most return
          return cy.wrap(res).find(searchEl)
    

    【讨论】:

    • 感谢您抽出宝贵时间回复我会看看是否有效!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    • 2020-12-30
    相关资源
    最近更新 更多