Intercepting request with Chrome Devtools Protocol

Sample project used in this topic: GitHub - katalon-studio-samples/katalon-studio-chrome-devtools-protocol-plugin-samples

The topic covers how to use Chrome Devtools Protocol (CDP) in Katalon Studio to intercept HTTP requests. Please note that this sample requires the plugin Chrome DevTools Protocol Integration.
This example will show you how to mock search requests in Wikipedia website so that the result will always be “Katalon Studio”. You can find the following code in the sample project (Intercept request test case).

Setup CDP


WebUI.openBrowser('')

ChromeDevToolsService cdts = CdpUtils.getService()

Page page = cdts.getPage()

Fetch fetch = cdts.getFetch()

Intercepting request

fetch.onRequestPaused({def requestIntercepted -> 

    String interceptionId = requestIntercepted.getRequestId()
	
	boolean isMocked = requestIntercepted.getRequest().getUrl().contains('api.php')
	
	String response = '["Katalon Studio",["Katalon Studio"],["Katalon Studio is an automation testing solution developed by Katalon LLC."],["https://en.wikipedia.org/wiki/Katalon_Studio"]]'
	
	String rawResponse = Base64.encode(response)
	
	System.out.printf('%s - %s%s', isMocked ? 'MOCKED' : 'CONTINUE', requestIntercepted.getRequest().getUrl(), System.lineSeparator())
	
	if(isMocked){
		fetch.fulfillRequest(interceptionId, 200, new ArrayList(), rawResponse, null)
	}
	    else {
		fetch.continueRequest(interceptionId)
	}
})

Enable CDP components

fetch.enable()

page.enable()

Selenium scripts

After setup with CDP, we can navigate, set text, click test object with Katalon as usual. Every request that matches our mocking conditions will return predefined responses. Here we search for “Intercept request” and the result show “Katalon Studio”.

WebUI.navigateToUrl('https://en.wikipedia.org/wiki/Main_Page')

WebUI.setText(findTestObject('Page_Wikipedia the free encyclopedia/Search input'), 
    'Intercept request')

WebUI.waitForElementVisible(findTestObject('Page_Wikipedia the free encyclopedia/Select Katalon'), 10)

WebUI.click(findTestObject('Page_Wikipedia the free encyclopedia/Select Katalon'))

WebUI.waitForPageLoad(10)

Note
The plugin uses GitHub - kklisura/chrome-devtools-java-client: Chrome DevTools java client. to connect to CDP. Please see the library’s repository for more examples.

12 Likes