在构建有效负载时动态设置 XML 标记值 [英] Dynamically set a XML tag value while building payload

查看:33
本文介绍了在构建有效负载时动态设置 XML 标记值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用空手道进行自动化测试.

I am trying automate testing using Karate.

我有一个 XML 负载.

I have a XML payload.

我有一个静态 XML 负载,我正在从文件中准备好它,我想循环调用我的服务.

I have a static XML payload which I am readying from a file and I want to call my service in loop.

对于每次调用,我想动态替换标签名称的值.

For each call I would like to replace value for a tag name dynamically.

我将如何实现这一目标?

How would I achieve this?

例如

下面是我的主要特征,它在循环中调用了我的共同特征

Below is my Main Feature which calls my common feature in loop

Feature: Loop Call
Background:
* def common = call read('classpath:CommonFeatures.feature')

Scenario:
* table table
    | payload_file    | field_tag  | field_value |
    | 'HappyPath.xml' | 'car_fuel' | 'Gas'     |
    | 'HappyPath.xml' | 'car_color'| 'Red'     |

* def response = call read('classpath:Car.feature')  table

汽车功能

Feature: Common
Scenario:
    * print payload_file
    * print field_tag
    * print field_value
    * xml payload = read('classpath:/payload/'+payload_file)
    * print payload
    * set payload/$field_tag = field_value

这是我在设置 field_tag 值时遇到问题的地方.

This is where I have issue setting the field_tag value.

我有其他选择来执行此操作,例如编写一个小型 java 脚本方法来替换标记值或使用 DOMParser 或 SAXParser 执行相同操作的小型 java 类.

I have other option to do this like writing a small java script method to replace the tag value or a small java class which use DOMParser or SAXParser to perform the same.

但是我想知道是否有任何空手道的构建方式来执行相同的操作.

However I would like to know if there is any karate in build way to perform the same.

如果我使用 var parser = new DOMParser(); 也可以使用 java 脚本方法替换标签值;似乎 DOMParser 不可用.有没有办法让它可用?

Also while using java script method to replace the tag value if I am using var parser = new DOMParser(); and it seems DOMParser is not available to use. Is there a way to make this available?

推荐答案

感谢 Peter 提供的所有帮助和示例.

Thanks to Peter for all help and the examples.

我觉得这是实现这一目标的最佳方式.

I feel this is the best way to achieve this.

写了一个小的javascript函数

Wrote a small javascript function

   """
    * def replaceTag =
      """
      function(x){
        karate.setXml('temp', x.payload);
        karate.pretty(karate.get('temp'));
        if (x.field_tag) karate.set('temp', x.field_tag, x.field_value);
        return karate.get('temp');
      }
    """

并从 Car.feature 调用相同的内容,如下所示,我得到动态替换的有效负载.

and calling the same from Car.feature like below and I get the dynamically replaced payload.

Feature: Common
Scenario:
    * print payload_file
    * print field_tag
    * print field_value
    * xml payload = read('classpath:/payload/'+payload_file)
    * print payload
    * def args = { payload: #(payload), field_tag: #(field_tag), field_value: #
          (field_value)}
    * print args
    * xml payload = call common.replaceTag args

注意:我必须升级 Karate 0.7.0 版本才能使用 karate.setXml 方法.

Note: I had to upgrade Karate 0.7.0 version in order to use karate.setXml method.

这篇关于在构建有效负载时动态设置 XML 标记值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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