Katalon still relies on Selenium WebDriver 3.*, which means we can't use Chrome DevTools directly. This blog describes a few cases where we might want to use Chrome DevTools. Hence the name, Chrome DevTools is only available in Chromium-based browsers.
Prerequisite As a prerequisite, we need to make sure both Java SDK and Gradle are installed / available on the path.
Modifying build.gradle
implementation group: 'io.testsmith', name: 'cdt-java-client', version: '4.0.0-testsmith-2'
Running Gradle Command
Make Chrome DevTools Available
public class CdpUtils {
public static String[] getServiceEndpoint() {
WebDriver driver = DriverFactory.getWebDriver()
HasCapabilities hc = ((driver) as HasCapabilities)
Map<?, ?> m = hc.getCapabilities().asMap()
m = m.get('goog:chromeOptions')
String da = m.get('debuggerAddress')
String[] daElements = da.split(':')
return daElements
}
public static ChromeDevToolsService getService() {
WebDriver driver = DriverFactory.getWebDriver()
String[] daElements = getServiceEndpoint()
String host = daElements[0]
int port = Integer.parseInt(daElements[1])
ChromeService cs = new ChromeServiceImpl(host, port)
ChromeTab t = cs.getTabs().find({
ChromeTab tab = ((it) as ChromeTab)
if (tab.getUrl().equals(driver.getCurrentUrl())) {
return tab
}
})
ChromeDevToolsService cdts = cs.createDevToolsService(t)
return cdts
}
}
Now we can implement the following features (and many more...) into Katalon.
@Keyword
def setGeoLocation(double latitude, double longitude, int accuracy) {
WebDriver driver = DriverFactory.getWebDriver()
ChromeDevToolsService cdts = CdpUtils.getService()
cdts.emulation.setGeolocationOverride(latitude, longitude, accuracy)
driver.navigate().refresh()
}
@Keyword
def setTimezone(String timezone) {
WebDriver driver = DriverFactory.getWebDriver()
ChromeDevToolsService cdts = CdpUtils.getService()
cdts.emulation.setTimezoneOverride(timezone)
driver.navigate().refresh()
}
@Keyword
def waitForNetworkRequest(String pathEndsWidth, int expectedAmount) {
/** Get DevTools service to a tab in Chrome browser */
WebDriver driver = DriverFactory.getWebDriver()
ChromeDevToolsService cdts = CdpUtils.getService()
/** Get individual CDP commands */
Page page = cdts.getPage()
Network network = cdts.getNetwork()
page.enable()
network.enable()
// Variable to track the completion of the desired request
int desiredRequestComplete = 0
String desiredRequestUrl = pathEndsWidth // Change this to your specific request URL
/** Log requests */
network.onRequestWillBeSent({ RequestWillBeSent event ->
println "request : ${event.getRequest().getMethod()} ${event.getRequest().getUrl()}"
})
/** Log responses and check for the desired request */
network.onResponseReceived({ ResponseReceived event ->
Response resp = event.getResponse()
println "response: ${resp.getStatus()} ${resp.getStatusText()} ${resp.getUrl()}"
if (resp.getUrl().endsWith(desiredRequestUrl)) {
desiredRequestComplete++;
}
})
WebUI.navigateToUrl(GlobalVariable.baseUrl)
// Wait until the desired request is completed
while (desiredRequestComplete != expectedAmount) {
Thread.sleep(500)
}
cdts.close()
cdts.waitUntilClosed()
}
Delen: