如何解决“找不到日志类型'性能'”的问题?

时间:2019-02-11 06:47:32

标签: selenium-webdriver logging

我正在尝试使用针对Chrome浏览器的硒自动化来获取所有网络调用(ajax等)。我正在使用“ LoggingPreferences”功能。但是,在尝试获取呼叫日志时,出现了以下错误。

log type 'performance' not found    

我正在使用selenium-server-standalone-2.53.0和chromedriver 2.40。我正在Mac上运行测试用例。

功能代码:

final DesiredCapabilities capabilities = new DesiredCapabilities();
    final List<String> chromeOptionArgs = new ArrayList<String>();
    final Map<String, Object> chromeOptions = new HashMap<>();

    chromeOptions.put("args", chromeOptionArgs);
    chromeOptions.put("mobileEmulation", ImmutableMap.of("deviceName",device.name));
    chromeOptionArgs.addAll(Arrays.asList("disable-extensions", "test-type", "no-default-browser-check",
            "ignore-certificate-errors"));
    LoggingPreferences logPrefs = new LoggingPreferences();
    logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
    capabilities.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
    capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
    return capabilities;

//获取网络呼叫的代码

列表les = getDriver()。manage()。logs()。get(LogType.PERFORMANCE).getAll();

理想情况下,应该获取网络调用,但在获取日志时会抛出错误:

 org.openqa.selenium.WebDriverException: unknown error: log type 'performance' not found    

2 个答案:

答案 0 :(得分:0)

您可以使用以下代码作为参考

Query Not Executed. Please Fix the Issue! You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use
near 'll always run back to! It's made of 5.5 oz., 50% cotton / 50% poly (with up to 5' at line 1

答案 1 :(得分:0)

以下代码正用于使用Selenium提取状态代码。

import java.util.Iterator;
import java.util.logging.Level;
import org.json.JSONException;
import org.json.JSONObject;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.logging.LogEntry;
import org.openqa.selenium.logging.LogType;
import org.openqa.selenium.logging.LoggingPreferences;
import org.openqa.selenium.remote.DesiredCapabilities;

public class TestResponseCode {
    public static void main(String[] args) {
        // simple page (without many resources so that the output is
        // easy to understand
        String url = "https://www.bing.com/";
        downloadPage(url);
    }

    private static void downloadPage(String url) {
        System.setProperty("webdriver.chrome.driver", "D:\\Softwares\\Selenium\\Drivers\\ChromeDrivers\\75.0.3770.140\\chromedriver.exe");
        ChromeDriver driver = null;
        try {
            ChromeOptions options = new ChromeOptions();
            // add whatever extensions you need
            // for example I needed one of adding proxy, and one for blocking
            // images
            // options.addExtensions(new File(file, "proxy.zip"));
            // options.addExtensions(new File("extensions",
            // "Block-image_v1.1.crx"));
            DesiredCapabilities cap = DesiredCapabilities.chrome();
            cap.setCapability(ChromeOptions.CAPABILITY, options);
            // set performance logger
            // this sends Network.enable to chromedriver
            /*
             * LoggingPreferences logPrefs = new LoggingPreferences();
             * logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
             * cap.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
             */

            LoggingPreferences logPrefs = new LoggingPreferences();
            logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
            options.setCapability("goog:loggingPrefs", logPrefs);
            driver = new ChromeDriver(options);
            // navigate to the page
            System.out.println("Navigate to " + url);
            driver.navigate().to(url);
            // and capture the last recorded url (it may be a redirect, or the
            // original url)
            String currentURL = driver.getCurrentUrl();
            LogEntries logs = driver.manage().logs().get("performance");
            int status = -1;
            System.out.println("\\nList of log entries:\\n");
            for (Iterator<LogEntry> it = logs.iterator(); it.hasNext();) {
                LogEntry entry = it.next();
                try {
                    JSONObject json = new JSONObject(entry.getMessage());
                    System.out.println(json.toString());
                    JSONObject message = json.getJSONObject("message");
                    String method = message.getString("method");
                    if (method != null && "Network.responseReceived".equals(method)) {
                        JSONObject params = message.getJSONObject("params");
                        JSONObject response = params.getJSONObject("response");
                        String messageUrl = response.getString("url");
                        if (currentURL.equals(messageUrl)) {
                            status = response.getInt("status");
                            System.out.println("---------- bingo !!!!!!!!!!!!!! returned response for " + messageUrl
                                    + ": " + status);
                            System.out.println("---------- bingo !!!!!!!!!!!!!! headers: " + response.get("headers"));
                        }
                    }
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            System.out.println("\nstatus code: " + status);
        } finally {
            if (driver != null) {
                driver.quit();
            }
        }
    }
}