首页 > 解决方案 > Roboframework 用户关键字可以包含正则表达式吗?

问题描述

有一个关键字来测试类似的行为是很常见的,但语义略有不同,如下所示:

The ${element_a} (gets updated with reference|has a reference) to the ${element_b}

但是,我无法在任何地方找到此功能的文档。我知道其他用于测试的库,例如 Cucumber 通过正则表达式支持匹配步骤,我想因为机器人框架支持关键字名称中的嵌入参数,这也可以在机器人中工作。

有没有办法编写一次关键字并在稍微不同的情况下重新使用它?

标签: robotframework

解决方案


*** Keywords ***
The ${element_a} ${updated_or_has} to the ${element_b}

    IF    """${update_or_has}""" == 'gets updated with reference'
        Do Actions For Updated
    ELSE IF   """${update_or_has}""" == 'has a reference'
        Do Other Actions
    ELSE
         Fail    Unsupported value
    END

这是一种方法,根据关键字名称中包含的字符串来分支执行。
调用它The element_a gets updated with reference to the element_b将使关键字采用一条路径,而The element_y has a reference to the element_z- 另一条路径。

顺便说一句,您可能会重新考虑稍微更改动词,因为框架将很难区分变量中的子字符串${element_a}和输入的内容${updated_or_has}- 输入静态单词,这将是关键字名称的一部分并充当两个变量的分隔符 b/n。


根据评论,不需要分支,只需确认调用者使用了两个可能/支持的值之一。这可以通过以下检查来完成:

Run Keyword If    """${update_or_has}""".lower() not in ('gets updated with reference', 'has a reference', )    Fail    Unsupported value

推荐阅读