简体   繁体   English

需要一种在一个关键字中设置变量并在另一个关键字中访问它而又不返回机器人框架中变量的方法

[英]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.

相关问题 从一个关键字访问另一个机器人框架的变量 - Access a variable from one keyword to another Robot framework 使用变量从另一个文件调用关键字的关键字 - Robot Framework - Keyword calling a keyword from another file with variable - Robot Framework 在机器人框架的变量部分动态设置变量 - Dynamically set a variable in variables section of robot framework 在 Robot 框架中将变量从一个测试用例传递到另一个测试用例(不使用全局变量) - Pass variables from one test case to another in Robot framework (Not using global variable) 无法使用“AND”条件为机器人框架中的多个变量设置套件变量 - Unable to Set Suite Variable for multiple variables in robot framework using "AND" condition Robot Framework-将列表变量作为关键字参数传递 - Robot Framework - Passing List variables as keyword argument 有没有办法访问作为参数传递给机器人框架的 YAML 变量文件的名称? - Is there a way to access the name of the YAML variable file passed as an argument to robot framework? 有没有办法将 Robot Framework 中的变量分配给 python 而不使用它作为参数? - Is there a way to assign variable in Robot Framework to python without using it as an argument? 在机器人框架内定义包含变量的关键字 - Define a keyword containing a variable within robot framework 有没有一种方法可以将嵌入参数和普通参数一起传递给机器人框架中的一个关键字? - Is there a way to pass embedded arguments and normal arguments together in one keyword in robot framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM