bloub
bloub

Reputation: 610

How to capture java script errors using Selenium Java WebDriver

I am trying to use Java Selenium WebDriver capture all javascripts errors of a webpage.

Here a sample of my code :

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.logging.LogType;

public class MainExample {
    public static void main(String[] args) {
        System.setProperty("webdriver.gecko.driver", "path_to_driver/geckodriver");
        FirefoxOptions options = new FirefoxOptions();
        WebDriver driver = new FirefoxDriver(options);
        driver.get("https://www.google.com");
        LogEntries entries = driver.manage().logs().get(LogType.BROWSER);
    }
}

As Firefox driver I am using this version : geckodriver-v0.30.0-linux64.tar.gz

Here is my Selenium version :

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.1.1</version>
</dependency>

My problem is that when running the previous code I get the following exception :

Driver info: driver.version: RemoteWebDriver at org.openqa.selenium.json.JsonInput.peek(JsonInput.java:122) at org.openqa.selenium.json.JsonTypeCoercer.lambda$null$6(JsonTypeCoercer.java:140) at org.openqa.selenium.json.JsonTypeCoercer.coerce(JsonTypeCoercer.java:126) at org.openqa.selenium.json.Json.toType(Json.java:69) at org.openqa.selenium.json.Json.toType(Json.java:55) at org.openqa.selenium.json.Json.toType(Json.java:50) at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:87) at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49) at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158) at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552) at org.openqa.selenium.remote.RemoteExecuteMethod.execute(RemoteExecuteMethod.java:35) at org.openqa.selenium.remote.RemoteLogs.getRemoteEntries(RemoteLogs.java:81) at org.openqa.selenium.remote.RemoteLogs.get(RemoteLogs.java:77) at MainExample.main(MainExample.java:17)

If I run the code on a custom page that have some Java script error I do see them in the logs :

JavaScript error: http://localhost/js/app.js?version=625f9736, line 1: TypeError: e is undefined

but I am not able to retrieve them using

driver.manage().logs().get(LogType.BROWSER);

I have tried the different codes of this related subject but I am each time getting this error.

I also have tried to downgrade my selenium version to 3.141.59 but I am still getting the same error.

Upvotes: 3

Views: 603

Answers (1)

Max Daroshchanka
Max Daroshchanka

Reputation: 2988

Using WebDriver log endopints (not supported)

There is no get-logs endpoint defined by W3C WebDriver yet..

https://www.w3.org/TR/webdriver/#endpoints

And this still opened:

https://github.com/w3c/webdriver/issues/406

So, unfortunately, driver.manage().logs() is not implemented by Firefox.

From geckodriver team:

This isn't in the W3C spec at this time, so we are delaying support until the behaviour is well specified. But your request is noted.

See


Using DevTools (seems to work)

I was able to see the console output with selenium-4.1.1 and devtools.v85

package org.example.getlogs

import org.openqa.selenium.WebDriver
import org.openqa.selenium.devtools.DevTools
import org.openqa.selenium.devtools.v85.log.Log
import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.firefox.FirefoxOptions

class GetLogsTest {

    public static void main(String[] args) {
        FirefoxOptions options = new FirefoxOptions();
        WebDriver driver = new FirefoxDriver(options);
        DevTools devTools = driver.getDevTools();
        devTools.createSession();
        devTools.send(Log.enable());
        devTools.addListener(Log.entryAdded(),
                logEntry -> {
                    System.out.println("" + logEntry.getLevel()+ ": " + logEntry.getText());
                });
        driver.get("https://stackoverflow.com/questions/70787924/how-to-capture-java-script-errors-using-selenium-java-webdriver");
        driver.quit();
    }
}

Upvotes: 1

Related Questions