I have a page with custom window.addEventListener("error", ...)
logic. I'd like to test that it works, and the only real way to do so is to throw an error.
I tried this:
await browser.executeScript("throw new Error();");
...but Selenium itself saw the error and considered it an exception. Fine.
JavascriptError: Error
at Object.throwDecodedError (node_modules/selenium-webdriver/lib/error.js:550:15)
at parseHttpResponse (node_modules/selenium-webdriver/lib/http.js:542:13)
at Executor.execute (node_modules/selenium-webdriver/lib/http.js:468:26)
Next, tried doing that asynchronously:
await browser.executeScript(() => {
setTimeout(() => { throw new Error(); }, 1);
});
...but the page itself didn't notice the error (and yes, I've validated that throwing one in the dev tools triggers the expected handling).
Next, I tried using an async script that schedules an error and then the callback:
await browser.executeAsyncScript((callback) => {
setTimeout(
() => {
setTimeout(callback, 1);
throw new Error();
},
1);
});
...but the page itself still didn't notice the error.
Am I missing something here?
You can easily simulate that using below script
var errorElem = document.createElement("script");
errorElem.textContent = 'throw new Error("simulated error");'
document.body.append(errorElem);
This should be able to simulate the event