Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
112 views
in Technique[技术] by (71.8m points)

java - 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:UsersBPJ0sGWAppDataLo...}, 
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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...