希望这些示例在运行时足以显示问题。
在 @setup 使用中,我们希望用动态值(变量)替换字段。 虽然这在基本场景中有效,但在 @setup 块中使用会失败并出现如下错误。 这是预期的功能还是错误?
* replace testGenerator
| token |value! |
| replaceVar |replacementVar |
| replaceString |'replaced String' |
expression error (replace string values need to be within quotes): js failed:
>>>>
01: replacementVar
<<<<
org.graalvm.polyglot.PolyglotException: ReferenceError: "replacementVar" is not defined
- <js>.:program(Unnamed:1)
这里是一个代码示例,显示了在单一场景中工作的变量的替换,但在大纲和@setup用法中失败。
Feature: scenario outline using a dynamic generator function
Background:
# setting url
* url 'https://www.google.com'
* def replacementVar = 'this was a replaced variable'
* configure continueOnStepFailure = true
@setup
Scenario: setup of data
# creating json data and casting as string for replacement
* def testGenerator = [{ name: 'test replaceVar', replaceVar: '<replaceVar>'},{ name: 'test replaceString', replaceVar: '<replaceString>' }]
* print 'testGenerator-initial-',testGenerator
# printing current generated data value
* string testGenerator = testGenerator
* print 'testGenerator-string-',testGenerator
# replacing data in generated data
* replace testGenerator
| token | value! |
| replaceVar | replacementVar |
| replaceString | 'replaced String' |
# converting generated data back to json object for use in scenario
* json testGenerator = testGenerator
# printing updated generated data value
* print 'testGenerator-json-',testGenerator
Scenario Outline: Outline test name: ${name}
# printing initial name value
* print 'name-',name
# printing initial replaceVar value
* print 'replaceVar-',replaceVar
Examples:
| karate.setup().testGenerator |
Scenario: Scenario test
# creating json data and casting as string for replacement
* def testGenerator = [{ name: 'test replaceVar', replaceVar: '<replaceVar>'},{ name: 'test replaceString', replaceVar: '<replaceString>' }]
* print 'testGenerator-initial-',testGenerator
# printing current generated data value
* string testGenerator = testGenerator
* print 'testGenerator-string-',testGenerator
# replacing data in generated data
* replace testGenerator
| token | value! |
| replaceVar | replacementVar |
| replaceString | 'replaced String' |
# converting generated data back to json object for use in scenario
* json testGenerator = testGenerator
# printing updated generated data value
* print 'testGenerator-json-',testGenerator
抱歉,
@setup
很特殊,根本不可能进行占位符或变量替换。