How to increment a variable each time a keyword is executed?

17,009 views
Skip to first unread message

B H

unread,
Jan 26, 2011, 12:14:27 PM1/26/11
to robotframework-users
Hi,

I am new in this tool and I am trying to automate my first test
script. Basically everything went fine except that I did not succeed
to implement a counter in case I want to run the same test n times.
I will try to explain it in more details:

- I would like to send 10 emails using ssh library and sender id
should be incremented by 1 each time an email is sent. The first email
should have -sender = 1, the second should have -sender = 2, the third
should have -sender = 3.....etc

- At the same time and each time an email is send, the corresponding
log file is accessed and greped for the -sender = id (which grows from
1 to 10 in my case)

My question is: How to make the -senderId grow by 1 for each emil
sent? As you can see I tried to put -sender= ${INTEGER} + 1 but it
did not work unfortunately.

Here is my commands:

Write ./mpRun -host=somehost.com -port=#### -iter=1 -threads=1 -
file=robotdemo.txt -charset_encoding=UTF8 -recpt=tes...@gmail.com -
sender= ${INTEGER} + 1

Repeat Keyword 9 times Write ./mpRun -host=somehost.com -port=####
-iter=1 -threads=1 -file=robotdemo.txt -charset_encoding=UTF8 -
recpt=tes...@gmail.com -sender= ${INTEGER} + 1

${out}= Execute Command grep ${INTEGER} /logs/TestLog.*



Thank you very much in advance.

Taylor, Martin

unread,
Jan 26, 2011, 1:47:10 PM1/26/11
to bha...@gmail.com, robotframework-users
Since ${INTEGER}+1 adds 1 to ${INTEGER} but doesn't change the value of ${INTEGER}, you can't do what you want using "Repeat Keyword", unless you wrap the following two steps in a new keyword. You could do that, or wrap them in a :FOR

New Keyword
Write ./mpRun -host=somehost.com -port= ... -sender= ${INTEGER} + 1
${INTEGER} = ${INTEGER} + 1

Repeat Keyword 9 times | New Keyword


Hope this helps!
Martin

> --
> You received this message because you are subscribed to the
> Google Groups "robotframework-users" group.
> To post to this group, send email to
> robotframe...@googlegroups.com.
> To unsubscribe from this group, send email to
> robotframework-u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/robotframework-users?hl=en.
>
>

Pekka Klärck

unread,
Jan 29, 2011, 6:36:27 AM1/29/11
to bha...@gmail.com, robotframework-users
2011/1/26 B H <bha...@gmail.com>:

>
> I am new in this tool and I am trying to automate my first test
> script. Basically everything went fine except that I did not succeed
> to implement a counter in case I want to run the same test n times.

Welcome aboard! Nice to hear things have worked pretty smoothly.

> I will try to explain it in more details:
>
> - I would like to send 10 emails using ssh library and sender id
> should be incremented by 1 each time an email is sent. The first email
> should have -sender = 1, the second should have -sender = 2, the third
> should have -sender = 3.....etc
>
> - At the same time and each time an email is send, the  corresponding
> log file is accessed and greped for the -sender = id (which grows from
> 1 to 10 in my case)
>
> My question is: How to make the -senderId grow by 1 for each emil
> sent?  As you can see I tried to put -sender= ${INTEGER} + 1  but it
> did not work unfortunately.

If you use `${INTEGER} + 1` in your test data, that `+ 1` is just
considered a static string that is catenated with the value of
`${INTEGER}`. If `${INTEGER}` is defined as a number, not string, you
could use `${INTEGER + 1}` which uses the so called extended variable
syntax [1] to get a value that is one bigger than variable alone. This
would not, however, increment the original value of the variable.

To increment the value you need to set it again. For that all of the
below approaches ought work. Notice that he second works even if the
variable type is originally a string, but the new value will be an
integer. The last one is handy if you need to make the new value
visible also outside the keyword where you use it.

${INTEGER} = Set Variable ${INTEGER + 1}
${INTEGER} = Evaluate ${INTEGER} + 1
Set Test Variable ${INTEGER} ${INTEGER + 1}

Finally, it might be easier to use `for in range` loop in your case
instead of `Repeat Keyword`.

[1] http://robotframework.googlecode.com/svn/tags/robotframework-2.5.5/doc/userguide/RobotFrameworkUserGuide.html#extended-variable-syntax
[2] http://robotframework.googlecode.com/svn/tags/robotframework-2.5.5/doc/userguide/RobotFrameworkUserGuide.html#for-in-range

Cheers,
.peke
--
Agile Tester/Developer/Consultant :: http://eliga.fi
Lead Developer of Robot Framework :: http://robotframework.org

Reply all
Reply to author
Forward
0 new messages