Robotframework Roboframework用户关键字能否包含Regexp?

Robotframework Roboframework用户关键字能否包含Regexp?,robotframework,Robotframework,通常会有一个关键字测试类似的行为,语义稍有不同,如: The ${element_a} (gets updated with reference|has a reference) to the ${element_b} 但是,我在任何地方都找不到这个特性的文档。我知道其他用于测试的库,比如Cucumber,通过regexp支持匹配步骤,我想既然robotframework支持在关键字名称中嵌入参数,那么它也可以在robot中工作 有没有办法只写一次关键字,然后在稍微不同的情况下重复使用 ***

通常会有一个关键字测试类似的行为,语义稍有不同,如:

The ${element_a} (gets updated with reference|has a reference) to the ${element_b}
但是,我在任何地方都找不到这个特性的文档。我知道其他用于测试的库,比如Cucumber,通过regexp支持匹配步骤,我想既然robotframework支持在关键字名称中嵌入参数,那么它也可以在robot中工作

有没有办法只写一次关键字,然后在稍微不同的情况下重复使用

*** 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
这是一种方法,根据关键字名称中的字符串来分支执行。
调用它时,
元素a参照元素b更新
将使关键字采用一条路径,而
元素y参照元素z
——另一条路径

顺便说一句,您可能会重新考虑稍微更改措辞,因为框架将很难区分变量
${element\u a}
中的子字符串和
${updated\u或}
中的子字符串-放一个静态单词,这将是关键字名称的一部分&充当两个变量的分隔符


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

Run Keyword If    """${update_or_has}""".lower() not in ('gets updated with reference', 'has a reference', )    Fail    Unsupported value
这是一种方法,根据关键字名称中的字符串来分支执行。
调用它时,
元素a参照元素b更新
将使关键字采用一条路径,而
元素y参照元素z
——另一条路径

顺便说一句,您可能会重新考虑稍微更改措辞,因为框架将很难区分变量
${element\u a}
中的子字符串和
${updated\u或}
中的子字符串-放一个静态单词,这将是关键字名称的一部分&充当两个变量的分隔符


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

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

可以使用正则表达式指定参数。所以,你们可以把你们想要匹配的部分当作一个论点来对待

来自官方用户指南,标题为:

自定义嵌入参数正则表达式是在参数的基名称之后定义的,因此参数和regexp用冒号分隔

在您的特定情况下,它可能看起来像这样:

The ${element_a} ${foo:gets updated with reference|has a reference} to the ${element_b}
${foo}
将是一个变量,它包含您可以忽略的任何小部分

以下是一个工作示例:

*** Keywords ***
The ${element_a} ${foo:gets updated with reference|has a reference} to the ${element_b}
    log  the foo argument is '${foo}'

*** Test Cases ***
Example
    The foo gets updated with reference to the bar
    The bar has a reference to the foo
    # verify that the keyword only matches those two variations
    Run keyword and expect error  
    ...  No keyword with name 'The something blah blah to the yada' found.
    ...  The something blah blah to the yada

可以使用正则表达式指定参数。所以,你们可以把你们想要匹配的部分当作一个论点来对待

来自官方用户指南,标题为:

自定义嵌入参数正则表达式是在参数的基名称之后定义的,因此参数和regexp用冒号分隔

在您的特定情况下,它可能看起来像这样:

The ${element_a} ${foo:gets updated with reference|has a reference} to the ${element_b}
${foo}
将是一个变量,它包含您可以忽略的任何小部分

以下是一个工作示例:

*** Keywords ***
The ${element_a} ${foo:gets updated with reference|has a reference} to the ${element_b}
    log  the foo argument is '${foo}'

*** Test Cases ***
Example
    The foo gets updated with reference to the bar
    The bar has a reference to the foo
    # verify that the keyword only matches those two variations
    Run keyword and expect error  
    ...  No keyword with name 'The something blah blah to the yada' found.
    ...  The something blah blah to the yada

谢谢@todor,在我的例子中,有趣的是我希望在两种情况下都有完全相同的行为,我只是在寻找一个关键字的定义,它将匹配“两个名称”,而不是一个是的,明白了,然后你就不分支,不管发送了什么,都执行步骤:)但是如果我只想匹配两种情况呢?传统的regexp blabla | bloblo是我的sue case。我在原始答案中指出,注释会弄乱格式。在
IF
语句中,您可以使用
$update\u或_has
,而不是使用三重引号,这稍微更健壮,更易于阅读。谢谢@todor,在我的例子中,有趣的是,我希望在这两种情况下都有完全相同的行为,我只是在寻找一个关键字的定义,它将匹配“两个名称”而不是一个是的,明白了,然后你只是不分支,不管发送了什么,都执行步骤:)但是如果我只想匹配两种情况呢?传统的regexp blabla | bloblo是我的sue case。我在原始答案中指出,注释会弄乱格式。在
IF
语句中,您可以使用
$update\u或_has
,而不是使用三重引号,这稍微更健壮,更易于阅读。