admin管理员组文章数量:1327963
I am switching from Protractor to Nightwatch.js, and I am facing some difficulties with the way Nightwatch handles the promises.
To give an example, I am trying to count the number of element satisfying a given criterion. The count function is in a page object, so separated from the test itself:
The page object mand:
countToDoList: function(browser) {
browser.elements('css selector', "input[ng-model='todo.done']", function(result){
return result.value.length;
});
}
and the call in the test:
'Angular - 1' : function(browser) {
var angular = browser.page.angularPO();
var mainPage = angular.section.main;
angular.openMainPage(browser);
var countToDoBoxes = 0;
countToDoBoxes = mainPage.countToDoList(browser);
console.log("countToDoBoxes = " + countToDoBoxes);
browser.end();
}
This returns "countToDoBoxes = undefined". And based on my (short) experience with Protractor, I assume that this is due to the function not fulfilling the promise, when it's passed to "countToDoBoxes".
Is there a way to make this work?
I am switching from Protractor to Nightwatch.js, and I am facing some difficulties with the way Nightwatch handles the promises.
To give an example, I am trying to count the number of element satisfying a given criterion. The count function is in a page object, so separated from the test itself:
The page object mand:
countToDoList: function(browser) {
browser.elements('css selector', "input[ng-model='todo.done']", function(result){
return result.value.length;
});
}
and the call in the test:
'Angular - 1' : function(browser) {
var angular = browser.page.angularPO();
var mainPage = angular.section.main;
angular.openMainPage(browser);
var countToDoBoxes = 0;
countToDoBoxes = mainPage.countToDoList(browser);
console.log("countToDoBoxes = " + countToDoBoxes);
browser.end();
}
This returns "countToDoBoxes = undefined". And based on my (short) experience with Protractor, I assume that this is due to the function not fulfilling the promise, when it's passed to "countToDoBoxes".
Is there a way to make this work?
Share Improve this question edited Nov 14, 2015 at 1:44 eppineda 6873 silver badges20 bronze badges asked Nov 9, 2015 at 11:02 MajujMajuj 1791 silver badge12 bronze badges 3-
1
If you expect
browser.elements
to return a promise, then you should probably use.then()
instead of passing the callback directly. Also, you'd obviously have toreturn
from thecountToDoList
method. – Bergi Commented Nov 14, 2015 at 14:30 - Just for my own interest, why are you switching? – SuperUberDuper Commented Dec 7, 2015 at 16:11
- @SuperUberDuper : I work on a different project, and this new one works with Nightwatch.js (not my choice, and can't change that) – Majuj Commented Dec 7, 2015 at 17:29
2 Answers
Reset to default 6I couldn't find the API description for elements
, however here is your code, modified to return a Bluebird promise. (You could do something similar with the Promise library of your choice.)
countToDoList: function(browser) {
return new Promise(function(resolve, reject) {
browser.elements('css selector', "input[ng-model='todo.done']", function(result){
resolve(result.value.length);
});
});
}
I'm not going to answer the question you have, instead, I'll show a solution to the problem you have. I think this will help Googlers who've e here trying to use promises with Nightwatch as well.
Firstly, Nightwatch doesn't have a promise-based API, it uses callbacks. If you try to use promises with Nightwatch it'll be painful because you're moving against its programming paradigm.
So what I think should do is, firstly, modify the countToDoList
function so it receives a callback function. Call it with the value you've derived.
It's also important that you return browser
so we can chain something after this.
countToDoList: function(browser, callback) {
return browser.elements('css selector', "input[ng-model='todo.done']", function(result){
callback(result.value.length);
});
}
Now, in your test, use the callback in countToDoList
to assign a value to countToDoBoxes
. You'll then need to chain the perform
method. This method ensures that whatever you put in its callback is added to the end of Nightwatch's mand queue. Now you can be sure you'll have the value of countToDoBoxes
available when you try to log it.
'Angular - 1' : function(browser) {
var angular = browser.page.angularPO();
var mainPage = angular.section.main;
angular.openMainPage(browser);
let countToDoBoxes;
mainPage
.countToDoList(browser, function(count) {
countToDoBoxes = count;
})
.perform(function() {
console.log("countToDoBoxes = " + countToDoBoxes);
})
.end();
}
By the way, this is all explained in the article Understanding the Command Queue which for some unknown reason is not directly in the Nightwatch docs where I think it belongs.
本文标签: javascriptHow to fulfill a promise with NightwatchStack Overflow
版权声明:本文标题:javascript - How to fulfill a promise with Nightwatch - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742251291a2440808.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论