Passing defined variable through different scenarios is not working

为君一笑 提交于 2019-12-02 02:24:01

问题


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.



来源:https://stackoverflow.com/questions/45448351/passing-defined-variable-through-different-scenarios-is-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!