Skip to content

Instantly share code, notes, and snippets.

@jamesdh
Created September 29, 2022 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesdh/92a80c52339b5259b88453ad60830091 to your computer and use it in GitHub Desktop.
Save jamesdh/92a80c52339b5259b88453ad60830091 to your computer and use it in GitHub Desktop.
Inspecting ChromeDriver HTTP Requests
// A simple Geb (gebbish.org) WebDriver config
import io.github.bonigarcia.wdm.WebDriverManager
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeDriverService
import org.openqa.selenium.chrome.ChromeOptions
import org.openqa.selenium.logging.LogType
import org.openqa.selenium.logging.LoggingPreferences
driver = {
// Uses https://github.com/bonigarcia/webdrivermanager to automate chromedriver setup
WebDriverManager.chromedriver().setup()
ChromeOptions options = new ChromeOptions()
ChromeDriverService service = new ChromeDriverService.Builder().build()
// Allows us to inspect the dev console network tab results for extracting headers (alternative to proxy/MITM interception)
LoggingPreferences prefs = new LoggingPreferences()
prefs.enable(LogType.PERFORMANCE, Level.ALL)
options.setCapability(ChromeOptions.LOGGING_PREFS, prefs)
// Finally, builds and returns the WebDriver instance
return new ChromeDriver(service, options)
}
// Then anywhere else that you've rendered a page and have access to the WebDriver, you can do something like the following to inspect the request
import groovy.json.JsonSlurper
import org.openqa.selenium.logging.LogEntries
import org.openqa.selenium.logging.LogEntry
import org.openqa.selenium.logging.LogType
LogEntries entries = driver.manage().logs().get(LogType.PERFORMANCE)
JsonSlurper slurper = new JsonSlurper()
entries.collect { LogEntry log ->
slurper.parseText(log.message.split("\\[INFO\\]").first()).message//.params?.request?.headers
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment