硒-Chrome性能日志无法正常运行

时间:2018-10-29 15:40:21

标签: java selenium selenium-chromedriver

嗨,我有一个运行的selenium脚本,应该给我性能日志。 我有一个方法“ printLog”,应该(显然)打印性能日志。我的代码将能够深入解释我到底想做什么。

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] + ",\"\"");
                }
            }
        }
    }
}

运行代码时,Chrome打开,我在控制台中得到了这个堆栈跟踪:

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)

我可以根据要求提供更多详细信息,但是基本上我只是想弄清楚为什么该方法返回此错误。谢谢。

2 个答案:

答案 0 :(得分:11)

对于最近接触此问题的任何人-在最新的Selenium和ChromeDriver版本(例如3.14.159,chrome驱动程序76.x)中,使用本地ChromeDriver设置带有ChromeDriver的loggingPref似乎都行不通-无论您做什么您似乎收到了log type 'performance' not found错误

原因是Selenium和ChromeDriver代码中的w3c规范越来越严格。

您需要使用loggingPrefs/CapabilityType.LOGGING_PREFS而不是goog:loggingPrefs功能,

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

我想Selenium迟早会注意到这一点,并更改CapabilityType.LOGGING_PREFS常量或添加一个新常量,但是该项目看上去并不友好,无法向我记录问题。

答案 1 :(得分:1)

https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/8386@Kiril S,看来这仅在使用RemoteWebDriver时发生。使用纯ChromeDriver对象即可。