0

I am writing a test script in Nightwatch.js and before I click this element I am grabbing the containing text and picking out the value field and assigning this to an externally assigned variable. The problem I am having is that the value is not being assigned within the callback function

The code I have is as follows:

var meeting=""

    module.exports = {
         'step 1: open event page': function (browser) {
             browser
                .url('http://example.com')
                .waitForElementVisible('.navigation-list-item:nth-of-type(2)', 20000)
                .getText('.navigation-view > .list-container > .navigation-list-item:nth-of-type(2) > a > .list-content > .list-body', function (location) {
                       meeting = location.value;
                })
                .pause(3000)
                .click('.navigation-view > .list-container > .navigation-list-item:nth-of-type(2) > a > .list-content > .list-body')
                .pause(3000)
                .assert.containsText('.ncb__title', meeting);
         }
   }

Any help would be appreciated, Cheers!

NOTE: actual URL of site being tested omitted for privacy reasons

Lampy14
  • 53
  • 1
  • 4
  • Can you share an extract of your HTML ? The second source code is wrong, because "meeting" is declared inside the callback, and then you try to access it outside... – Nicolas Pennec May 22 '15 at 12:43
  • @NicoPennec - see updated issue, original issue has been resolved but another has risen out of this due to changes made – Lampy14 May 22 '15 at 15:34

2 Answers2

0

I don't think your value is not getting assigned first try to print the meeting variable after assigning in callback. Try this

.   .getText('.navigation-view > .list-container > .navigation-list-item:nth-of-type(2) > a > .list-content > .list-body', function (location) {
                   meeting = location.value;
                  console.log(meeting);

            })

And if its getting print then its not the problem , as I am new to JS so don't know the reason but found a workaround. Try this:

.url(function(){
    this.assert.containsText('.ncb__title', meeting)
  })

Hope it would help you.

Juhi Saxena
  • 1,197
  • 11
  • 17
0

The problem here has to do with the fact that Nightwatch runs asynchronously. So, while you are inside the callback where you assign the value of the getText result, Nightwatch has already gone ahead to the next step where you are using it to check the result.

The solution is to use steps to explicitly break this up.

Example:

// Declare your variable as a global at the top of the test
var myTestVar = 'foo';

module.exports = {
    // You don't necessarily have to navigate in an isolated step, but
    // this will ensure that everything else is done after navigation.
    'Step One: Navigate to page': function (browser) {
        browser
          .url('http://example.com')
          .pause(2000);
    },

    'Step Two: Gather information': function (browser) {
        browser.getText('@element', function(result) {
            // Assign the value of the result inside your callback function
            // For this test, we're assuming the text of @element is 'bar'
            myTestVar = result.value;
        });
        // Since nightwatch runs asynchronously, this will print 'foo'
        console.log(myTestVar);
    },

    'Step Three: Print out result': function (browser) {
        // This code will only run after Step Two has completed
        // therefore, this will print 'bar'
        console.log(myTestVar);
    };
John Theodore
  • 91
  • 1
  • 3