Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium - Chrome Performance Logs not Working

Hi I have a selenium script that runs and is supposed to give me performance logs. I have a method 'printLog' that should (obviously) print the performance logs. My code will be able to explain in depth exactly what I am trying to do.

static void printLog(String type, RemoteWebDriver driver, String inputURL)  {

    ChromeOptions cap = new ChromeOptions();
    LoggingPreferences logP = new LoggingPreferences();
    logP.enable(LogType.PERFORMANCE, Level.ALL);
    cap.setCapability(CapabilityType.LOGGING_PREFS, logP);

    List<LogEntry> entries = driver.manage().logs().get(type).getAll();
    System.out.println("\"Input URL\"," + "\"" + inputURL + "\"");
    for (LogEntry entry : entries) {
        // Checks whether this is a webtrends tag and whether it was accepted by the
        // server

        if (entry.getMessage().contains("statse") && entry.getMessage().contains("Network.responseReceived")) {
            String statseString = entry.getMessage();
            // regex for finding all wt tags: WT\..+?(?=&)
            // List<String> allMatches = new ArrayList<String>();
            // Matcher m = Pattern.compile("WT\\..+?(?=&)")
            // .matcher(statseString);
            // while (m.find()) {
            // allMatches.add(m.group());
            // }
            int statseBegin = statseString.indexOf("\"url\":\"") + 1;
            int statseEnd = statseString.indexOf("\"},\"", statseBegin);
            statseString = statseString.substring(statseBegin, statseEnd);
            String[] allMatches = statseString.split("&");
            for (String tags : allMatches) {
                tags = tags.replaceFirst("=", "ReallyLongUniqueStringWithNoChanceOfOverlap");
                String tagParts[] = tags.split("ReallyLongUniqueStringWithNoChanceOfOverlap");

                if (tagParts.length > 1) {
                    System.out.println("\"" + tagParts[0] + "\",\"" + tagParts[1] + "\"");
                } else {
                    System.out.println("\"" + tagParts[0] + ",\"\"");
                }
            }
        }
    }
}

When I run the code, Chrome opens and I get this stacktrace in my console:

Exception in thread "main" org.openqa.selenium.WebDriverException: unknown 
error: log type 'performance' not found
(Session info: chrome=69.0.3497.92)
(Driver info: chromedriver=2.42.591088 
(7b2b2dca23cca0862f674758c9a3933e685c27d5),platform=Windows NT 10.0.16299 
x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05- 
08T15:15:03.216Z'
System info: host: 'WKSP0009ADAD', ip: '172.17.237.35', os.name: 'Windows 
10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_191'
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, 
applicationCacheEnabled: false, browserConnectionEnabled: false, 
browserName: chrome, chrome: {chromedriverVersion: 2.42.591088 
(7b2b2dca23cca0..., userDataDir: C:\Users\BPJ0sGW\AppData\Lo...}, 
cssSelectorsEnabled: true, databaseEnabled: false, goog:chromeOptions: 
{debuggerAddress: localhost:50809}, handlesAlerts: true, hasTouchScreen: 
false, javascriptEnabled: true, locationContextEnabled: true, 
mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: 
false, pageLoadStrategy: normal, platform: XP, platformName: XP, rotatable: 
false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, 
unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 
69.0.3497.92, webStorageEnabled: true, webdriver.remote.sessionid: 
f50e54d130a8c7e3b3a9cb6984f...}
Session ID: f50e54d130a8c7e3b3a9cb6984fcb558
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
atsun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAcc 
essorImpl.java:62)
atsun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstr 
uctorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
atorg.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:
atorg.openqa.selenium.remote.RemoteLogs.getRemoteEntries(RemoteLogs.java:81)
at org.openqa.selenium.remote.RemoteLogs.get(RemoteLogs.java:77)
at demoJenkins.WebTrendsCapture.printLog(WebTrendsCapture.java:141)
at demoJenkins.WebTrendsCapture.main(WebTrendsCapture.java:114)

I can provide more details upon request, but basically I am just trying to figure out why this method returns this error. Thanks.

like image 541
Mr. Mayonnaise Avatar asked Nov 27 '22 23:11

Mr. Mayonnaise


2 Answers

For anyone coming to this recently - in recent Selenium and ChromeDriver versions (eg 3.14.159, chrome driver 76.x) setting up loggingPrefs with ChromeDriver doesn't seem to work, using the local ChromeDriver - no matter what you do you seem to get the log type 'performance' not found error

The reason is increasingly strict w3c spec compliance, both in Selenium and ChromeDriver code.

Instead of the loggingPrefs/CapabilityType.LOGGING_PREFS capability, you need to use goog:loggingPrefs, like so

    ChromeOptions options = new ChromeOptions();
    LoggingPreferences logPrefs = new LoggingPreferences();
    logPrefs.enable( LogType.PERFORMANCE, Level.ALL );
    options.setCapability( "goog:loggingPrefs", logPrefs );

I guess that sooner or later the Selenium side will notice this and change the CapabilityType.LOGGING_PREFS constant or add a new one, but the project didn't look very friendly to log an issue with to me.

like image 158
Jon N Avatar answered Dec 09 '22 19:12

Jon N


From https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/8386 by @Kiril S,This seems to only happen when using RemoteWebDriver. Using a pure ChromeDriver object will work.

like image 42
thouger Avatar answered Dec 09 '22 18:12

thouger