如何为空手道特征使用动态值



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

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

但不管我怎么努力,我都做不到。我认为我应该分享我尝试使用的代码部分,以便开始讨论。

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

<?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>

正如您所看到的,我有一些将从外部给出的变量,它们是:accountNosubsIDsubTypemobile

现在,我有了一个特性文件,在该文件中,我使用上面的文件来调用SOAP服务。我正在为请求的所有变量分配新值,这样我就可以一直创建新用户。

以下是示例:

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文件。

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

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

提前谢谢。

EDIT:我知道我需要使用table或json、csv或excel类型的数据持有者来晚些时候使用它,所以下面是我的用户表。只是不知道如何在我的功能文件中实现它,这样它就会创建太多用户。

* 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文件,用于放置创建方法以启动单用户创建机制。它看起来是这样的:

@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**文件来调用该模板。看起来是这样的:

Feature: call template feature.

背景:*url SOAP_CREATE_SUBSCRIBER_HOST

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

* 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')

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

我在下面提供了一个简化的示例,但相信您会在这里找到问题的答案。使用karate.set(varName, xPath, value)API:可以轻松地在Karate中循环数据和构建XML

* 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>
"""

最新更新