You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Robot Framework:如何使用带命名参数的Run Keyword

问题场景

用Robot Framework做网页功能测试时,封装了一个关键字,通过Run Keyword and Return Status执行网页检查,同时临时禁用SeleniumLibrary的Register Keyword to Run on Failure(默认触发截图),避免检查失败时生成大量冗余截图。

原Robot文件实现如下,但无法支持timeout=20这类命名参数——因为@{args}无法识别字符串中的“=”,无法解析命名参数:

Library    BuiltIn
Library    SeleniumLibrary

Get Status From Keyword
    [Documentation]    This will prevent Run Keyword And Return Status from triggering a screenshot if it returns false
    ...                This should help to reduce file bloat in test case results
    [Arguments]    ${kw}    @{args}

    ${failureKeyword}=    Register Keyword To Run On Failure    NONE

    ${status}=    Run Keyword And Return Status    ${kw}    @{args}
    Register Keyword To Run On Failure    ${failureKeyword}

    [Return]    ${status}

尝试用Python实现**kwargs支持命名参数,但导入和调用存在问题:

# These imports are almost definitely not right this is just my best guesswork at their location
from SeleniumLibrary.keywords.runonfailure import runonfailure
from robot.libraries.BuiltIn import BuiltIn

def Get_Status_From_Keyword(name, **args):
    '''This will prevent Run Keyword And Return Status from triggering a screenshot if it returns false
    This should help to reduce file bloat in test case results'''

    failureKeyword = selenium.register_keyword_to_run_on_failure(NONE)

    status = BuiltIn.run_keyword_and_return_status(name, args)
    selenium.register_keyword_to_run_on_failure(failureKeyword)

    return status

需要解决:在Robot文件或Python代码中,如何正确调用带命名参数的Run Keyword?


解决方案

一、改进Robot文件版本(支持位置+命名参数)

Robot Framework支持同时用@{args}接收位置参数、&{kwargs}接收命名参数,修改关键字的参数定义即可兼容两种参数类型:

Library    BuiltIn
Library    SeleniumLibrary

Get Status From Keyword
    [Documentation]    执行指定关键字并返回状态,同时临时禁用失败截图以减少冗余文件
    ...                支持位置参数和命名参数(如timeout=20)
    [Arguments]    ${kw}    @{args}    &{kwargs}

    # 保存原失败处理关键字并临时禁用
    ${failureKeyword}=    Register Keyword To Run On Failure    NONE

    # 同时传入位置参数和命名参数执行目标关键字
    ${status}=    Run Keyword And Return Status    ${kw}    @{args}    &{kwargs}

    # 恢复原失败处理关键字
    Register Keyword To Run On Failure    ${failureKeyword}

    [Return]    ${status}

调用示例

${result}=    Get Status From Keyword    Click Element    ${button_locator}    timeout=10

二、正确的Python实现版本

修正导入和调用逻辑,通过Robot Framework的API正确获取库实例、处理参数:

from robot.libraries.BuiltIn import BuiltIn

def get_status_from_keyword(kw_name, *args, **kwargs):
    """执行指定关键字并返回状态,临时禁用Selenium的失败截图以减少冗余文件
    支持位置参数和命名参数"""
    builtin = BuiltIn()
    selenium = builtin.get_library_instance("SeleniumLibrary")

    # 保存原失败处理关键字并临时禁用
    original_failure_kw = selenium.register_keyword_to_run_on_failure("NONE")

    # 执行目标关键字,同时传入位置参数和命名参数
    status = builtin.run_keyword_and_return_status(kw_name, *args, **kwargs)

    # 恢复原失败处理关键字
    selenium.register_keyword_to_run_on_failure(original_failure_kw)

    return status

调用说明

  1. 将该Python文件保存为CustomKeywords.py,在Robot测试文件中导入:
    Library    CustomKeywords.py
    
  2. 调用方式和Robot版一致:
    ${result}=    Get Status From Keyword    Click Element    ${button_locator}    timeout=10
    

内容的提问来源于stack exchange,提问作者remington howell

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 10:17:36