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

how to get value from raw or xml HTML response by groovy

我正在尝试从这种原始响应中获取 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