Created
May 18, 2021 23:43
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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