Shashank Lahariya
Shashank Lahariya

Reputation: 109

How to fix "log type 'performance' not found" issue?

I am trying to fetch all the network calls(ajax,etc.) using selenium automation for Chrome browser. I am using "LoggingPreferences" capability for the same. But I am getting the below error thrown while it tries to fetch the call logs.

log type 'performance' not found    

I am using selenium-server-standalone-2.53.0 and chromedriver 2.40. I am running my test cases on mac.

Capabilities Code:

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;

//Code to fetch network calls

List les = getDriver().manage().logs().get(LogType.PERFORMANCE).getAll();

Ideally network calls should be fetched but rather error is getting thrown while fetching the logs:

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

Upvotes: 1

Views: 10433

Answers (3)

Ahmed Amin
Ahmed Amin

Reputation: 1

I'm using undetected chrome drive and Python and it only works when I set the options after the capabilities

capabilities = DesiredCapabilities.CHROME
capabilities["goog:loggingPrefs"] = {"performance": "ALL"}
options = uc.ChromeOptions()
options.add_argument("--incognito")
options.add_argument('--disable-gpu')
self.browser = uc.Chrome(options=options, desired_capabilities=capabilities)

Upvotes: 0

Ankush kumar
Ankush kumar

Reputation: 61

You may use below code for reference

    public void public static void main(String[] args) {    
        WebDriver driver = null;
         Map<String, String> mobileEmulation = new HashMap<>();
         mobileEmulation.put("deviceName", "Nexus 5");
         //this is just to lauch my browser in mobile view
         ChromeOptions op =  new ChromeOptions();
         op.setExperimentalOption("mobileEmulation", mobileEmulation);
        
        //this is to ENABLE LogType.PERFORMANCE
        DesiredCapabilities caps = DesiredCapabilities.chrome();
        LoggingPreferences logPrefs = new LoggingPreferences();
        logPrefs.enable(LogType.PERFORMANCE, Level.INFO);
        caps.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
        
        //this is to merge your desired capabilties with ChromeOptions (as ChromeDriver(capabilties) is suppressed)
        op.merge(caps); //THIS IS SOMETHING WHICH IS MISSING IN YOUR CODE

        //this is just to instantiate the driver and lauch my application
        driver = new ChromeDriver(op);
        driver.get("https://m.snapdeal.com/");
    
        //this is just to make you know user number of logs with LogType as PERFORMANCE
        List<LogEntry> entries = driver.manage().logs().get(LogType.PERFORMANCE).getAll();
        System.out.println(entries.size() + " " + LogType.PERFORMANCE + " log entries found");
      
      
        //as the request and response will consists HUGE amount of DATA so I will be write it into text file for reference
        for (LogEntry entry : entries) 
        {
            try 
            {
                FileWriter f = new FileWriter("pathOfYourFileWhereYouWantToWriteYourData", true);
                BufferedWriter bufferedWriter = new BufferedWriter(f);
                        
                String data =  (new Date(entry.getTimestamp()) + " " + entry.getLevel() + " " + entry.getMessage()).toString();
                        
                bufferedWriter.write(data);
                bufferedWriter.write("\n"+"@@@@@@@@@@"+"\n\n");
                bufferedWriter.close();
            } catch (IOException e) 
             {
                e.printStackTrace();
             }
        }
    
     }

Upvotes: 1

Martell
Martell

Reputation: 23

This Below Code is working for the Status code extraction using 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();
                }
            }
        }
    }

Upvotes: 1

Related Questions