【问题标题】:Robot Framework - Looking for a keyword like "Run Keyword And Return Log"Robot Framework - 寻找像“运行关键字并返回日志”这样的关键字
【发布时间】:2021-07-02 09:35:13
【问题描述】:

我正在研究一个运行任务的机器人框架案例。当用户没有适当的权限时,不应完全执行此任务。但是,设置是这样的,在这种情况下任务仍然可以成功运行,因此“运行关键字并期望错误”中没有可以使用的错误消息。但是,关键字(日志级别=信息)下的日志告诉我“您无权执行 XYZ”。所以我需要在日志中查找这个字符串。

这似乎是一项简单的任务,但我似乎无法将此信息消息作为变量检索。

我想做类似的事情

*** Test Cases *** 

Check if user can execute task
    ${info_message}=   Run Keyword and Return Log    Run Task 123
    Should Contain    ${info_message}    You do not have permission

有没有人知道如何将此日志作为变量检索,以便我随后检查?谢谢!

【问题讨论】:

  • 您最好的方法(而且可能更容易)是使用库 RESTinstance 从 http 响应中获取该信息

标签: robotframework


【解决方案1】:

看看这是否解决了你的问题(它可能更简单,但它不会很有趣):

*** Test Cases ***
Check if user can execute task
    ${info_message}    ${status}=    Run Keyword and Return Log    Run Task 123
    Should Contain    ${info_message}    You do not have permission
    # Positive call
    ${info_message}    ${status}=    Run Keyword and Return Log    Run Task 123    sudo    Make me a sandwich
    Should Contain    ${info_message}    Here is your nice sandwich, Sir! ?

*** Keywords ***
Run Keyword and Return Log
    [Arguments]    @{keyword}
    ${status}    ${output}=    Run Keyword And Ignore Error    Run Keyword    @{keyword}
    Log    ${status}
    ${return code}=    Set Variable If    "${status}" == "PASS"    0    -1
    [Return]    ${output}    ${return code}

Run Task 123
    [Arguments]    @{command}
    @{command}=    Create List    @{command}    'none'
    Log Many    @{command}
    ${output}=    Set Variable If    "${command[0]}" == "sudo"    Here is your nice sandwich, Sir! ?\nI did not add mayonaise, as usual.    You do not have permission to execute task 123
    [Return]    ${output}

【讨论】:

  • 遗憾的是,这种方法不适用于 OP 要求的目标关键字的日志记录,而是其返回值。
  • 但是真正的 Run Task 123 会输出消息进行处理,对吧?
  • 它并没有这么说,而 OP 在问题中说 - “但是,关键字下的日志(日志级别 = 信息)告诉我”,“所以我需要寻找这个日志中的字符串。”
  • 是的,Todor 是对的。我的任务不输出消息(然后我可以将输出分配给一个变量并相应地使用这个变量),而只是显示我在日志中寻找的消息。但是感谢您的建议和对美味三明治的支持 =)
  • 我的困惑,是关于日志的来源。它来自机器人框架、库还是操作系统? (不是来自用户关键字,因为这很容易解决)
猜你喜欢
  • 2011-11-26
  • 2015-07-13
  • 2017-06-05
  • 2016-12-16
  • 2016-09-17
  • 2015-12-04
  • 2018-06-24
  • 1970-01-01
  • 2019-04-26
相关资源
最近更新 更多