Passing defined variable through different scenari

2020-04-15 15:09发布

问题:

Trying to pass a reference of the response of a POST scenario to another scenario with PATCH method and changing one of the properties and getting errors. Debbuging the source code i found that when Scripts.assign is called for one scenario, the variable scope doesn't pass to the another scenario.

  • Karate version: 0.5.0
  • Java version: 1.8

Full feature of the test:

Feature: Products CRUD Test

Background: 
  * url demoBaseUrl

Scenario: fetch some products
  Given path 'products'
  When method get
  Then status 200
  And assert response.size() === 6

Scenario: add a product
  Given path 'products'
  And request {name: 'Iphone 7 Plus 128GB', description: 'Iphone 7 Plus Space Gray 128GB' }
  When method post
  Then status 200
  And match response contains { id: '#number', name: 'Iphone 7 Plus 128GB', description: 'Iphone 7 Plus Space Gray 128GB'}
  And def newProduct = response

Scenario: update a product
  Given path 'products'
  And def payload = {name: '#(newProduct.name)', description: '#(newProduct.description)'}
  And set payload $.id = #(newProduct.id)
  And match payload.id == (newProduct.id)
  And request payload
  When method patch
  Then status 200
  And match response contains {name: 'New Product Iphone 7'}

回答1:

This is by design - if you need variables to be in the scope for all Scenarios within a feature, move it to the Background. And if you want this variable to be initialized only once, look at the callonce keyword.



标签: java karate