Sourabh Chapali
Sourabh Chapali

Reputation: 391

Same asserts for every scenario can be put in a separate file to avoid duplication in karate?

Here are two scenarios , One after the other

Scenario: Positive - Create a discount with ABSOLUTE discount and ROOM_NIGHT_PRICE and search 

Given url baseUrl + SEARCH
And request changes
When method post
Then status 200 

And match $.data.hotels[0].transaction_discount.discounts[0].discount_id == discountId
And match $.data.hotels[0].transaction_discount.discounts[0].code == couponCode
And match $.data.hotels[0].transaction_discount.discounts[0].discount_value ==  incentive_value
And match $.data.hotels[0].transaction_discount.discounted_sell_price == (sellPrice-incentive_value)

Scenario: Positive - Create a discount with ABSOLUTE discount and TRANSACTION_PRICE and search 

Given url baseUrl + SEARCH
And request changes
When method post
Then status 200 

And match $.data.hotels[0].transaction_discount.discounts[0].discount_id == discountId
And match $.data.hotels[0].transaction_discount.discounts[0].code == couponCode
And match $.data.hotels[0].transaction_discount.discounts[0].discount_value ==  incentive_value
And match $.data.hotels[0].transaction_discount.discounted_sell_price == (sellPrice-incentive_value)

If you notice that assertions are same for these scenarios , I have similar 20 scenarios with exactly same assertions , Can I put it in a separate file to avoid duplication and easy to maintain ?

If Yes then how ? If No then is there any other way to avoid duplication in karate

Upvotes: 1

Views: 37

Answers (1)

Babu Sekaran
Babu Sekaran

Reputation: 4239

I don't see any change on your request either.

If only change in your scenarios are payload

You can try using Scenario Outline: and pass different payloads from Examples: table

Scenario Outline: Positive - Create a discount and search 
 Given url baseUrl + SEARCH 
 And request <changes> 
 When method post 
 Then status 200 
 And match $.data.hotels[0].transaction_discount.discounts[0].discount_id == discountId      
 And match $.data.hotels[0].transaction_discount.discounts[0].code == couponCode  
 And match $.data.hotels[0].transaction_discount.discounts[0].discount_value == incentive_value  
 And match $.data.hotels[0].transaction_discount.discounted_sell_price == (sellPrice-incentive_value)
Examples:
|  changes  |
|RNP_PAYLOAD|
|TXP_PAYLOAD|

you can create these payloads instances in Background:, this could help you avoid scenario duplication.

OR

If your intention is still to have this on a separate file

You can create a feature file which takes both expected and actual JSON as an input and perform match operation in it.

Then you can call that feature file in all of your scenarios passing the values to the calling feature.

Upvotes: 1

Related Questions