2

I have read this question

How to test for blank text field when using robotframework-selenium?

as well as the two links to the Robot Framework documentation in the answers but I still don't get how to check if a variable is empty.

I want to do this

if var A equals var B then
   do something
else
   do something else

where A is a string that can both contain something as well as be empty and where B is empty or null.

2 Answers2

4

can be achieve using many different ways some are as follows, use whichever fits for you

  1. this way you can check two variables equals OR not

    Run Keyword If    '${A}'=='${B}'   do something    ELSE    do something
    
  2. this way you can check if both of your variable are None or not in one go

    Run Keyword If    '${A}'=='None' And '${B}'=='None'    do something
    
  3. using following also you can get if your variables are equal of not if both values are equal it will return true

    Should Be Equal    ${A}    ${B}
    
  4. if both values are NOT equal it will return true.

    Should Not Be Equal   ${A}    ${B}
    

for more information go through this docs

there is also ${EMPTY} variable in robot framework which you can use to check if variable is empty or not

Todor Minakov
  • 19,097
  • 3
  • 55
  • 60
Dev
  • 2,739
  • 2
  • 21
  • 34
  • 1
    So "Run keyword If '${A}'=='${EMPTY}' ? Why do you write this with apostrophes? –  Oct 24 '18 at 10:26
  • How do you combine example 3 and 4 with an if-clause? –  Oct 24 '18 at 10:28
  • 1
    @hensti for first query..... reason behind wrapping it in apostrophes because if your variable differs like A is int and B is string then it will treat both as string then it will perform operation, without apostrophes only similar datatype variables can be compared – Dev Oct 24 '18 at 11:07
  • @hensti for second query ....`Run Keyword If '${A}'!='${Empty}' And '${B}'!='${Empty}' Should Be Equal ${A} ${B}` likewise you can do for 4th as well – Dev Oct 24 '18 at 11:15
  • Hmm, I am not sure I follow your answer on my second query. `should be equal ${A} ${B}` then what? If the condition is satisfied or not? –  Oct 24 '18 at 12:21
  • it run only if both A and B are NOT empty, any one of them is empty then it wont check for equality, it wont run `should be equal ${A} ${B}` this keyword – Dev Oct 24 '18 at 12:48
  • Sorry, I was unclear. I meant how do you use Should (not) be Equal on its own? That is, (in pseudocode) `Should be equal ${A} ${B} then A+B else A*B`. No `Run Keyword If` involved. Sorry for the confusion. –  Oct 24 '18 at 13:38
  • @hensti the `Should Be Equal` keyword is an assert, it will pass if the 2 are equal, or fail if not, and that's it. Such branching "if a do X else Y" is not done with it, but with `Run Keyword If`, like in the first example of this answer. – Todor Minakov Oct 24 '18 at 18:49
0

Like this is working:

${aaax}=     set variable  aaa aa ba baavaa
${aaaxx}=    set variable  aaa aba baavaa
${aba}=      set variable  aba

${res1}=     run keyword and return status  should contain  ${aaax}     ${aba}
${res2}=     run keyword and return status  should contain  ${aaaxx}    ${aba}

log to console  ${EMPTY}
log to console  res1: ${res1}
log to console  res2: ${res2}
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
xrc
  • 52
  • 4