创建字典并将其作为映射传递给 Robot Framework 中的 Java 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30306963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create a dictionary and pass it as a map to a Java method in Robot Framework
提问by Alex
I need to pass a java.util.Map to a a test method In the tsv file, I tried to create a dictionary like this:
我需要将一个 java.util.Map 传递给一个测试方法在 tsv 文件中,我尝试创建一个这样的字典:
${MYDICT} = Create Dictionary a 1 b 2
But I got the error:
但我得到了错误:
Setting variable '${MYDICT} = Create Dictionary a 1 b 2' failed: Variable name '${MYDICT} = Create Dictionary a 1 b 2' is invalid.
设置变量 '${MYDICT} = 创建字典 a 1 b 2' 失败:变量名 '${MYDICT} = 创建字典 a 1 b 2' 无效。
I declare the dictionary in the variables section like this:
我在变量部分声明字典是这样的:
${MYDICT}= Create Dictionary COUNTRY US CURRENCY_CODE USD
the test case is this:
测试用例是这样的:
testCase1 run the test using ${MYDICT}
and the test keywords are defined like this:
并且 test 关键字定义如下:
run the test using ${MAP}
call java method ${MAP}
and the Java method is:
和Java方法是:
public void CallJavaMethod(Map<String, String> map)
However, if I declare the dictionary in the Test keywords section, everything works fine and the Java method is called:
但是,如果我在 Test Keywords 部分声明字典,一切正常,Java 方法将被调用:
run the test using
${MYDICT}= Create Dictionary COUNTRY US CURRENCY_CODE USD
call java method ${MYDICT}
I do not understand why I must declare the dictionary at the point where I want to use it. What if I want to run that test case with different inputs?
我不明白为什么我必须在我想使用它的地方声明字典。如果我想用不同的输入运行那个测试用例怎么办?
Fix( used what @Uri and @Brian suggested) I try to use the "Set Suite Variable" like in this example:
修复(使用@Uri 和@Brian 建议的)我尝试使用“设置套件变量”,如本例所示:
*** Settings ***
Library Collections
Suite Setup Initialize dictionary
*** Keywords ***
Initialize dictionary
${dict}= Create Dictionary COUNTRY US
Set Suite Variable ${dict}
*** Test cases ***
testDict
Dictionary should contain item ${dict} COUNTRY US
And the test passes successfully.
并且测试成功通过。
回答by Bryan Oakley
I declare the dictinary in the variables section like this:
${MYDICT}= Create Dictionary COUNTRY US CURRENCY_CODE USD
我在变量部分声明字典是这样的:
${MYDICT}= Create Dictionary COUNTRY US CURRENCY_CODE USD
That's the problem. You can't call keywords like that in the variable section. The variable table is for defining static values.
那就是问题所在。您不能在变量部分中调用这样的关键字。变量表用于定义静态值。
From the robot framework user's guide section on variable tables:
来自关于变量表的机器人框架用户指南部分:
Their [variable table] main disadvantages are that values are always strings and they cannot be created dynamically.
它们的 [变量表] 主要缺点是值始终是字符串,不能动态创建。
If you want to create a dictionary that can be used in multiple tests, create it in a keyword and use the Set Suite Variablekeyword to make it available everywhere in the suite. You can call this keyword from a Suite Setup. Or, create it in a variable file.
如果要创建可在多个测试中使用的字典,请在关键字中创建它并使用Set Suite Variable关键字使其在套件中的任何地方都可用。您可以从套件设置中调用此关键字。或者,在变量文件中创建它。
Example
例子
The following example creates a suite-level variable named ${dict}
which contains two keys. The dictionary is initialized in a suite setup. There are two simple tests to verify that the dictionary was set up properly and is accessible to both tests.
以下示例创建了一个名为的套件级变量${dict}
,其中包含两个键。字典在套件设置中初始化。有两个简单的测试来验证字典是否正确设置并且两个测试都可以访问。
*** Settings ***
| Library | Collections
| Suite Setup | Initialize dictionary
*** Keywords ***
| Initialize dictionary
| | ${dict}= | Create Dictionary
| | ... | COUNTRY | US
| | ... | CURRENCY_CODE | USD
| | Set suite variable | ${dict}
*** Test cases ***
| Test A
| | Dictionary should contain item | ${dict} | COUNTRY | US
| Test B
| | Dictionary should contain item | ${dict} | CURRENCY_CODE | USD
回答by Uri Shtand
Where did you define the line?
你在哪里定义了这条线?
From the error message it looks like the line is not defined inside a keyword / test.
从错误消息看来,该行未在关键字/测试中定义。
Instead it looks like it's defined in the variables section of a resource file, or in a variable file.
相反,它看起来像是在资源文件的变量部分或变量文件中定义的。
You cannot use a keyword in the variables section.
您不能在变量部分使用关键字。
Instead, if you want the variable to be available for all the tests, you should define the variable in the suite-setup section as a suit variable
相反,如果您希望该变量可用于所有测试,您应该将套件设置部分中的变量定义为西装变量
*** Settings ***
Suite Setup Define map
*** Keywords ***
Define Map
${var}= Create Dictionary COUNTRY US CURRENCY_CODE USD
Set Suite Variable ${MYDICT} ${var}
The error that you got (no keyword COUNTRY) seems to say that you have an extra CRLF (enter) where there shouldn't be one.
您得到的错误(无关键字 COUNTRY)似乎表明您有一个额外的 CRLF(输入),而本不该出现。
The Create Dictionary keyword is deifned in the same line - and make sure to use double space (sometimes a single tab is considered as a single space instead of a double one)
Create Dictionary 关键字在同一行中定义 - 并确保使用双空格(有时单个选项卡被视为单个空格而不是双空格)