First of all, in most cases it is better to use the BuiltIn keyword
'Set Test Variable' in the test data to accomplish this. Setting the
variable can be wrapped into a user keyword in a resource file and you
can then distribute that resource file with rest of the
library/framework/toolset.
There might, of course, be reasons why to use the
BuiltIn.set_test_variable directly in your library. I haven't tried
that myself, but I'm quite sure that you should be using either
BuiltIn().set_test_variable('${var}', 'value')
or
BuiltIn().set_test_variable('$var', 'value')
The latter syntax is what the keyword expects, but the former might
also work. As I said, I haven't tried this myself.
Cheers,
.peke
---------- Forwarded message ----------
From: Pekka Klärck <pe...@iki.fi>
Date: 2008/12/9
Subject: Re: Set Test Variable from own library
To: xter...@o2.pl
2008/12/9 BS <xter...@o2.pl>:
I created following simple test library and after executing 'Set Var'
keyword both ${spam} and ${eggs} variables were available.
from robot.libraries.BuiltIn import BuiltIn
def set_var():
BuiltIn().set_test_variable('${spam}', 'eggs')
BuiltIn().set_test_variable('$eggs', 'spam')
In your earlier mail you mentioned something about Variables in
Setting table. If that means that you try to use variable files then
the above does not work for you since it is a test library. Variable
files could really be suitable for your case, though, and they are
very simple. The simplest variable file that would create same
variables as the above keyword is:
spam = 'eggs'
eggs = 'spam'
For more information about variable files, see
http://robotframework.googlecode.com/svn/tags/robotframework-2.0.3/doc/userguide/RobotFrameworkUserGuide.html#variable-files
Cheers,
.peke
Setting var.foo to new value doesn't have any effect because Robot
Framework has already read it's value into variable ${foo}. You are
just rebinding the value of 'foo' attribute to a new value but Robot
can't notice that. If the value would originally be a mutable object
(like a list or your custom object) you should be able to alter its
state using something like "var.foo.do_something('x')".
Using BuiltIn.set_xxx_variable is probably a better idea than altering
the state of the variable as it's more explicit. You can also remove
'var.foo = "spam"' line (as well as importing var) altogether and set
the variable like
BuiltIn().set_(global|suite|test)_variable('$foo', 'spam')
Cheers,
.peke
2008/12/9 Pekka Klärck <pe...@iki.fi>:
> 2008/12/9 BS <xter...@o2.pl>:
>>
>> How to get actual value (in library file of
>> course)? import var; var.foo is not the best option as you noticed
>> above.
>
> I would simply use the variable in the test data and then get the
> actual value into the library. There is no stable public API for
> getting variables, but if you really want you can see how the BuiltIn
> library gets the access. You may also want to add a star or otherwise
> comment this issue:
> http://code.google.com/p/robotframework/issues/detail?id=167
I was wrong, BuiltIn keyword Replace Variables can actually be used to
replace variables with their values, and BuiltIn.replace_variables
method thus works in libraries. The original use case for this keyword
was replacing variables in template files read from the system, but it
works pretty well in this situation too. The only limitation is that
it always returns values as a string, but I already opened a separate
issue [1] about removing that limitation.
[1] http://code.google.com/p/robotframework/issues/detail?id=175
Cheers,
.pkee
Cheers,
.peke
I agree that replace_variables is not the best method name to use
inside a library for this purpose, but Return Variable and Get
Variable wouldn't make sense as keywords since you can just use
variable syntax in the test data. If you are really bothered by the
not-exactly-optimal method name, you can simply rename it in your
library like
from robot.libraries.BuiltIn import BuiltIn
BuiltIn.get_variable = BuiltIn.replace_variables
Alternatively you can have a helper method like
def get_variable(string):
return BuiltIn().replace_variables(string)
Cheers,
.peke
We try not to modify keywords so that their functionality changes, and
if we would break some existing behavior we would most likely revert
back. It wouldn't help much if we had another method in the library
since we could accidentally break it too.
Cheers,
.peke
What do you mean with this? Are you trying to set new variables or
alter settings? Setting variables works with
set_test/suite/global_variable but changing settings is not supported.
Cheers,
.peke