【问题标题】:Need a way to set a variable in one keyword and access it in another without returning the variables in robot framework需要一种在一个关键字中设置变量并在另一个关键字中访问它而不返回机器人框架中的变量的方法
【发布时间】:2019-05-04 14:45:39
【问题描述】:

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

我曾尝试将值作为套件变量提供,然后使用 Get Variable Value 关键字来获取该值。问题在于上述关键字的参数将给出“未找到变量定义”错误。请找出这两种方法

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

我会在 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

【问题讨论】:

  • 您在寻找python dicts 吗?这些是关联数组/哈希表的python等价物
  • 不是专门的python dicts。任何预先存在的关键字可能吗?如果它们不可用,则使用哈希图。我实际上想一步完成。因此,欢迎所有回复
  • 请不要使用代码截图,因为它们会阻止想要复制您的问题的人复制和粘贴。

标签: python pycharm robotframework


【解决方案1】:

在我看来,这个问题的核心是间距。以下是您问题的基本解决方案。在关键字名称中使用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}

【讨论】:

  • 我不知道为什么,间距不能解决我的问题。我已复制代码供您参考
  • 我不知道你想说什么。我假设当您将示例复制到单独的机器人文件中并运行它时,您应该获得通过的测试。如果不是,请用观察到的结果发表评论。
  • 嗨。有效!我很困惑,因为悬停时出现“未找到变量定义”错误,但是当我运行它时,它起作用了。一个疑问,关键字的参数应该分开一个空格还是两个?当我在关键字调用和关键字中给它两个空格时,我得到“关键字”不能为空错误。但是,当我给它一个空格时,就像上面的代码一样,我没有收到错误。非常抱歉!我得到了基本的怀疑!
【解决方案2】:

如果要嵌入可变参数,则需要删除 [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}

还可以考虑使用“设置测试变量”来降低变量的范围,否则您可能会对后续的其他测试产生一些不良影响(除非您在套件级别需要它)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-02
    • 1970-01-01
    • 2017-11-11
    • 1970-01-01
    • 2021-04-16
    • 2019-04-09
    • 1970-01-01
    • 2020-08-30
    相关资源
    最近更新 更多