如何为空手道特征使用动态值 [英] How to use dynamic values for Karate Features

查看:22
本文介绍了如何为空手道特征使用动态值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的空手道测试功能中使用动态值.

I've a need where I should use dynamic values in the features of my karate tests.

我遇到了一些这样的问题和答案:如何从 excel 电子表格中读取输入数据并在空手道框架中传递 JSON 有效负载?

I've came accross with some of the questions and answers like this: How to read input data from an excel spreadsheet and pass it JSON payload in karate framework?

但是不管我多么努力,我都无法做到.我认为我应该分享我正在尝试使用的代码部分,以便开始讨论.

But no mather what how hard I try, I couln't make it happen. I believe I should share the code parts that I am trying to use, so that a discussion can start.

我有一个创建新用户的 SOAP 请求,如下所示:

I have a SOAP request for creating new users as below:

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xxxxxx>
<soapenv:Header/>
<soapenv:Body>
    <int:createSubscriber soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
        <custBean xxxxx>
            <accountNumber xsi:type="xsd:string">#(accountNo)</accountNumber>
            <custName xsi:type="xsd:string" xs:type="type:string">Xbox</custName>
        </custBean>
        <addSubscriberBean xxxxx>7
            <subscriberID xsi:type="xsd:string">#(subsID)</subscriberID>
            <password xsi:type="xsd:string" xs:type="type:string">0</password>
            <areaID xsi:type="xsd:string" xs:type="type:string">1</areaID>
            <lineOfCredit xsi:type="xsd:int" xs:type="type:int"></lineOfCredit>
            <creditCycle xsi:type="xsd:int" xs:type="type:int"></creditCycle>
            <points xsi:type="xsd:int" xs:type="type:int"></points>
            <bandwidth xsi:type="xsd:int" xs:type="type:int"></bandwidth>
            <physicalPorts xsi:type="xsd:string" xs:type="type:string">8080</physicalPorts>
            <mobilePhoneNo xsi:type="xsd:string" xs:type="type:string">#(mobile)</mobilePhoneNo>
            <stbCount xsi:type="xsd:int" xs:type="type:int">5</stbCount>
            <oTTCount xsi:type="xsd:int" xs:type="type:int">10</oTTCount>
            <subscriptionType xsi:type="xsd:string" xs:type="type:string">#(subsType)</subscriptionType>
        </addSubscriberBean>
        <sequenceID xxxxx>1234567840123422700</sequenceID>
    </int:createSubscriber>
</soapenv:Body>

如您所见,我有一些将从外部提供的变量,它们是:accountNosubsIDsubsType移动.

As you've seen, I have some variables which are going to be given from outside, and those are: accountNo, subsID, subsType and mobile.

现在,我有一个功能文件,可以在其中使用上述文件调用 SOAP 服务.我正在为请求的所有变量分配新值,以便我可以一直创建新用户.

Now, I have a feature file where I make a call to a SOAP service by using above file. I am assigning new values to all variables of request, so that I can create new users all the time.

示例如下:

Feature: Create Subscriber Feature End-To-End Scenario

Background:
* url SOAP_CREATE_SUBSCRIBER_HOST

* def accountNumber = '789'
* def subscriberID = '456'
* def userMsisdn = '123'
* def subscriptionType = 'ASD'

* def createUser = read('create-user-soap.xml') # This is the above one
* replace createUser
  | token              | value               |
  | #(accountNo)       | accountNumber       |
  | #(subsID)          | subscriberID        |
  | #(mobile)          | userMsisdn          |
  | #(subsType)        | subscriptionType    |

Scenario: Create Subscriber
Given request createUser
When soap action SOAP_CREATE_SUBSCRIBER_HOST
Then status 200

And match //returnCode == 0
And match //returnMessage == 'The operation succeeded.'

但是,我需要创建一堆用户,所以我需要使用动态变量来调用我的 .xml 文件太多次.

However, I need to create bunch of users, so I need to use dynamic variables to call my .xml file too many times.

我检查了文档并在这里回答:如何从 excel 电子表格中读取输入数据并在空手道框架中传递 JSON 有效负载?

I checked the docs and answer here: How to read input data from an excel spreadsheet and pass it JSON payload in karate framework?

但在我的情况下找不到它.

But couldn't locate it in my situation.

提前致谢.

我知道我需要使用 table 或 json 或 csv 或 excel 类型的数据持有者来晚使用它的情况,所以下面是我的 users 表.只是不知道如何将它实现到我的功能文件中,以便它可以创建太多用户.

I am aware of the situation that I need to use table or json or csv or excel kind of data holder to use it late, so below is my users table. Just don't know how to implement it to my feature file so that it can create too many users.

    * table userstable
  | accountNo   | subsID         | mobile       | subsType   |
  | '113888572' | '113985218890' | '1135288836' | 'asd'     |
  | '113888573' | '113985218891' | '1135288837' | 'qwe'     |
  | '113888582' | '113985218810' | '1135288846' | 'asd'     |
  | '883889572' | '883985219890' | '8835298836' | 'qwe'     |
  | '773888572' | '773985218890' | '7735288836' | 'asd'     |
  | '663888572' | '663985218890' | '6635288836' | 'qwe'     |
  | '553888572' | '553985218890' | '5535288836' | 'asd'     |
  | '443888572' | '443985218890' | '4435288836' | 'qwe'     |
  | '333888572' | '333985218890' | '3335288836' | 'asd'     |
  | '223888572' | '223985218890' | '2235288836' | 'qwe'     |
  | '165488572' | '175585218890' | '1114788836' | 'asd'     |

编辑 2:在深入研究了一些答案并阅读了大量文档之后,我遇到了以下解决方案.应该有一个 .feature 文件,您可以在其中放置 create 方法以启动单用户创建机制.它看起来像这样:

EDIT 2: After a deep dive into the some answers and reading lots of docs, I've encounter with the solution below. There should be a .feature file where you place your create method to fire up the single user creation mechanism. It is going to look like this:

@ignore
Feature: re-usable feature to create a single user

Background:
* url SOAP_CREATE_SUBSCRIBER_HOST

Scenario: Create single user

* match __arg == bulkusers[__loop]
* def createUser = read('xxxx')

Given request createUser
When soap action SOAP_CREATE_SUBSCRIBER_HOST
And request { accountNo: '#(accountNo)', subsID: '#(subsID)', mobile: '#(mobile)', subsType: '#(subsType)' }
Then status 200

所以上面的代码可以作为模板放在你的脑海里.另一方面,我们需要另一个**.feature** 文件来调用该模板.它看起来像这样:

So above code can be placed as a template in your mind. On the other hand, we need another**.feature** file to call that template. And it is going to look like this:

Feature: call template feature.

背景:* 网址 SOAP_CREATE_SUBSCRIBER_HOST

Background: * url SOAP_CREATE_SUBSCRIBER_HOST

场景:使用 bulkusers 表创建默认用户

Scenario: Use bulkusers table to create default users

* table bulkusers
  | accountNo   | subsID         | mobile       | subsType |
  | '131451715' | '133451789134' | '5335167897' | 'asd'   |
  | '122452715' | '123452789124' | '5334287897' | 'qwe'  |
  | '124453715' | '123453789114' | '5334367817' | 'asd'   |

* def result = call read('user-create.feature') bulkusers
* def created = $result[*].response

* match result[*].__loop == [0, 1, 2]
* match created[*].name == $bulkusers[*].name

* def createUser = read('xxx')

这段代码实现的是,它用 user-create.feature 打包了 bulkusers 表,因此,user-create.feature 模板类被递归调用,直到表变量的数量以 bulkusers 变量结尾.

What this code achieve is, it is packing up the bulkusers table with user-create.feature, thus, user-create.feature template class being called recursively till the number of the table variables ends, with the bulkusers variables.

推荐答案

我在下面提供了一个简化的示例,但相信您会在这里找到问题的答案.使用 karate.set(varName, xPath, value) API 在空手道中循环数据和构建 XML 很容易:

I am providing a simplified example below, but am sure you will find the answers to your questions here. It is easy to loop over data and build XML in Karate using the karate.set(varName, xPath, value) API:

* table users
  | accountNo   | subsID         | mobile       | subsType  |
  | '113888572' | '113985218890' | '1135288836' | 'asd'     |
  | '113888573' | '113985218891' | '1135288837' | 'qwe'     |
  | '113888582' | '113985218810' | '1135288846' | 'asd'     |

* def xml = <users></users>
* def fun =
"""
function(u, i) {
  var base = '/users/user[' + (i + 1) + ']/';
  karate.set('xml', base + 'account', u.accountNo);
  karate.set('xml', base + 'mobile', u.mobile);
  karate.set('xml', base + 'type', u.subsType);
}
"""
* eval karate.forEach(users, fun)
* match xml ==
"""
<users>
  <user>
    <account>113888572</account>
    <mobile>1135288836</mobile>
    <type>asd</type>
  </user>
  <user>
    <account>113888573</account>
    <mobile>1135288837</mobile>
    <type>qwe</type>
  </user>
  <user>
    <account>113888582</account>
    <mobile>1135288846</mobile>
    <type>asd</type>
  </user>
</users>
"""

这篇关于如何为空手道特征使用动态值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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