Repository navigation
Description
RF:2.8.6
Python: 2.7
OS: W7
Thanks in advance for your help.
I have installed RF 2.8.6 and I am using import variable as is shown below, somehow seems is not working, the payment_variables.py file is created with some values in it in the first test, then is updated for the second tests (I can verify this by open the file directly), but the variable is not being re-imported / updated.
Steps to reproduce it
1.- Create a variable file as x.py with some variables and values
example
('fields_to_validate', {'session id': 'YjI2ZTVlYmItYTllZC00N2Q2LWI4NjMtMGZlN2M5NTYxNzAy', 'IP address': '88.88.88.88', 'Amount': '10.64', 'Last4CCDigits': '1111', 'Product Code': '7181', 'Transaction Status': 'true', 'CCType': 'VISA', 'Error Code': '0'})
2.- Import the variable file and check its variables and values
Import Variables ${RES_eCAPS_WECAN}/x.py
3.- Make changes in the values of the variables and save the file
4.- Import the variable file again and check its variables and values
I expect the 'import variables' keyword should import file even if was already imported, but this seems not be the case, because the variables keep the values gotten in the first Import (Step 2).
any suggestion?
here you go more details.
in the following example may help to understand what is going on.
I first read the file using OperatingSystem.Get File and its content is displayed.
right after that I use BuiltIn.Import Variables to import the variables from the same file, and clearly the content is different.
any idea? shouldn't Import Variable re-import.
KEYWORD: ${the file} = OperatingSystem.Get File ${RES_eCAPS_WECAN}/payment_variables.py
Documentation:
Returns the contents of a specified file.
Start / End / Elapsed: 20150624 12:05:29.847 / 20150624 12:05:29.848 / 00:00:00.001
12:05:29.848 INFO Getting file 'C:\Workspace\CAPSProject\capstest\resources\eCAPS\eCAPS_WECAN\payment_variables.py'
12:05:29.848 INFO ${the file} = fields_to_validate = {'CCType':'','Transaction Status':'true','Error Code':'0','IP address':'88.88.88.88','Amount':'10.64','Last4CCDigits':'','session id': 'YjI2ZTVlYmItYTllZC00N2Q2LWI4NjMtMGZlN2M5NTY...
KEYWORD: BuiltIn.Import Variables ${RES_eCAPS_WECAN}/payment_variables.py
Documentation:
Imports a variable file with the given path and optional arguments.
Start / End / Elapsed: 20150624 12:05:29.850 / 20150624 12:05:29.853 / 00:00:00.003
12:05:29.853 INFO True
C:\Workspace\CAPSProject\capstest\resources\eCAPS\eCAPS_WECAN\payment_variables.py
[u'overwrite=True']
<module 'payment_variables' from 'C:\Workspace\CAPSProject\capstest\resources\eCAPS\eCAPS_WECAN\payment_variables.py'>
[('fields_to_validate', {'session id': 'YjI2ZTVlYmItYTllZC00N2Q2LWI4NjMtMGZlN2M5NTYxNzAy', 'IP address': '88.88.88.88', 'Amount': '10.64', 'Last4CCDigits': '1111', 'Product Code': '7181', 'Transaction Status': 'true', 'CCType': 'VISA', 'Error Code': '0'})]
Activity
pekkaklarck commentedon Aug 14, 2015
We currently explicitly put imported libraries and resource into a cache to avoid spending time for re-importing them. I don't think we do that with variable files, but Python itself uses
sys.modulesas a cache imported modules and I believe that causes this behavior.We already now remove a module from
sys.modulesif we notice that the imported module is wrong (e.g. if we importsome/path/foo.pyandfoomodule we got is fromanother/path/foo.py) but I guess we could remove them always. Even that wouldn't 100% sure way to get all fresh variables, though, because if the variable file itself imports other modules, those modules will still be get fromsys.modules. Trying to detect everything that is imported and removing them is just not possible.This change is potentially a bit risky and not something we want to do in a minor release. I've been lately thinking about not caching libs and resources in RF 3.0, so perhaps we could look at this then too.
[-]Robotframework: Import Variables keyword should import files even if they are already imported[/-][+]Variable files should always be re-imported[/+]Samu1971 commentedon Aug 14, 2015
thanks for the prompt response.
I will consider this info, and workaround meanwhile a long term solution is develop (hopefully).
cheersSamuel Fuentes.-
We currently explicitly put imported libraries and resource into a cache to avoid spending time for re-importing them. I don't think we do that with variable files, but Python itself uses sys.modules as a cache imported modules and I believe that causes this behavior.We already now remove a module from sys.modules if we notice that the imported module is wrong (e.g. if we import some/path/foo.py and foo module we got is from another/path/foo.py) but I guess we could remove them always. Even that wouldn't 100% sure way to get all fresh variables, though, because if the variable file itself imports other modules, those modules will still be get from sys.modules. Trying to detect everything that is imported and removing them is just not possible.This change is potentially a bit risky and not something we want to do in a minor release. I've been lately thinking about not caching libs and resources in RF 3.0, so perhaps we could look at this then too.—
Reply to this email directly or view it on GitHub.
MarkAWalters commentedon Jun 10, 2019
I have only been using Robot Framework with Ride for a few months, and am desperate to use Variable files to reduce the clutter (I am using variables to store all locators in, in line with Page Object Model, and there are quite a few), and using a variable file would deliver several benefits but the caching issue is a major problem, as the only way to fix this seem(ed) to be to close and restart Ride. (FYI - I am only using the simplest form of variable file, with just variable name and value pairs).
Therefore, having the ability to refresh the cache when the file is updated would be a great help.
In the meantime, a slightly less painful workaround (compared to restarting Ride), is that I save my variable file under a slightly different name (e.g in notepad), and edit Ride to pick up the new name. Still not pretty but it seems to work so far, hopefully this might help some others. Of course, once the tests are stable, I will just set the name back before sharing.
pekkaklarck commentedon Mar 3, 2021
My understanding why this occurs is this:
sys.modulesby Python. I already mentioned that in a comment above.sys.modules, same dictionary is used even after re-import.If someone could provide a simple example demonstrating this problem I could debug it further. Modifying
sys.modulesis always a bit risky, though, so I cannot promise we could really fix this. It could also be that the variable file imports some extra modules that RF wouldn't know about and they needed to be removed fromsys.modulesas well.Good news is that there are some possible workarounds to this problem:
Set Test Variable. You ought to be able to use the global variable as the initial value, but you may need to take a a copy of it or otherwise you could still be modifying the same object.get_variablesfunction instead of defining them in the variable file module. This could avoid the caching problem.