Edmondo
Edmondo

Reputation: 20080

Can Roboframework user keyword contain Regexp?

It is very common to have a keyword that tests a similar behaviour, with a slightly different semantic like so:

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

However I wasn't able to find this feature documented anywhere. I know other libraries for testing such as Cucumber support matching steps through regexp, and I imagine since robotframework support embedded arguments in keyword names this can also work in robot.

Is there a way to write a keyword once and re-use it in slightly different situations?

Upvotes: 1

Views: 274

Answers (2)

Bryan Oakley
Bryan Oakley

Reputation: 386010

You can specify an argument with a regular expression. So, you could treat part you want to match against as if it was an argument.

From the official user guide, in a section titled Using custom regular expressions:

A custom embedded argument regular expression is defined after the base name of the argument so that the argument and the regexp are separated with a colon.

In your particular case it might look something like this:

The ${element_a} ${foo:gets updated with reference|has a reference} to the ${element_b}

${foo} will be a variable that has whatever that little part is, which you can just ignore.

Here is a working example:

*** 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

Upvotes: 2

Todor Minakov
Todor Minakov

Reputation: 20067

*** 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

That's one way to do it, branch the execution based on what string was part of the keyword name.
Calling it The element_a gets updated with reference to the element_b will have the keyword take one path, while The element_y has a reference to the element_z - a different one.

By the way, you might reconsider changing the verbing slightly, as the framework will have a bit hard time distinguishing what substring goes in the variable ${element_a} and what in ${updated_or_has} - put a static word, that'll be a part of the keyword name & act as a separator b/n the two vars.


Based on the comments, the branching is not needed, just a confirmation the caller used one of the two possible/supported values. This can be done with this check:

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

Upvotes: 3

Related Questions