【问题标题】:Using `within` in custom helpers在自定义助手中使用 `within`
【发布时间】:2021-01-26 13:58:56
【问题描述】:

我正在使用CodeceptJS,我正在尝试编写一个custom helper,它断言一个文本并单击“确定”。此对话框以 iframe 模式弹出以同意 cookie。

如果我在我的场景中编写以下步骤

I.amOnPage('/some-path');
within({frame: '#iframeID'}, () => {
  I.see('Headline text for dialog');
  I.click('OK');
});
// ...

...我的测试似乎运行良好。

但是当我用它制作一个自定义助手并正确配置它以便我可以使用它时:

const { Helper } = codeceptjs;

class CookieConsent extends Helper {

  consentWithCookies() {
    const { Puppeteer } = this.helpers;
    within({frame: '#iframeID'}, () => {
      Puppeteer.see('Headline text for dialog');
      Puppeteer.click('OK');
    });
  }

}

module.exports = CookieConsent;

...并将其用作一个步骤:

I.amOnPage('/some-path');
I.consentWithCookies();
// ...

...它似乎不起作用,因为同意对话框没有像直接在场景中实现它时那样被点击。根据一些console.log() 调试,内部回调根本不会被调用。控制台不会抛出任何关于 undefined within 或任何可疑的错误。

我怀疑在自定义帮助程序中使用 within 不起作用,或者我做错了我无法从文档中弄清楚的事情。

documentation 的此警告并没有真正阐明何时错误地使用了 inside,而使用 await 并不能解决问题。

within 使用不当可能会导致问题。如果您看到测试的奇怪行为,请尝试将其重构为不在内部使用。建议尽可能保持在最简单的情况下。由于 inside 返回一个 Promise,因此即使您不打算使用返回值,也可能需要等待结果。

【问题讨论】:

    标签: iframe codeceptjs


    【解决方案1】:

    当涉及到自动化时,如果没有 iFrame,工作可能会很痛苦。有许多因素会导致 iFrame 无法访问框架,例如跨域 iFrame,通常用于提高所服务内容的安全性。

    现在要解决您的问题,您所要做的就是在 CodeceptJS 中使用 switchTo() - Docs,这是一个可供所有可用助手使用的功能。顺序应该是

    I.switchTo('your iframe');
    ..... some actions here;
    I.switchTo(); // You do this so that you get out of the iFrame context when done
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-10
      • 2019-11-07
      • 2016-01-29
      • 2013-06-27
      • 1970-01-01
      • 1970-01-01
      • 2020-04-18
      相关资源
      最近更新 更多