Is there a way to capture errors occurring in the DOM
in Selenium
and probably flag the same as an error in the page?
To give a brief exam
If you're using java, you're welcome to try this library comitted by me which allows to easily collect JS errors received in Chromedriver session, using annotations on test methods. It works on on JUnit5 with extended annotation, and on TestNG with a listener parsing the annotation. The annotation contains boolean values which let you decide whether you want to assert or log the found errors after test execution.
JUnit5 example:
@Test
@JSErrorsCollectorJUnit
void referenceErrorTest(TestInfo testInfo) throws InterruptedException {
// Create a new instance of ChromeDriver.
driver = new ChromeDriver();
// Set your test name to point its ChromeDriver session in HashMap.
JSErrorsDriverHolder.setDriverForTest(testInfo.getDisplayName(), driver);
// Navigate to URL.
driver.get("http://testjs.site88.net");
// The click on the button in the test site should cause JS reference error.
driver.findElement(By.name("testClickButton")).click();
waitBeforeClosingBrowser();
}