如何通过groovy从原始或xml HTML响应中获取值



我正试图从这种原始响应中获取userToken:

HTTP/1.1 200 
Date: Wed, 19 Feb 2020 14:48:32 GMT
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Server: nginx
Vary: Accept-Encoding
Content-Security-Policy: default
Content-Language: en-US
Content-Encoding: gzip
<!DOCTYPE html>
<html>
<head>
<link rel="icon" href="https.."/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<title>Portal</title>
</head>
<body>
<div id="root"></div>
<script type="application/javascript">
window.__webpack_public_path__ = "https";
window.pageData = {
configuration: {
lang: "en",
country: "UK",
customization: (input)
},
userToken: "3ae85a89-1905-4120-9a8c-7a5d623b51d6",
cOMPANY: 'OPEL',
policies: JSON.parse(INPUT)

不幸的是,我无法直接了解userToken的值,我尝试了这种断言

import com.eviware.soapui.support.XmlHolder
import net.sf.json.groovy.JsonSlurper
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder(messageExchange.responseContentAsXml)
/*for ( userToken in holder.getNodeValues( "//window.pageData/configuration/userToken" ))
log.info "UserToken : [$userToken]"
*/
def userT = holder["//*:script/@userToken"]
token = (Arrays.toString(userT))
log.info token

但仍然什么都没有,我收到的是空括号[];如果没有@userToken,我将从脚本标签中接收整个部分。知道怎么处理吗?将感谢

在xml中,响应如下:

<html>
<head>
<meta content="HTML Tidy for Java (vers. 26 wrz 2004), see www.w3.org" name="generator"/>
<link href="https:" rel="icon"/>
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" name="viewport"/>
<title>Portal Seite</title>
</head>
<body>
<div id="root"/>
<script type="application/javascript">window.__webpack_public_path__ = "https:";
window.pageData = {
configuration: {
lang: "de",
country: "CH",
customization: JSON.parse(input)
},
userToken: "3ae85a89-1905-4120-9a8c-7a5d623b51d6",
company: 'OPEL',
policies: JSON.parse(
]]><br/><![CDATA[
"}]"), 
]]><script src="https:" type="text/javascript"/>
</body>
</html>

要详细说明您关于RegEx解决方案的问题,您可以将脚本更改为:

def xml = messageExchange.responseContentAsXml
def pattern = /userToken: "([a-z0-9-]+)"/
def match = xml =~ pattern
def token = (match.find()) ? match.group(1) : null
println token

最新更新