Reputation: 8948
Whenever I try to run an automated test script written using JavaScript with Protractor, I can see that these two are actually ran in parallel independently of each other. Example:
it("Validation of ND account", function() {
// I logged in, and navigated to the page I need
// And this is where is gets intresting
console.log("\n");
console.log("Before getting number of users");
var numberOfUsers = element(by.xpath('//div[@viewid="userList"]//div[@class="results-count ng-binding"]')).getText().then(function(text) {
console.log(text);
});
console.log("After getting number of users");
// for (i=1, i<numberOfUsers, i++) {
// console.log(i);
// }
});
I assume that I get my logs in the same order - before, number and after, but first I get JS, and then Protractor (because it takes longer to load). This is a result of running this script in console output:
Started
Before getting number of users
After getting number of users
161
With that said, my problem is that if I want to open a page, get an element text and then perform some operations with it(run a FOR loop which is commented out), it won't let do this because it will return an unresolved promise before it even load the page. More precisely what it does is starts opening a page, right away before the page is loaded, it will run that loop, which depends on element from the page. The loop fails because the element isn't appeared yet, and the program still doesn't have its text attribute. So here is the question: is it possible to strictly adhere to the script sequence (to not let JS run scripts written after protractor commands before execution of protractor commands are completed) without JS timeouts or wait functions?
Upvotes: 1
Views: 409
Reputation: 18799
You need to understand Promises and work with the callbacks to make it work sequentially.
For this particular case, you can't wait for numberOfUsers
for your test to keep going, you'll have to continue inside the callback function:
console.log("\n");
console.log("Before getting number of users");
element(by.xpath('//div[@viewid="userList"]//div[@class="results-count ng-binding"]')).getText().then(function(text) {
console.log(text);
// continue working here with "text"
console.log("After getting number of users");
});
Upvotes: 1