Sum of two variables in RobotFramework

46.2k Views Asked by At

I have two variables:

${calculatedTotalPrice} = 42,42

${productPrice1} = 43,15

I executed

${calculatedTotalPrice}     Evaluate ${calculatedTotalPrice}+${productPrice1}

I got

42,85,15

How can I resolve it?

4

There are 4 best solutions below

0
On BEST ANSWER

By default variables are string in Robot. So your first two statements are assigning strings like "xx,yy" to your vars. Then "evaluate" just execute your statement as Python would do. So, adding your two strings with commas will produce a list:

$ python
>>> 1,2+3,4
(1, 5, 4) 

So you should use number variables using ${} and . (dots) for separator like in this example:

*** Test Cases ***
sum of variables
  ${calculatedTotalPrice} =    set variable    ${42.42}
  ${productPrice1} =    set variable    ${43.15}
  ${calculatedTotalPrice} =    Evaluate    ${calculatedTotalPrice}+${productPrice1}
  log to console  ${calculatedTotalPrice}

This will produce: $ pybot test.robot

==============================================================================
Test
==============================================================================
sum of variables                                                      ...85.57
==============================================================================
0
On

You can also use inline Python evaluation.

*** Variables ***
${calculatedTotalPrice}  ${42.42}
${productPrice1}         ${43.15}

*** Test Cases ***
Add two variables
    ${sum}  set variable  ${{ $calculatedTotalPrice + $productPrice1 }}
    should be equal as integers  ${sum}  ${85.57}

Check out the documentation here for more options.

0
On

Laurent's answer is almost always going to be the best course, but if for some reason you desire or require your Robot variables to be Strings that contain numbers, you can alternatively convert them to numbers inside of the Evaluate call:

*** Test Cases ***
Test1
    ${I1} =    set variable  10
    ${I2} =    set variable  5
    ${F1} =    set variable  42.42
    ${F2} =    set variable  57.15
    ${ISUM} =    Evaluate    int(${I1}) + int(${I2})
    ${FSUM} =    Evaluate    float(${F1}) + float(${F2})
    log to console  ${ISUM} ${FSUM}

This gives output:

Test1                                                                 ......15 99.57
2
On

the simplest way to add two variables in robotframework without the need to call keywords: you declare it in the VARIABLES sections

*** Variables ***
${A1}               ${1}
${A2}               ${2}
${A3}               ${${A1}+${A2}}

then the output of ${A3} is: 3