[英]Need a way to set a variable in one keyword and access it in another without returning the variables in robot framework
I have two keywords in my robot file. 我的机器人文件中有两个关键字。 The first method should give me a status code and the second should get this code from context and match it with the argument I have given.
第一种方法应该给我一个状态代码,第二种方法应该从上下文中获取此代码并将其与我给出的参数匹配。 The test case basically checks whether the status code from an api is 200 or not, the 200 which I will be passing as an argument.
测试用例基本上检查api的状态码是否为200,我将作为参数传递的200。
I had tried to give the value as suite variable and then use the Get Variable Value keyword to get the value. 我曾尝试将值提供为套件变量,然后使用“获取变量值”关键字来获取值。 The problem with this is the argument to the above keyword will give 'Variable definition not found' error.
问题在于上述关键字的参数将给出“未找到变量定义”错误。 Please find the two methods
请找到两种方法
The reason I do not want to return from the first keyword and give to the second is because of the format of test cases I am told to use. 我不想从第一个关键字返回并给出第二个关键字的原因是因为我被告知要使用的测试用例的格式。
I would have used a hashmap in Java. 我会在Java中使用哈希图。 I am new to robot framework and pycharm.
我是机器人框架和pycharm的新手。 Can someone help me out?
有人可以帮我吗?
I Post A POST Request
create session ${Post_Request.alias} ${Post_Request.session_url}
${headers} = create dictionary Accept=${Post_Request.Accept} Cache-Control=${Post_Request.Cache_Control} Content-Type=${Post_Request.Content_Type}
${params} = create dictionary grant_type=${Post_Request.grant_type} redirect_uri=${Post_Request.redirect_uri} client_id=${Post_Request.client_id} refresh_token=${Post_Request.refresh_token}
${resp} = POST REQUEST ${alias} ${Post_Request.uri} params=${params} headers=${headers}
log to console ${resp.json()}
set suite variable ${response_code} ${resp.status_code}
Verify The status Code ${resp.status_code}
[Arguments] ${resp.status_code}
${response_code} = Get Variable Value ${response_code}
log to console resp_code=${response_code}
should be equal as strings ${resp.status_code} ${response_code}
log to console Status code is 200
The core of this issue seems to me to be spacing. 在我看来,这个问题的核心是间隔。 Below is the essential solution of your problem.
以下是您的问题的基本解决方案。 When using embedded arguments in the keyword name there should be no double spaces as they have special meaning in RF.
在关键字名称中使用嵌入参数时 ,不应使用双精度空格,因为它们在RF中具有特殊含义。
*** Test Cases ***
Test Case
When I post A POST Request
Then verify the status code is 200
*** Keywords ***
I Post A POST Request
Set Suite Variable ${resp_code} 200
Verify the status code is ${status_code}
Should Be Equal As Strings ${resp_code} ${status_code}
You will need to remove the [Argument] if you want the variable parameter embedded. 如果要嵌入变量参数,则需要删除[Argument]。 Also change the embedded variable name, as at the moment it looks like a dictionary value passes in from just a string value:
还要更改嵌入的变量名称,因为目前看起来字典值只是从字符串值传入的:
*** Test Cases ***
Test Case
When I post A POST Request
Then verify the status code 200
*** Keywords ***
I Post A POST Request
&{resp}= create dictionary status_code=200
set suite variable ${resp.status_code}
Verify The status Code ${expected_response_code}
${actual_response_code} = Get Variable Value ${resp.status_code}
should be equal as strings ${expected_response_code} ${actual_response_code}
Also consider using "Set Test Variable" to lower the scope of the variable or you might have some undesired impact on other tests further down the line (unless you need it at suite level). 还可以考虑使用“设置测试变量”来减小变量的范围,否则可能会对其他测试产生不希望的影响(除非在套件级别需要它)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.