Cucumber 如何导出为全局变量

Cucumber 如何导出为全局变量,cucumber,bdd,cucumber-jvm,cucumberjs,karate,Cucumber,Bdd,Cucumber Jvm,Cucumberjs,Karate,我有一个空手道特征文件 Scenario: Feature Given url 'https://testlocal/v1/test/authorize' And request 'type=code&uri=http://www.testlocal/app' And header Accept = 'application/x-www-form-urlencoded' And header Content-Type = 'application/x-www-form-urlencod

我有一个空手道特征文件

Scenario: Feature

Given url 'https://testlocal/v1/test/authorize'
And request 'type=code&uri=http://www.testlocal/app'
And header Accept = 'application/x-www-form-urlencoded'
And header Content-Type = 'application/x-www-form-urlencoded'
When method POST
Then status 302
* def code = responseHeaders['Location'][0].substring(59,95)
* print code
Scenario: Login

Given url 'https://testlocal/v1/test/authorize/login'
And request 'username=test1@gmail.com&password=123!&requestId=**"I am not sure how to call the above code here"**
And header Accept = 'application/x-www-form-urlencoded'
And header Content-Type = 'application/x-www-form-urlencoded'
When method POST
Then status 302
当我运行上面的功能文件时,我可以看到单独打印的代码,但是我希望在同一个功能文件的下面使用该代码

Scenario: Feature

Given url 'https://testlocal/v1/test/authorize'
And request 'type=code&uri=http://www.testlocal/app'
And header Accept = 'application/x-www-form-urlencoded'
And header Content-Type = 'application/x-www-form-urlencoded'
When method POST
Then status 302
* def code = responseHeaders['Location'][0].substring(59,95)
* print code
Scenario: Login

Given url 'https://testlocal/v1/test/authorize/login'
And request 'username=test1@gmail.com&password=123!&requestId=**"I am not sure how to call the above code here"**
And header Accept = 'application/x-www-form-urlencoded'
And header Content-Type = 'application/x-www-form-urlencoded'
When method POST
Then status 302

谢谢

听起来您只需要一个
背景:
部分,请将此作为一个很好的重用示例:

如果确实需要跨多个要素文件重复使用,请参阅有关如何重复使用的文档

我想建议改进您的测试,您可以执行以下操作,而不是使用语法手动生成请求:

编辑:完整解决方案

Background:
Given url 'https://testlocal/v1/test/authorize'
And form field type = 'code'
And form field uri = 'http://www.testlocal/app'
When method post
Then status 302
And def code = responseHeaders['Location'][0].substring(59,95)

Scenario: login
Given path 'login'
And form field username = 'test1@gmail.com'
And form field password = '123!'
And form field requestId = code
When method post
Then status 302

不是wokring,@Peter Thomas
java.lang.RuntimeException:javascript评估失败:abc在com.intuit.karate.Script.evalInNashorn(Script.java:423)在com.intuit.karate.StepDefs.evalList(StepDefs.java:137)在com.intuit.karate.StepDefs.formField(StepDefs.java:225)在✽.表单字段requestId=abc(示例/users/users.feature:16)由以下原因引起:javax.script.ScriptException:ReferenceError:“代码”未在第1行定义。我提供的代码是一个最好的猜测,我不能说问题出在这里。一切都很好。目前,在一个
场景中包含所有步骤至少对你有用吗?使用
背景:
而不是
场景:
,因为第一个场景不起作用?啊,是的,背景很好(我一直在犯错误)非常感谢