Skip to content

Instantly share code, notes, and snippets.

@serg987
Created May 18, 2021 23:43
Show Gist options
  • Save serg987/1fc9bc6512f048529cd53fc9499cb802 to your computer and use it in GitHub Desktop.
Save serg987/1fc9bc6512f048529cd53fc9499cb802 to your computer and use it in GitHub Desktop.
Wait till all requests are done (works in Chrome, Edge, Opera, doesn't in Firefox - unable to get logs)
const webdriver = require('selenium-webdriver-3');
const chrome = require('selenium-webdriver-3/chrome');
const Condition = webdriver.Condition;
const Capabilities = webdriver.Capabilities;
const Capability = webdriver.Capability;
const {DriverService} = require('selenium-webdriver-3/remote');
function areAllRequestsLoaded(entries) {
if (!entries) return false;
const networkRequestIds = new Set();
entries.map(e => JSON.parse(e.message).message)
.filter(m => (m.method === 'Network.requestWillBeSent' || m.method === 'Network.loadingFinished'))
.forEach(m => {
if (m.method === 'Network.requestWillBeSent') {
networkRequestIds.add(m.params.requestId);
} else {
networkRequestIds.delete(m.params.requestId);
}
}
);
return networkRequestIds.size === 0;
}
function untilAllRequestsLoaded() {
return new Condition('All requests are still not loaded', (d) => {
return d.manage().logs().get(webdriver.logging.Type.PERFORMANCE).then(e => areAllRequestsLoaded(e));
})
}
const capabilities = new Capabilities()
.set(Capability.BROWSER_NAME, webdriver.Browser.CHROME);
capabilities.setLoggingPrefs({ performance: 'ALL' });
var srv = new DriverService.Builder("C:/Webdrivers/chromedriver.exe").setLoopback(true).build();
let driver = chrome.Driver.createSession(capabilities, srv);
try {
const secToWait = 10;
driver.get('http://www.yahoo.com');
driver.wait(untilAllRequestsLoaded(),
secToWait * 1000,
`Warning! Requests are not loaded in ${secToWait} secs`);
} finally {
driver.quit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment