使用Nightwatch访问iFrame元素

Sim*_*egg 8 javascript css iframe dom nightwatch.js

我正在使用Nightwatch来测试diviframe中是否给出了正确的值.

我的HTML;

<div class="o-ads__inner" id="leaderboard-gpt">
  <div id="google_ads_iframe">
    <iframe id="some_id">
      #document
        <html>
          <head></head>
          <body>
            <div id="ad-data" creative-id="53134803289">
              <!-- All of the stuff -->
            </div>
          </body>
        </html>
    </iframe>
  </div>
  <iframe><!-- Another iframe (I don't want) --></iframe>
</div>
Run Code Online (Sandbox Code Playgroud)

这是我的夜班测试;

module.exports = {
  'Leaderboard Visibility' : function (client) {
    client
    .url(some_url)
    .waitForElementVisible('body', 5000)
    .waitForElementPresent('#leaderboard > iframe', 10000)
    .pause(5000)
    .frame(0)
      .pause(5000)
      .waitForElementPresent('div#ad-data', 5000)
      .assert.attributeContains('div#ad-data', 'creative-id', '53134803289')
    .end();
  }
};
Run Code Online (Sandbox Code Playgroud)

我从Nightwatch得到的错误是Timed out while waiting for element <div#ad-data> to be present for 5000 milliseconds.我知道它在那里通过在失败的线之前检查.

我已经.pause(5000)提到了2 秒,因为类似的问题表明,iframe中的内容加载速度不够快可能会出现问题.由于我已经完成了一些调试,我认为这不是这种情况,但我暂时将它们留在那里.

我在想我无法访问,div#ad-data因为iframe拥有自己的contentDocument属性,其中包含head&body(以及随后的divs).

如何使用Nightwatch div在iframe中声明属性的值contentDocument

Kil*_*ati 7

为您的第一个iframe <iframe id="some_id">使用.frame('some_id').


Tar*_*ngh 6

动态ID外部iframe访问

这是一种测试解决此问题的方法,获取属性ID并在框架中使用它,frame(0)不提供访问权限,守夜需要iframe ID。对于动态生成的ID,您可以执行以下操作。

module.exports = {
      'Checkout Braintree' : function (client) {
        client
        .url('#enter_your_url_here')
        .waitForElementPresent('[data-e2e="brainTree3Ds"]', 10000)
        .pause(5000)
        .perform(() => {
          client.getAttribute('[data-e2e="brainTree3Ds"] iframe', 'id',(result) => {
            client
              .frame(result.value)
              .setValue('input[name="external.field.password"]', 1234)
              .click('input[type="submit"]');
          });
        })
        .testSuccessPage() // External Command
        .end();
    }
Run Code Online (Sandbox Code Playgroud)