user10028942
user10028942

Reputation:

How to test if a string variable in Robot Framework is empty?

How to test if a string variable in Robot Framework is empty?

My first naïve attempt looked like this:

Run Keyword If  ${myVar}!=${EMPTY}

but it failed:

Evaluating expression '!=' failed: SyntaxError: unexpected EOF while parsing (, line 1)

I then found this issue at Github but it didn't suggest a solution, just that the error message was unclear. An alternative solution was presented here:

${length}=    Get Length    ${Portfolio_ste}
Run Keyword If    ${length}    Go To Edit Portfolio

but is this really the best practice?

(The context is that I use a variable argument list and if a certain variable contains a value something should be done, otherwise just ignore it)

Upvotes: 6

Views: 46572

Answers (3)

user8128167
user8128167

Reputation: 7696

Byran's answer is good for this specific case but here are also some more general helpful commands to check for empty string:

Should Not Be Equal  ${EMPTY}  ${myVar}
Should Not Be Empty  ${myVar}

See https://robotframework.org/robotframework/latest/libraries/BuiltIn.html

Upvotes: 0

MarkHu
MarkHu

Reputation: 1869

Try Get Variable Value. It solved my problem.

Upvotes: -1

Bryan Oakley
Bryan Oakley

Reputation: 386342

The expression needs to be a valid python expression after variable substitution. Assuming for the moment that myVar might be something like the number 42, your expression would end up looking like this after substitution:

Run Keyword if  42!=

When comparing against the empty string you need to add quotes to guarantee that the expression is a proper python expression after substitution. For example:

Run Keyword If  "${myVar}"!="${EMPTY}"

Upvotes: 10

Related Questions