How to assign global variable to another variable in another robot file

1

I have assigned global variable as ${googlesite} = http://google.com and I want to use this variable in another robot file under variable section as

robot1.robot

${googlesite} = http://google.com

*** keywords ***

set suite variable ${googlesite}

robot2.robot

*** Variables ***

${googlelogin} = ${googlesite}/login.html

*** Keywords ***
log to console ${googlesite}-- Its printing as [http://google.com]

log to console ${googlelogin}-- printing only ./login.html

(not appending ${googlesite} from variables section)

Probably best to use a resource or a variable file in this case: Robot Framework User Guide

Both your tests would import the resource file and be able to use the variables/methods within. This assumes the variable will remain the same throughout your tests.

I tried as resource import in robot2 still not working.

All files are in the same directory for this example:

globalVariables.resource

*** Variables ***
${googlesite}=     https://google.com

robot2.robot

*** Settings ***
Resource    globalVariables.resource 

*** Keywords ***
log to console    ${googlesite}

Thanks Brandon.

This is what I am looking for …

In robot2.robot
*** Settings ***
Resource globalVariables.resource

*** Variables ***
${Login} ${googlesite}/login.html
${logout} ${googlesite}/logout.html

*** Keywords ***
log to console ${Login}
log to console ${Logout}

Should display as https://google.com/login.html
https://google.com/logout.html

I kept resource files under resource folder and robot files in different folder

In that case depending on your file structure you will need to use a relative path for the resource import:

Unless I am misunderstanding and you are saying you have everything working now?

Currently I am using all global variables under keyword since its not working under variables. Pls see below code

globalVariables.resource

*** Variables ***
${googlesite}= https://google.com

In robot2.robot
*** Settings ***
Resource globalVariables.resource

*** Keyword ***
Set urls
Set Global Variable ${Login} ${googlesite}/login.html
Set Global Variable ${logout} ${googlesite}/logout.html

It works as expected.
You where however not using correct keywords syntax in your posts…
(Maybe just a mistake here…)

Robot Live Code Playground

May be I didn’t exact scenario. Please check here I updated the code.

https://robotframework.org/code/?codeProject=N4IgdghgtgpiBcIDCBXAzgFwPZRAGhABMY0BjAJwEsAHDSrMBEfEAM0oBsSEBtUdrgDlocRAGsYATwDuWcoTQA6ciSwpypOAVIMMMMBiYAqEwAIAyjAx0wAczSmTRgDpgASqvWbTPnwDcIKggAIy4lFTQ1DRhXVydTADVAyhCwxxNXABJgABksW0owAF9fH2z1DiKAeg58wsUACwwoDizgWts1DBLfcvJKmvyuxubWsDizAGkpWXkHJ1c821MoSQtKYjRXUqXTABUsUyQGSK5SvYbKBw6HDAaYUwxZUwCg0JJ4bd9dg6OTrDOvVydWKXx8P0OxzApweQI6XSKrhARTw-E4MGEsCYrxS73CnmiLB0Bn0hkQ8SSbzSCzA2TQlD0PQA4lh8lw2hVEeNufFpjI5Ap0i4wJYMKZ6XowaYAJIAMVMdIZMB6PgAvKrTAByFlsmCaqWlPqVUqixLJVKw0q+JoYahoeBVKqdXWKHRQKUAURy5g9MvlQIlytK6q1AE0IA1WfqwFbDcBObGLFYzVTLT4bXaHVVJBHWa6cJ7BAARKWmpm1YIQDgp3GAsrx-pFZEAXQIxCofhghAACuQsAArGCkMkYcgoZVAA

Still it works, when you fixed all your syntax errors.

Try It out HERE

And also if you work with global variables.

If you append under variables its working. Pls check keywords.resource file

*** Settings ***
Resource     variables.resource

*** Variables ***
${Login}     ${url}/login.html
${logout}    ${url}/logout.html

*** Keywords ***
Log my Sides
    Log To Console    This logs the two variables:
    Log To Console    ${Login}
    Log To Console    ${logout}

could you please try to create a complete example of what you try to accomplish?

So if i understand you correctly, you try to set a variable with a keyword.
The ${url} variable…

And then you want to use that variable in another variable in the variable section?

And you get the error, that this variable ${url} is not set, when you call “Log my Sides” keyword?

Yes correct.

So if i understand you correctly, you try to set a variable with a keyword.
The ${url} variable…

When I run I am not getting ${url} part only displaying /login.html

OK.

The problem is, that if you import that resource file with your keyword, that keyword is not yet executed…
So the created variables just have not set variables in there.

  1. You have to call the actual keyword, that calculates google or yahoo…
  2. Now the variable ${url} is globally set, now you can use it.
  3. But this means, you have to resolve it now.
  4. The variables section has already tried to read it way before that time! so no values there.

You can accomplish that by using the variable as string with a backslash escaped and the replace it with its value after you called the set keyword.

WORKING EXAMPLE HERE! :wink:

Click the run button.

And by the way, you do not need to set ${url} at the variables section if you run that keyword.
In your situation before it was just that the variable ${url} is never set by your code, so adding that to the *** Variables *** did just cause an error less.
But when calling that setter thing, everything is fine.

Thanks Rene. This one works.

Can you try variables section in under keyword resource file instead of using in same file.