Karan Bhatia
Karan Bhatia

Reputation: 13

How to pass variable value from one function to another in Nightwatch js

I am using Nightwatch.js test automation framework.

In my code I get the value, but I am not being able to pass this recorded value to another function. Here is the code:

CreateLife: function (browser) {

    browser.getValue('#LifeID', function (r) {
        a = r.value;

        browser.execute(function () {
            LifeTemplatesManager.loadAddOnlineItem(a);  //doesn't work/The value of 'a' is not being passed.
        });
        console.log("ID of newly created Life", a);
    }
}

Whenever a new life gets created, it gets assigned a new ID. I am able to get this ID and store it in 'a' and display it in the console. My question is how can I pass the value of 'a' into the function 'LifeTemplatesManager.loadAddOnlineItem'

Help would be appreciated. I am a beginner at JS.

Thanks.

Upvotes: 1

Views: 1956

Answers (1)

Rajat Sharma
Rajat Sharma

Reputation: 385

browser.execute is used to inject a snippet of JavaScript into the page (inside browser environment) for execution, and variable a is not accessible in that environment.

In order to make use of a inside this browser environment, you can do the following:-

  browser.execute(function (a) {
        LifeTemplatesManager.loadAddOnlineItem(a);
    },[a]);

Please refer this link from nightwatch documentation.

Upvotes: 2

Related Questions