Answer the question
In order to leave comments, you need to log in
Promise doesn't work right, huh?
There is a task: to get some data from the page, generate urls with this data, then send requests to the urls and check it in the response.
The following code was made:
function checkSpec() {
return driver.executeScript("var search = []; if (typeof xmlDataSpeclist !== 'undefined') {" +
"$.each(xmlDataSpeclist, function (key, item) {" +
"search.push(" +
"'http://domain/?spec='" + " + item.id" +
");" +
"});" +
"};" +
"return search;"
).then((search) => {
return search.forEach(function(val, i) {
console.log(search.length); //debug printing
return new Promise((resolve, reject) => {
request(search[i], function(error, response, body){
if (error) {
reject(error);
}
resolve(body);
});
}).then((body) => {
console.log(body);
assert.include(body, 'class="someClass"');
});
});
});
}
return checkSpec();
function checkSpec() {
return driver.executeScript("var search = []; if (typeof xmlDataSpeclist !== 'undefined') {" +
"$.each(xmlDataSpeclist, function (key, item) {" +
"search.push(" +
"'http://domain/?spec='" + " + item.id" +
");" +
"});" +
"};" +
"return search;"
).then((search) => {
return Promise.all(search.map(function(val, i) {
/*console.log(search.length);*/
return new Promise((resolve, reject) => {
request(search[i], function(error, response, body){
if (error) {
reject(error);
}
resolve(body);
});
}).then((body) => {
var $ = cheerio.load(body);
var txt = $('locator1').text().replace(/\s+/g," ").trim();
var doc = $(locator2').text().trim();
/*console.log(txt);*/
return assert.notEqual(txt, '', "не найдены: "+ doc);
});
}));
});
}
return checkSpec();
Answer the question
In order to leave comments, you need to log in
You need to wrap the promises returned in the loop in an array and return Promise.all(array) at the end.
This action starts waiting for all the promises in the array to be fulfilled. A promise is also returned, from which you can call
.then(...)
.catch(...)
In general, I suggest paying attention to the innovation: async/await - life becomes much easier with them.
forEach always returns undefined, you need a map
To wait for all promises in the array to complete, use Promise.all
total:
function checkSpec() {
return driver.executeScript("var search = []; if (typeof xmlDataSpeclist !== 'undefined') {" +
"$.each(xmlDataSpeclist, function (key, item) {" +
"search.push(" +
"'http://domain/?spec='" + " + item.id" +
");" +
"});" +
"};" +
"return search;"
).then((search) => {
return Promise.all(search.map(function(val, i) {
console.log(search.length); //debug printing
return new Promise((resolve, reject) => {
request(search[i], function(error, response, body){
if (error) {
reject(error);
}
resolve(body);
});
}).then((body) => {
console.log(body);
assert.include(body, 'class="someClass"');
});
}));
});
}
return checkSpec();
var executeSequence = function(promiseFn, values, acc) {
if (!values.length) return Promise.resolve(acc);
var promise = promiseFn(values.shift());
acc = acc || [];
return promise.then(function(result) {
acc.push(result);
return executeSequence(promiseFn, values, acc);
});
};
var requestUrl = function(url) {
return new Promise((resolve, reject) => {
request(url, function(error, response, body){
if (error) {
reject(error);
}
resolve(body);
});
}
executeSequence(requestUrl, ['http://google.com', ...])
.then(function(response) {
// response это массив из body для каждого из url по порядку
})
.catch(function(reason) {
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question