karate - How to set specific values in a feature f

2019-06-01 19:59发布

问题:

This is my framework structure currently

CreateDiscount.feature
Discount_Payload.json
SearchDiscount.feature
Search_Payload.json

This is how the code in each file looks CreateDiscount.feature

Scenario: Create a discount
* def changes = read('Discount_Payload.json')
* set changes.code = cu.getCouponCode(4)
* set changes.operator = 'LT'
Given url baseUrl + CREATE_DISCOUNT
And request changes
When method post
Then status 200

Discount_Payload.json

{
"code": "ND12", 
"name": "NDS coupon Testing",
"description": "NDS coupon testing via postman",
"operator": "EQ"
}

SearchDiscount.feature

Scenario: Create a discount and search it
* def createDiscount = call read('CreateDiscount.feature') 
* print createDiscount
* def coupon_code = createDiscount.changes.code
* print coupon_code
* def changes = read('Search_Payload.json')
* set changes.coupon_code = coupon_code
Given url baseUrl + SEARCH
And request changes
When method post
Then status 200

Search_Payload.json
{
 "mode" : "Browse",
 "coupon_code" : "abrakadabra"
}

So currently whenever I run SearchDiscount.feature , It internally calls CreateDiscount.feature , This is fine if I have only code and operator values to be set while creating discount.

But now when I want to run separate test case in SearchDiscount.feature for which I want to update the value of name and description of Discount_Payload.json also from SearchDiscount.feature.

回答1:

You can pass values to feature file that you are calling

Instead of reading a json in you feature which you want to reuse, pass that json in a variable as an input to your feature.

SearchDiscount.feature

* def discountInput = read('Discount_Payload.json')
* def createDiscount = call read('CreateDiscount.feature' ) {'changes' :'#(discountInput)'}

Now you don't need to read for input in your CreateDiscount.feature

Delete

 * def changes = read('Discount_Payload.json')

from your CreateDiscount.feature

you can change whatever values you want in your calling feature before passing it to your reusable feature.

EDIT: If you want to run this for different values you can use scenario outline and set values to your discountInput before passing it to your feature.

please refer karate documentation.

https://github.com/intuit/karate#data-driven-features

EDIT 2: Scenario Outline sample for your question.

Scenario Outline:
    * def discountInput = read('Discount_Payload.json')
    * set discountInput.name = <name>
    * set discountInput.description = <description>
    * def createDiscount = call read('CreateDiscount.feature' ) {'changes' :'#(discountInput)'}
Examples:
    |   name  |   description  |
    |   'A'   |      'Ades'    |
    |   'B'   |      'Bdes'    |

now the scenario will run with these 2 set of values provided in the examples.



标签: karate