获取XHR响应(网络流量)并在Katalon Studio中进行解析 [英] Get XHR response (network traffic) and parse it in Katalon Studio

查看:264
本文介绍了获取XHR响应(网络流量)并在Katalon Studio中进行解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何读取XHR响应并在Katalon Studio中解析?

How can I read an XHR response and parse it in Katalon Studio?

我目前使用一种变通方法来测试我的应用程序的响应速度:我使用各种waitForElement_*_()(* =可见,可点击,存在,不可见,不可点击,不存在)命令来测量加载时间各种元素.

I currently use a workaround way of testing responsiveness of my app: I use various waitForElement_*_() (*=visible, clickable, present, not-visible, not-clickable, not-present) commands in order to measure loading time of various elements.

我想更具体一点,并衡量网络请求的持续时间(可以在DevTools中看到-网络流量).

I would like to get more specific and measure the duration of network requests (that can be seen in DevTools - network traffic).

能做到吗?

推荐答案

在Katalon 7中以及 Chrome DevTools协议集成插件,如在这里,您可以拦截网络请求.

In Katalon 7 and with and with Chrome DevTools Protocol Integration plugin, as was described here you can intercept network requests.

以下示例显示了如何在Wikipedia中模拟搜索请求,以便结果始终为"Katalon Studio".

The following example shows how to mock search requests in Wikipedia so that the result will always be "Katalon Studio".

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.github.kklisura.cdt.protocol.commands.Fetch as Fetch
import com.github.kklisura.cdt.protocol.commands.Page as Page
import com.github.kklisura.cdt.services.ChromeDevToolsService as ChromeDevToolsService
import com.katalon.cdp.CdpUtils as CdpUtils
import com.kms.katalon.core.util.internal.Base64 as Base64
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObject as TestObject

WebUI.openBrowser('')
ChromeDevToolsService cdts = CdpUtils.getService()
Page page = cdts.getPage()
Fetch fetch = cdts.getFetch()
fetch.onRequestPaused({ def requestIntercepted ->
    String interceptionId = requestIntercepted.getRequestId()
    String url = requestIntercepted.getRequest().getUrl()
    boolean isMocked = url.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', url, System.lineSeparator())
    if (isMocked) {
        fetch.fulfillRequest(interceptionId, 200, new ArrayList(), rawResponse, null)
    } else {
        fetch.continueRequest(interceptionId)
    }
})

fetch.enable()
page.enable()
WebUI.navigateToUrl('https://en.wikipedia.org/wiki/Main_Page')
TestObject searchInput = new TestObject().addProperty('css', ConditionType.EQUALS, '#searchInput')
TestObject containing = new TestObject().addProperty('xpath', ConditionType.EQUALS, "//div[div[contains(.,'containing...')]]")
WebUI.setText(searchInput, 'Intercept request')
WebUI.waitForElementVisible(containing, 10)

注释:

Katalon论坛上的原始帖子: https://forum.katalon.com/t/intercepting-request-with-chrome-devtools-protocol/36081 .

Original post on Katalon forum: https://forum.katalon.com/t/intercepting-request-with-chrome-devtools-protocol/36081.

本主题中使用的示例项目: https://github.com/katalon-studio-samples/katalon-studio-chrome-devtools-protocol-plugin-samples .

Sample project used in this topic: https://github.com/katalon-studio-samples/katalon-studio-chrome-devtools-protocol-plugin-samples.

该插件使用 https://github.com/kklisura/chrome-devtools- java-client 连接到CDP.

The plugin uses https://github.com/kklisura/chrome-devtools-java-client to connect to CDP.

这篇关于获取XHR响应(网络流量)并在Katalon Studio中进行解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆