dont assume new target is a new page

This commit is contained in:
Bruno Windels 2018-08-23 10:03:37 +02:00
parent f49b85897d
commit 6be5975050

View file

@ -164,17 +164,21 @@ module.exports = class RiotSession {
waitForNewPage(timeout = 5000) {
return new Promise((resolve, reject) => {
const timeoutHandle = setTimeout(() => {
this.browser.removeEventListener('targetcreated', callback);
this.browser.removeListener('targetcreated', callback);
reject(new Error(`timeout of ${timeout}ms for waitForNewPage elapsed`));
}, timeout);
const callback = async (target) => {
if (target.type() !== 'page') {
return;
}
this.browser.removeListener('targetcreated', callback);
clearTimeout(timeoutHandle);
const page = await target.page();
resolve(page);
};
this.browser.once('targetcreated', callback);
this.browser.on('targetcreated', callback);
});
}