A general solution is having a suitable library for that. In your case
you needed to have Divide keyword. Having Math library as a standard
library for Robot Framework would be pretty nice actually.
If you only need to do simple calculations and are OK with your test
data getting a bit more complicated, you may consider either of the
workarounds below.
1) Use built-in Evaluate keyword which takes a Python expression and
returns its result. It ought to be possible to do just:
| ${nbrPages} = | Evaluate | ${nmrRows} / 50 |
2) Use extended variable syntax. If ${nbrRows} is an integer, you
should be able to use ${nbrRows / 50} without even having ${nbrPages}.
See user guide for more information about the extended variable syntax
in general.
Cheers,
.peke
---------- Forwarded message ----------
From: Pekka Klärck <pe...@iki.fi>
Date: 2009/2/18
Subject: Re: Math functions?
To: claes...@gmail.com
2009/2/18 Claes <claes...@gmail.com>:
>
>> > Say for example I want to calculate ${nbrRows}/${50} and put it into $
>> > {nbrPages}. How can I do that?
>>
>> 1) Use built-in Evaluate keyword which takes a Python expression and
>> returns its result. It ought to be possible to do just:
>>
>> | ${nbrPages} = | Evaluate | ${nmrRows} / 50 |
>
> Evalute works. ${nbrPages} = 3.125. How can I round this up without
> creating my own keyword "Round Up"?
Python has built-in method 'round' but it rounds to nearest number,
and 'int' always rounds down. I don't know any simple way to round up
using Evaluate but this ought to work:
int(__import__('math').ceil(${nmrRows} / 50))
You should consider creating a higher level keyword for this one or
perhaps creating you own library. In a library you could just have:
import math
def round_up(number):
return int(math.ceil(float(number)))
Note that 'float' is only needed to handle possible strings containing numbers.
> Ps. The documentation on Evaluate could be updated with a link to a
> page where I could read more about how Python evaluates things. The
> text "Evaluates the given expression in Python and returns the
> results." is to thin for me as I don't know how to program Python. I.e
> I don't know what operators work. +, -, /, * is straight forward by
> does % work and is there more of them?
Yeah, we could add http://python.org there but I expect people can
actually found that or other Python resources easily if they want. If
you want to learn some Python I recommend the official tutorial and
http://diveintopython.org.
Cheers,
.peke
It would be possible, but then we probably needed to support all other
Python modules too. Instead you can use either __import__ function
with Evaluate keyword or have a simple library with this
functionality. There are examples of both in the mail I just forwarded
to the list.
Cheers,
.peke