Skip to main content

Console Error Collector

ConsoleErrorCollector captures JavaScript console errors during test execution. Use it to catch silent JS failures that don't cause visible test failures.


Configuration​

testfly.yml
browser:
captureConsoleErrors: true # collect JS errors (default: false)
failOnConsoleErrors: false # fail test on any JS error (default: false)

Manual collection in a test​

import io.testfly.browser.ConsoleErrorCollector;

@Test
public void noJsErrorsOnLogin() {
open("/login");
ConsoleErrorCollector.injectShim(); // start capturing

type(By.id("username"), "admin");
type(By.id("password"), "secret");
click(By.id("submit"));

List<String> errors = ConsoleErrorCollector.getErrors();
softAssert().that(errors.isEmpty(), "Unexpected JS errors: " + errors);
}

Clear between interactions​

Isolate which action produced errors:

open("/checkout");
ConsoleErrorCollector.injectShim();

click(By.id("add-to-cart"));
List<String> addErrors = ConsoleErrorCollector.getErrors();

ConsoleErrorCollector.clear(); // reset before next action

click(By.id("proceed-to-payment"));
List<String> paymentErrors = ConsoleErrorCollector.getErrors();

Chrome vs Firefox​

BrowserMethodNotes
ChromeWebDriver browser logsWorks automatically, no shim needed
FirefoxJS console shimCall injectShim() after page load

Chrome collects errors automatically via LogType.BROWSER. Firefox requires the JS shim because it does not expose browser logs through WebDriver.


API​

ConsoleErrorCollector.injectShim()​

Injects a JavaScript shim that intercepts console.error calls. Call after page load on Firefox or for cross-browser consistency.

ConsoleErrorCollector.collect()​

Returns all JS errors captured since the last clear(). Reads from WebDriver logs (Chrome) or the JS shim buffer.

ConsoleErrorCollector.getErrors()​

Alias for collect().

ConsoleErrorCollector.clear()​

Resets the JS shim error buffer on the current page.

ConsoleErrorCollector.isEnabled()​

Returns true if browser.captureConsoleErrors: true is set in config.