how to bring global values inside a function in nightwatch.js

  • 5 Replies
  • 5629 Views
var username="abc"

 module.exports={
    'example' : function (browser) {
       browser
.execute(function(result,username){
 alert(username)
})
}
}


the variable "username" is used as a global value how to bring this global value inside the function

am using the tool nightwatch.js

Re: how to bring global values inside a function in nightwatch.js
« Reply #1 on: April 21, 2016, 10:09:05 am »
Hi Antony,

I ran the following test:

Code
var searchText = "rembrandt van rijn'";

module.exports = {
    'Demo test Google': function (browser) {
        browser
            .useXpath() // every selector now must be xpath
            .url('http://www.google.com/ncr')
            .waitForElementVisible('//body', 1000)
            .assert.title('Google')
            .assert.visible("//input[@name='q']")
            .setValue("//input[@name='q']", searchText)
            .waitForElementVisible("//button[@name='btnG']", 1000)
            .click("//button[@name='btnG']")
            .pause(1000)
            .assert.containsText("//div[@id='rso']//h3[@class='r']/*[1]",
                'Rembrandt - Wikipedia')
            .end();
    }
};


And it works fine:

Code
Google Demo Test] Test Suite
=============================

Running:  Demo test Google
 ✔ Element <//body> was visible after 167 milliseconds.
 ✔ Testing if the page title equals "Google".
 ✔ Testing if element <//input[@name='q']> is visible.
 ✔ Element <//button[@name='btnG']> was visible after 31 milliseconds.
 ✔ Testing if element <//div[@id='rso']//h3[@class='r']/*[1]> contains text: "Rembrandt - Wikipedia".

OK. 5 assertions passed. (5.45s)


*

Oren

  • **
  • 59
Re: how to bring global values inside a function in nightwatch.js
« Reply #2 on: April 21, 2016, 10:21:35 am »
Hi,

There shouldn't be a problem using a global variable inside a nightwatch test.

I have run tests similar to what Marat has posted and it works fine for me.

Can you tell us the error you are getting ?

Re: how to bring global values inside a function in nightwatch.js
« Reply #3 on: April 21, 2016, 13:54:22 pm »
hi frndz,

the example which you gave me works well for me also but its not used inside the function the error which am facing is for example
var a=[1,2,3,4,5,6]
 
i cannot bring all the global values of "a" one by one inside the functions

    for(i=0;i<5;i++){
             .getAttribute("xpath/css" , 'checked', function(result){
                    console.log (a(i))
                })
              .getValue("xpath/css" , function(result){
                       console.log (a(i))
                 })
              .getText("xpath/css" , function(result){
                     console.log (a(i))
                 })
          }

while using these functions am un-able to bring all the values of "a" inside the functions.you try these code which i said you will understand well
note: a(i) is used with square bracket but this web page does not support square bracket while posting so i used this bracket for posting
« Last Edit: April 21, 2016, 14:03:53 pm by antony prince »

Re: how to bring global values inside a function in nightwatch.js
« Reply #4 on: April 24, 2016, 12:48:20 pm »
Hi antony,

To get global variables inside a callback function you need to define them in a globals.js file, and include a reference to that file from your nightwatch.json.

set a globals.js file:
Code
var data = {
  arr: [1,2,3,4,5,6],
  i: 1,
  text1: "some text"
};

module.exports = {
data: data
};

include it in nightwatch.json:
Code
....
"page_objects_path" : "",
"globals_path" : "globals.js",

  "selenium" : {
....

Now you can use those variables inside your test suite:
Code
module.exports = {

    'Demo': function (browser) {
        browser
            .useXpath()
            .url('https://jsbin.com/jezofeniqu')
            .waitForElementVisible('//body', 1000);

browser.globals.data.i = 1;
for(i = 1; i <= 5; i++) {
browser
.getAttribute('//*[@id="checkbox' + browser.globals.data.i + '"]', 'checked',
function(result) {
console.log(browser.globals.data.arr[browser.globals.data.i]);
browser.globals.data.i++;
})
}

browser.end();
    }
};

Note that you cannot use the iterator 'i' inside the callback function, since each callback function is executed only after the .getAttribute functions are done.

Re: how to bring global values inside a function in nightwatch.js
« Reply #5 on: April 25, 2016, 07:25:16 am »
ok thank you john

 

Sitemap 1 2 3 4 5 6 7 8