Skip to content

Variable files should always be re-imported #2101

Description

@Samu1971

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

  1. pekkaklarck commented on Aug 14, 2015

    @pekkaklarck
    Member

    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.

  2. added this to the 3.0 milestone on Aug 14, 2015
  3. changed the title [-]Robotframework: Import Variables keyword should import files even if they are already imported[/-] [+]Variable files should always be re-imported[/+] on Aug 14, 2015
  4. Samu1971 commented on Aug 14, 2015

    @Samu1971
    Author

    thanks for the prompt response.
    I will consider this info, and workaround meanwhile a long term solution is develop (hopefully).
    cheersSamuel Fuentes.-

     El Viernes, 14 de agosto, 2015 10:26:24, Pekka Klärck <[email protected]> escribió:
    

    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.

  5. modified the milestone: 3.0 on Dec 7, 2015
  6. MarkAWalters commented on Jun 10, 2019

    @MarkAWalters

    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.

  7. pekkaklarck commented on Mar 3, 2021

    @pekkaklarck
    Member

    My understanding why this occurs is this:

    • Imported variable file is added to sys.modules by Python. I already mentioned that in a comment above.
    • These variables are globally available to all tests.
    • Dictionaries are mutable. When one test changes a globally available dictionary, also other tests see the change.
    • Due to caching in 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.modules is 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 from sys.modules as well.

    Good news is that there are some possible workarounds to this problem:

    1. Don't modify globally available dictionaries or other mutable objects. Instead create a new variable on the test level using 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.
    2. Implement variable file so that it returns variables from get_variables function instead of defining them in the variable file module. This could avoid the caching problem.
  8. added this to the v8.0 milestone on Mar 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions