Capture web requests using Selenium

Selenium tool is basically using for automating web applications. During test suite execution it is good to capture the web requests so that we can easily figure out the issues observed in execution.

There are two ways to capture the network requests,

1. Through proxy - Capture all web requests through a proxy like Fiddler or Browsermob and parse the log later.
2. Using chrome driver - Enable performance logging in desired capabilities. You can capture performance log, timeline and page events.

Here its detailed steps, First we need to define performance logging in desired capabilities section.

Enable logging in desired capabilities
DesiredCapabilities caps = DesiredCapabilities.chrome();
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.PERFORMANCE, Level.INFO);
caps.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
Now all network related logs are capturing and next step is the storing of log data. Here shows its code snippet for storing the data.

Collecting Logs
FileWriter fr=new FileWriter("C:\\data.txt");
BufferedWriter br=new BufferedWriter(fr);
LogEntries lo = driver.manage().logs().get(LogType.PERFORMANCE);
 for (LogEntry le : lo) {      
  br.write(le.getMessage());     
  br.newLine();       
 }
All collected data now available in data.txt file and you can process it based on your requirements.
Previous Post Next Post