[英]How to store returned value of an Anonymous function to a Variable in JavaScript?
[英]Unable to store value into global variable from anonymous function - Javascript and Protractor
我正在寫量角器腳本。 我的問題是,我無法將值存儲到全局變量中。 既不進入global.somevariable,也不進入browser.params.somevariable。
我有2個文件:1. login.js 2. helper.js
從helper.js文件內部的匿名方法,我試圖將值存儲到全局變量中。 我正在從是PageObject文件的js文件中調用與此helper.js相關的方法。
在config.js中,我聲明了2個變量-一個帶有global關鍵字。 第二個使用onPrepare()方法,因此我可以使用browser.params.someVar。 但是,沒有任何效果。
在該方法內部,可變參數內部的值很好。 但是,當我訪問該helper.js之外的相同變量時,它們為null /不正確。
config.js
exports.config =
{
params:
{
tempVar:false
},
onPrepare:function()
{
global.result=false;
}
};
loginpage.js
var loginPage = function()
{
var un = element(by.id('un'));
var helper = new help();
helper.verifyElemExists(un);
console.log(global.result);//False,though promise returned true
console.log(browser.params.tempVar); // This is also false
if(global.result===true)
{
// Code will do something...
}
}
module.exports = login;
helper.js
var helper = function()
{
verifyElemExists = function(elem)
{
elem.isPresent().then(function(res)
{
browser.params.tempVar=res;
global.result =res;
});
}
module.exports = helper;
您需要了解nodejs執行量角器腳本的過程。 您的問題的根本原因來自以下代碼片段:
helper.verifyElemExists(un);
// when nodejs execute above line, all promises generated in this function
// will be added into a list (you can call the list as protractor control flow),
// after all sync script be executed, the protractor control flow start to execute
// the promise in the list one by one in the order as they are added into list.
console.log(global.result);
console.log(browser.params.tempVar);
if(global.result===true)
{
// Code will do something...
}
// when nodejs execute above lines, because all of them are sync script,
// the execution result of then return immediately, and at the time point they are executed,
// the promise of `helper.verifyElemExists(un);`
// have not start to execute(or not complete execute),
// thus the `global.result` and `browser.params.tempVar` are still with init value: false
從量角器腳本的執行過程中,您可以看到當諾言和同步代碼執行完成時,將為諾言代碼創建諾言列表,但不會為同步代碼創建諾言列表。 然后執行列表中的promise。
要解決您的問題,您可以將上面的同步代碼包裝成一個helper.verifyElemExists(un);
,以便在helper.verifyElemExists(un);
生成的helper.verifyElemExists(un);
之后執行它們helper.verifyElemExists(un);
helper.verifyElemExists(un);
// browser.getTitle() is a promise which be added into promise list after
// helper.verifyElemExists(un);
browser.getTitle().then(function(){
console.log(global.result);
console.log(browser.params.tempVar);
if(global.result===true)
{
// Code will do something...
}
})
// or change the `helper.verifyElemExists()` to return a promise
var helper = function()
{
verifyElemExists = function(elem)
{
return elem.isPresent().then(function(res)
{
browser.params.tempVar=res;
global.result =res;
});
}
module.exports = helper;
helper.verifyElemExists(un).then(function(){
console.log(global.result);
console.log(browser.params.tempVar);
if(global.result===true)
{
// Code will do something...
}
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.