Skip to content

Verifying Javascript Errors from browser

bialkos edited this page Mar 15, 2021 · 11 revisions

Note: this function is temporarily not working due to an issue within Selenium: https://github.com/SeleniumHQ/selenium/issues/7900

Our test framework supports Javascript error logging from a browser. In case of any defined Javascript error, the test will fail and log details. To enable Javascript error logging you have to set below flag to trues (default) in App.config file:

<add key="JavaScriptErrorLogging" value="true"/>

You can also define which Javascript errors will fail the test (also configurable from App.config file). Those ones are defined by default:

<add key="JavaScriptErrorTypes" value="SyntaxError,EvalError,ReferenceError,RangeError,TypeError,URIError,Refused to display,Internal Server Error,Cannot read property" />

In case of fail you will see below message:

Message: JavaScript errors found. See the logs for details.>

And details in a log file:

2018-02-05 10:02:34.3895|Error|Ocaramba.DriverContext.LogJavaScriptErrors|JavaScript error(s): http://the-internet.herokuapp.com/javascript_error 6:51 Uncaught TypeError: Cannot read property 'xyz' of undefined>

Javascript errors are check in ProjectTestBase class in AfterTest() method.

        public void AfterTest()
        {
            this.DriverContext.IsTestFailed = TestContext.CurrentContext.Result.Outcome.Status == TestStatus.Failed || !this.driverContext.VerifyMessages.Count.Equals(0);
            var filePaths = this.SaveTestDetailsIfTestFailed(this.driverContext);
            this.SaveAttachmentsToTestContext(filePaths);
            this.LogTest.LogTestEnding(this.driverContext);
            var javaScriptErrors = this.DriverContext.LogJavaScriptErrors();
            if (this.IsVerifyFailedAndClearMessages(this.driverContext) && TestContext.CurrentContext.Result.Outcome.Status != TestStatus.Failed)
            {
                Assert.Fail();
            }

            if (javaScriptErrors)
            {
                Assert.Fail("JavaScript errors found. See the logs for details");
            }
        }
Clone this wiki locally