How to import a variable value present in a .robot file into a variable in .py file


I am trying to get counts of similar elements using robot framework and storing it in a variable and setting the same as global variable .

Below is the Keyword in the robot file under keyword section:

Fetch Total count of Record Request Tile on RA dashboard
Wait Until Element Is Visible ${RecordRequest_Tile_Locator}
${RecordRequest_Tile_count} = Get Element Count ${RecordRequest_Tile_Locator}
log ${RecordRequest_Tile_count}
Set Global Variable ${RecordRequest_Tile_count}

Now im making using of the global variable : ${RecordRequest_Tile_count} in another xpath locator present in .py file:

from robot.libraries.BuiltIn import BuiltIn
Comm_Portal_HomePage_RecordRequestsTab_WithTotalCount_Locator=“//div[contains(text(),'Record Requests (”+str(RecordRequest_Tile_count)+ " )')]"

lets assume the value of global var ${RecordRequest_Tile_count} is 12

but when executed i see that the value of the global variable is not passed to the xpath locator variable of .py file

i see the element locator as :
//div[contains(text(),‘Record Requests (None )’)] instead of //div[contains(text(),‘Record Requests (12)’)]

can anyone please help me with this issue?

Hi Aish,

A global variable is only global within the same process, as in global to all functions this process might call not global to all programs running on my computer.

It looks like you are running the python file in python, so it would have no idea about a variable in another robot framework process, I know you’re importing a robot framework module into your python script but it still doesn’t know anything about another robot framework test running in another process.

There are many ways you can pass the information from one script to another, some examples:

  • have the robot file create/modify a system environment variable, then have the python script read that environment variable, depending on your security team this may need elevated privileges? also doing this is very transitory and won’t survive a reboot of the test machine, so it’ll depend on your setup if this is ok or not
  • have the robot file create/modify a file (usually text), then have the python script read that file
  • have the robot file store the data in a remote location, then have the python script read that remote location for the data, best if your using temporary test runners (temporary virtual machines, docker containers, etc) where the scripts might not be on the same machine. That remote location might be
    • a database server
    • a file server
    • your version control system
    • Test Data Table

Hope that helps,