简体   繁体   English

从空手道中的 html 响应中提取 Javascript

[英]Extract Javascript from html response in karate

I need help here.我在这里需要帮助。 I have a POST request which returns response with a HTML response and this HTML response contains javascript inside it.我有一个 POST 请求,它返回带有 HTML 响应的响应,并且这个 HTML 响应在其中包含 javascript。 I need to extract text/javascript where it says checkout = {"TransactionID""}.. and pass it as a body in the new POST Request using Karate. I think I need to write javascript function inside the karate feature file, however, I am not able to import libraries etc.我需要提取文本/javascript,其中显示 checkout = {"TransactionID""}.. 并使用空手道在新的 POST 请求中将其作为正文传递。我想我需要在空手道功能文件中编写 javascript function,但是,我无法导入库等。

        <!DOCTYPE html>
<html lang="en">
<head>
    <title> ABC test</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="#eb843a">
    <meta name="theme-color" content="#eb843a">
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
    <link rel="icon" href="/favicon.ico" type="image/x-icon">
    <link rel="stylesheet" href="https://use.typekit.net/plq2clw.css" />
    <style type="text/css">
        body {
            background-color: #eff3ef;
        }

        .splash-content {
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        }



        .splash-ring {
            position: relative;
            width: 64px;
            height: 64px;
            margin: 0 auto;
            margin-bottom: 20px;
        }



        }
    </style>
</head>
<body>
    <div aurelia-app="root">





        <div class="splash-body animated fadeIn">
            <div class="splash-content splash-primary">

                <div>
                    <div class="splash-ring"><div></div><div></div><div></div><div></div></div>
                </div>
                humming...
            </div>
        </div>
    </div>

    <script src="/Client/jspm_packages/system.js"></script>
    <script type="text/javascript">
        System.baseURL = "/Client/";
        var version = '1.1.46.29705';
        var checkout = null;
        var transactionId = null;
        var channel = null;
    </script>

    <script src="/Scripts/crtgpf.js"></script>
    <script src="/Scripts/crtgpf-wrapper.js"></script>

        <script type="text/javascript">
            checkout = **{"TransactionId":457123,"TransactionTimeout":0,"Step":38,"Status":10,"Channel":1,"Variation":"DEFAULT","ProductType":"EPOZ","ProductMode":2,"PlatformType":"Default","Platform":{"Name":"Default","Currency":"AUD","IsTesting":false,"IsShipped":false,"Nonce":null,"CustomerInfo":{"Title":"Mr","FirstName":"first541","MiddleName":"JOURNEYA","LastName":"external541","FullName":"first541 JOURNEYA external541","Email":"aaa@b.com.au","Mobile":"33333","PhoneHome":"33333","PhoneWork":"0881110011","DateOfBirth":"1970-08-01T00:00:00Z"},"Instalments":[],"Deposit":0.0,"DepositInitial":0.0,"Amount":200.5,"Description":"","FinancedAmount":200.5,"Frequency":2,"TermsInMonths":5},"Data":{"customer.detail.middleName":"JOURNEYA","employment.suburb":"burb1","customer.address.home.line1":"1528 STURT ,"client.headers":"[{"Key":"Cache-Control","Value":"max-age=0"},{"Key":"Connection","Value":"keep-alive"},{"Key":"Content-Length","Value":"1549"},{"Key":"Content-Type","Value":"application/x-www-form-urlencoded"},"Outcome":0,"AcceptedTerms":null,"AcceptedPrivacy":null,"ConsumerFees":null,"PaymentPlans":[],"Redirect":null,"AuthReason":null,"ErrorCode":null}**;
            transactionId = 457123;
        </script>

    <script src="/Client/2222/config.js"></script>
    <script src="/Client/2222/dist/startup.js"></script>
        <!-- Google Tag Manager -->
        <script>
            var gtmContainerId = 'GTM-5533';

            (function (w, d, s, l, i) {
                w[l] = w[l] || []; w[l].push({
                    'gtm.start':
                        new Date().getTime(), event: 'gtm.js'
                }); var f = d.getElementsByTagName(s)[0],
                    j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : ''; j.async = true; j.src =
                        'https://www.googletagmanager.com/gtm.js?id=' + i + dl; f.parentNode.insertBefore(j, f);
            })(window, document, 'script', 'dataLayer', gtmContainerId);
        </script>
        <!-- End Google Tag Manager -->

</body>
</html>;

I need to extract this javascript which will be a request in the next POST call.我需要提取这个 javascript,这将是下一个 POST 调用中的请求。

Use a Java regex :使用Java 正则表达式

* def pattern = java.util.regex.Pattern.compile('checkout =.+(\\{[^;]+)')
* def matcher = pattern.matcher(response);
* assert matcher.find()
* def body = matcher.group(1)

We will be adding this as a karate.extract() helper in 0.9.6.RC3 onwards: https://github.com/intuit/karate/issues/1094#issuecomment-616059232我们将在 0.9.6.RC3 之后将其添加为karate.extract()助手: https://github.com/intuit/karate/issues/1094#issuecomment-616059232

EDIT - better, detailed example: https://stackoverflow.com/a/67331307/143475编辑 - 更好,更详细的例子: https://stackoverflow.com/a/67331307/143475

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM