Declare Global variables in background of feature-karate Soap API

时间秒杀一切 提交于 2020-03-23 17:19:30

问题


I am using karate for SOAP API services. How can I declare a global variable ?? as you can see in below feature file? after print response I have variable name def ourSession = /Envelope/Body/LoginReturn I am using this variable (ourSession) in next scenario but it's failing ? but it's not global. So how can I declare "ourSession" as a global variable ?? so I can use into other scenarios? Any help will be highly appreciated. Please have a look of my feature file below;

#
Feature: 
    SOAP calls to the following service:

Background:

     * url baseUrl
     * def configSS = Java.type('practice.utilities.Shellscripts')

##################################### LOG IN #########################################################
@DataAcquisition
 Scenario: login
     Given request
     """
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsh="http://www.informatica.com/wsh">
   <soapenv:Header/>
   <soapenv:Body>
      <wsh:Login>
         <RepositoryDomainName>Domain_Aqr_Dev_Powercenter</RepositoryDomainName>
         <RepositoryName>Powercenter_Repository_Service</RepositoryName>
         <UserName>#(AM_USERNAME)</UserName>
         <Password>#(AM_PASSWORD)</Password>
         <UserNameSpace/>
      </wsh:Login>
   </soapenv:Body>
</soapenv:Envelope>

     """
     When soap action '/login'
     Then assert responseStatus == 200
     And print 'response: ', response
     * def ourSession = /Envelope/Body/LoginReturn
     * print ourSession

回答1:


i am using this variable (ourSession) in next scenario

You can't. There are many answers in Stack Overflow addressing this question, please find them and read them.

And please read the docs: https://github.com/intuit/karate#script-structure

To quote:

if you are expecting a variable in the Background to be modified by one Scenario so that later ones can see the updated value - that is not how you should think of them, and you should combine your 'flow' into one scenario.

There are many ways to "re-use" in Karate such as call. So you should be able to figure out how to do what you want.

Now if you really want a "global" variable, please use callonce that is most likely what you are looking for:




回答2:


Do not create a new scenario for the subsequent call. A single scenario can have multiple requests.



来源:https://stackoverflow.com/questions/59062583/declare-global-variables-in-background-of-feature-karate-soap-api

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