Sum of two variables in RobotFramework

33,935

Solution 1

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

Solution 2

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

Share:
33,935
buurkeey
Author by

buurkeey

Updated on July 09, 2022

Comments

  • buurkeey
    buurkeey almost 2 years

    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?

  • mercury
    mercury over 2 years
    It did not work in my context.
  • mercury
    mercury over 2 years
    Needs double curly brace: ${{${A1}+${A2}}}.