Is there any way to change the variable during the run-time, if the variable has undeclared variable(s)?

4,664 views
Skip to first unread message

wu feng

unread,
Aug 15, 2011, 8:55:25 PM8/15/11
to robotframework-users
Variable Value
${a} a+'\${b}'

test ${b}= Set Variable b
${c}= Get Variable Value ${a}
log ${c}

i expect that variable c is 'a+b', but the result still is 'a+${b}'
Is there any way to change the variable during the run-time if the
variable has undeclared variable(s)?

Using user keyword can solve the issue. but i think maybe there are
some better solutions. Could you give me some suggestion? Thanks a lot


Bulkan

unread,
Aug 16, 2011, 12:47:11 AM8/16/11
to mrbi...@gmail.com, robotframework-users
Hi,

Can you not do something like the following 

*** Variables ***
${b}  b
${a}  a + ${b}

*** Test Cases ***
My Test Case
     ${c}  Set Variable  ${a}
     Log  ${c}

My Other Test Case
     ${a}  Set Variable  "Value with quotes"
     Log   ${a}


If you put quotes around variable values, Robot Framework will assign them to them to variable. In your example that is why you're getting;

a+'\${b}'

instead of;

a+b

Hope that helps.

---


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


Mikko Korpela

unread,
Aug 16, 2011, 2:39:08 AM8/16/11
to mrbi...@gmail.com, robotframework-users
Hello,

I'm assuming now that you are expecting similar behavior as in Excel:
Cell value is a function that changes whenever the argument values
change.
If this is not what you are expecting then ignore this explanation.

Robot doesn't work like Excel. The variables work the way they work in
most programming languages. In other words what you write in to the
variable table cell as a value is the variables value only at the
beginning -- it will not be read again during the test execution.

To change a variables value you can do this:
*** Variables ***
${a} ${1} # Initializes ${a} to number 1

*** Test Cases ***
Test
Log ${a} # on this line ${a} is 1
${a}= Set Variable ${a+3} # After this line ${a} is ${value of
'a' before this row + 3} == ${1+3} == ${4} == number 4
Log ${a} # on this line ${a} is 4

The other thing worth of noting is that you can't use variables before
they have been defined:

In your example ${b} is not defined when a + '\${b}' is set as value of ${a}.
Also the '\' will escape '$'-sign so this will never use the value of
${b} and will just write 'a+${b}'.

Kind regards,
Mikko Korpela

2011/8/16 wu feng <mrbi...@gmail.com>:

wu feng

unread,
Aug 16, 2011, 4:30:16 AM8/16/11
to robotframework-users
Thanks for Mikko explain.
maybe the example makes you misunderstanding.
I want to declare all variables in a variable file, and import it to a
test suite
i think it will make the scripts maintain easily.
in some scenarios, I want to define a variable with a variable at
least
,and i hope that get the real value during the running time.
for example: ${url}=http://www.test.com/?param1=${value1}&amp;param2=$
{value2}...
i want to set the ${value1} or ${value2} with different values in
some test cases
currently, i only define a user keyword and using the arguments to do
it.
I expect RF allow me to pre-declare the variable.Whether RF can
Enhancement it?

Thanks
-Feng
> 2011/8/16 wu feng <mrbigh...@gmail.com>:

sandeep s

unread,
Aug 16, 2011, 4:35:57 AM8/16/11
to mrbi...@gmail.com, robotframework-users
Hi Weng,
You can create a resource file and use all the variables inside one file and keep calling it wherever /when ever needed.
We follow a practice where testdata,object repositories and other runtime informations(keywords) are kept in resources and called whenever needed.
the other way we do calling runtime data is : made a keyword(java) and ask this function to retrevie all the information on the rumtime and store it back to the variable.

Sandeep

wu feng

unread,
Aug 16, 2011, 5:11:11 AM8/16/11
to robotframework-users
Thanks Sandeep. you are right,the same as I do. maybe it's the best
way.:)

On Aug 16, 4:35 pm, sandeep s <sandis...@gmail.com> wrote:
> Hi Weng,
> You can create a resource file and use all the variables inside one file and
> keep calling it wherever /when ever needed.
> We follow a practice where testdata,object repositories and other runtime
> informations(keywords) are kept in resources and called whenever needed.
> the other way we do calling runtime data is : made a keyword(java) and ask
> this function to retrevie all the information on the rumtime and store it
> back to the variable.
>
> Sandeep
>

> >http://groups.google.com/group/robotframework-users?hl=en.

Pekka Klärck

unread,
Aug 16, 2011, 5:31:32 AM8/16/11
to mrbi...@gmail.com, robotframework-users
2011/8/16 wu feng <mrbi...@gmail.com>:

> I want to define a variable with a variable at least
> ,and i hope that get the real value during the running time.
> for example: ${url}=http://www.test.com/?param1=${value1}&amp;param2=$
> {value2}...
> i want to set the ${value1} or ${value2}  with different values in
> some test cases

There's BuiltIn keyword 'Replace Variables' that ought to work in this
case. Assuming you have ${url} variable with the above shown content,
and variables ${value1} and ${value2} have some value, you can use it
like:

| ${url2} = | Replace Variables | ${url} |

Notice that if you set different values for ${value1} and ${value2},
the result you get from 'Replace Variables' also changes.

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