I was wondering if there is a better way to do things.
We are waiting for a few rest request to finish and collect the data to it.
With the Chrome console plugin getting the requests is handy, but waiting for the response and getting the data is quiet “ugly” as getting the response and getting the body data to it is split into two functions and the call needs to be finished up front
So currently we do something like:
Init…
ChromeDevToolsService cdts = CdpUtils.getService()
Page page = cdts.getPage()
def map = [:]
Network network = cdts.getNetwork()
Log log = cdts.getLog()
Register Request and Response events and store the data in a map…
network.onRequestWillBeSent({ def event ->
if (event.type.toString() == 'XHR') {
if (!(map.containsKey(event.requestId))) {
def requestEnty = [('RequestID') : event.requestId, ('RespondeID') : '', ('ResponseStatusCode') : '', ('RequestBody') : ''
, ('RequestUrl') : event.getRequest().getUrl(), ('RequestMethod') : event.getRequest().method]
map.put(event.requestId, requestEnty)
}
}
})
network.onResponseReceived({ def event ->
if (event.type.toString() == 'XHR') {
WebUI.comment((('onResponseReceived Request ID: ' + event.requestId) + ' - ') + event.response.status.toString())
if (map.containsKey(event.requestId)) {
valueList = map.get(event.requestId)
valueList.RespondeID = event.requestId
valueList.ResponseStatusCode = event.response.status.toString()
map.put(event.requestId, valueList)
}
}
})
Enable the listener
page.enable()
network.enable()
Now katalon runs sequential and the calls do come in one by one as some may take a few seconds or longer…so waiting for all to finish seams the only way…
boolean hasNotAllRequest = true
while (hasNotAllRequest) {
def map2 = bar = map.getClass().newInstance(map)
def sizebefore = map.size()
for (def e : map2) {
if (e != null) {
def valueList = e.value
if (e.key == valueList.RespondeID) {
hasNotAllRequest = false
} else {
hasNotAllRequest = true
}
}
}
if (map.size() != sizebefore) {
hasNotAllRequest = true
}
if (hasNotAllRequest) {
WebUI.delay(1)
}
}
and than getting the body data to it once all calls are finished…
def map2 = bar = map.getClass().newInstance(map)
for (def entry : map2) {
if (entry != null) {
try {
String body = network.getResponseBody(entry.key.toString()).body
entry.value.RequestBody = body
}
catch (Exception e) {
}
}
}
Am i overlooking something that is a bit more smart or elegant?
We tried and call the getResponseBody in the onResponseReceived event and that results in a error.
Any ideas are welcome