2

I'm writing selenium tests which are launch on the firefox (gecko) driver.

If a test fails, I want to retrieve the log present on the firefox js console.

In order to do that, I have added the following into my code:

On the "Before" statement:

LoggingPreferences logs = new LoggingPreferences();
logs.enable(LogType.BROWSER, Level.ALL);
logs.enable(LogType.DRIVER, Level.ALL);
logs.enable(LogType.CLIENT, Level.ALL);
logs.enable(LogType.PERFORMANCE, Level.ALL);
logs.enable(LogType.PROFILER, Level.ALL);
logs.enable(LogType.SERVER, Level.ALL);
        
options.setCapability(CapabilityType.LOGGING_PREFS, logs);
        
driver = new FirefoxDriver(options);

On the "After" statement:

   try {
        FileOutputStream out = new FileOutputStream("target/surefire-reports/consoleLog-" + fileName + ".log");

        List<LogEntry> myLogs = driver.manage().logs().get(LogType.BROWSER).getAll();
        
        for (int i = 0; i < myLogs.size(); i++){
            out.write(myLogs.get(i).toString().getBytes());
        }
        
        out.close();
    }

However, when this is run, I got the following error:

org.openqa.selenium.UnsupportedCommandException: POST /session/a6064f0a-713c-4f55-9a17-f6d80f4c457d/log did not match a known command

After doing some research, it turns out that The Selenium log API is not (yet?) supported by geckodriver. (source: link 1, link 2)

Question:

So, is there some other way around to accomplish what I want to accomplish? (using Chrome instead of Firefox is not a solution).

1

2 Answers 2

1

Try this piece of code and see if you get the logs:

WebDriver driver;
System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") + "//geckodriver");
System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE, "FFLogs.txt");
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("https://www.tutorialspoint.com/tutor_connect/index.php");
driver.manage().window().maximize();
Thread.sleep(2000);

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
// identify element
driver.findElement(By.id("search-strings")).sendKeys("Selenium");

Thread.sleep(5000);
driver.close();
driver.quit();

Reference: https://www.tutorialspoint.com/getting-console-log-output-from-firefox-with-selenium

0

If you can't access the logs via the webdriver, you can maybe work around it by adding the logs to the DOM with the JavascriptExecutor. With the JavascriptExecutor it may be possible to insert this script into the page to capture an error. Though this will most likely not help when the error already happened.

You can ask the frontend developers if they could add the mentioned script to every page to make test automation easier for you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.