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

时间:2018-12-03 10:11:01

标签: python pycharm robotframework

我的机器人文件中有两个关键字。第一种方法应该给我一个状态代码,第二种方法应该从上下文中获取此代码并将其与我给出的参数匹配。测试用例基本上检查api的状态代码是否为200,我将作为参数传递的状态代码为200。

我曾尝试将值提供为套件变量,然后使用“获取变量值”关键字来获取值。问题在于上述关键字的参数将给出“未找到变量定义”错误。请找到两种方法 The two methods where the variable has to be used

我不想从第一个关键字返回并给出第二个关键字的原因是因为我被告知要使用的测试用例的格式。

Test case

我会在Java中使用哈希表。我是机器人框架和pycharm的新手。有人可以帮我吗?

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

2 个答案:

答案 0 :(得分:0)

在我看来,这个问题的核心是间距。以下是您的问题的基本解决方案。在关键字名称中使用embedded arguments时,不应使用双精度空格,因为它们在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}

答案 1 :(得分:0)

如果要嵌入变量参数,则需要删除[Argument]。还要更改嵌入的变量名称,因为目前看起来字典值只是从字符串值传入的。

*** 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}

还可以考虑使用“设置测试变量”来减小变量的范围,否则可能会对其他测试产生不希望的影响(除非在套件级别需要它)。