Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing iFrame elements using Nightwatch

I am using Nightwatch to test that the correct value has been given to a div within an iframe.

My 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>

This is my nightwatch test;

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();
  }
};

The error I get from Nightwatch is Timed out while waiting for element <div#ad-data> to be present for 5000 milliseconds. I know that it is there by checking before the failing line.

I have put in the 2 .pause(5000)s as similar questions suggest it may be a problem that the contents in the iframe isn't loading quickly enough. I don't think that's the case here due to some debugging I have done but I have left them there for the time being.

I'm thinking that I can't access the div#ad-data because the iframe has it's own contentDocument property, which contains the head & body (and thus subsequent divs).

How can I assert the value of the div's property within the iframe's contentDocument using Nightwatch?

like image 341
Simon Legg Avatar asked Dec 16 '15 16:12

Simon Legg


People also ask

What is Nightwatch JSON?

json is a configuration file which tells the Nightwatch tool where to find the stand-alone Selenium binary, browser drivers, location of the test, environment profile, and more. The package.json contains the metadata and node package dependencies for the project. The ci/functional-test.

What is Nightwatch API?

What is Nightwatch API? Nightwatch API is JavaScript (Node. js) programming interface for controlling Nightwatch. js which is an End-to-End (E2E) testing solution for browser based apps and websites.


2 Answers

for your first iframe <iframe id="some_id"> use .frame('some_id').

like image 79
Killuminati Avatar answered Sep 24 '22 08:09

Killuminati


Dynamic id external iframe access

Here is a way around to test solve this issue, get the attribute id and use it in the frame, frame(0) does not provide access, nightwatch need iframe id. and for dynamic generated id's here is what you can do.

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();
    }
like image 35
Tarandeep Singh Avatar answered Sep 20 '22 08:09

Tarandeep Singh