Forum Discussion

TikiHardBop1's avatar
TikiHardBop1
New Contributor
2 months ago

Problem accessing variables inside of Python scripts

Having issues with accessing variables inside of Python scripts -- all kinds of variables from Project Suite Variables to Project Variables to Keyword test variables. It is giving me an object not found error. 

I will shut down TestComplete and bring it up fresh. The first time I run the script, it runs perfectly, but every time I run it after that, it fails. 

I have tried accessing variables via a Keyword and that seems to work OK. It is only when I call a Python script do I get this unusual behavior. 

As a side issue, which may or may not be related, I also cannot get Log.Message() to work inside of my Python scripts, but it does seem to work inside the Keyword tests, just like above. 

I am running 15.76.5.7 on a Windows 11 machine and the AUT is a web application. 

7 Replies

  • It would help if you provide your Python script to look at.

    Root Cause Analysis

    TestComplete’s Python engine loads each script unit only once per project session. Any references you make to ProjectSuite.Variables, Project.Variables or Log.Message at the module (global) scope execute at import time—and then stay cached.

    • On first run, the module is imported, the global references succeed, and everything works.
    • On subsequent runs, TestComplete reuses the already-loaded module and does not re-evaluate module-level code. Your stale or missing references now trigger “object not found.”
    • Likewise, calling Log.Message outside of a test routine or after import can silently fail or not bind to the TestComplete test engine context.

    Step-by-Step Solution

    1. Move all variable reads and log calls inside a Python function or test routine—never at the top-level of the script unit.
    2. Use the full object model for each variable kind:
      • Suite variables: ProjectSuite.Variables.YourSuiteVar
      • Project variables: Project.Variables.YourProjectVar
      • Keyword-test variables: pass them as parameters when you invoke your Python routine from a Keyword Test.
    3. Inside your routine, wrap your variable lookups in a try/except to catch missing-object errors and report them via Log.Error().
    4. Ensure you invoke your function from a Python Script Test or call it via a Keyword Test each time you need it. This guarantees the code block that reads variables and logs runs fresh on every execution.
    5. (Optional) If you ever need to force-reload your module after editing, you can use Python’s importlib.reload(), but well-structured functions typically make that unnecessary.

    Code Snippet

    With this pattern, every time you run the Test routine—whether back-to-back or after many runs—the engine will (re)execute ReadAndLogVariables(), freshly resolve your variables, and correctly bind Log.Message() to the active test context.

    # Put this in your Script Unit, not at module top-level
    
    def ReadAndLogVariables():
        # Dynamically fetch suite & project variables
        try:
            suite_var   = ProjectSuite.Variables.MySuiteVariable
            project_var = Project.Variables.MyProjectVariable
        except AttributeError as e:
            Log.Error("Variable access failed: " + str(e))
            return
    
        # Log the values so you can verify each run
        Log.Message("Suite variable value: {}".format(suite_var))
        Log.Message("Project variable value: {}".format(project_var))
    
    
    # Your Python Script Test entry point
    def Test():
        ReadAndLogVariables()

    💬 If a response helped you out, don’t forget to Like it! And if it answered your question, mark it as the solution so others can benefit too.

    Note: This reply was drafted with AI assistance, and as I do not have access to license I did not try out such Code Snippet.

    • TikiHardBop1's avatar
      TikiHardBop1
      New Contributor

      Thanks for your rapid reply. Here is a step-by-step of what is happening. This is the keyword test Sandbox: 

      This is the Keyword test variables: 

      Here are the project level variables: 

      Here is the code for the script CheckForEnvVariables in the file LoginScripts: 

      def CheckForEnvVariables():
        for i in range(KeywordTests.Sandbox.Variables.VariableCount):
              name = KeywordTests.Sandbox.Variables.GetVariableName(i)
              Log.Message("Name: " + str(name))
              Log.Message("Value: " + str(KeywordTests.Sandbox.Variables.VariableByName[name]))

        for i in range(Project.Variables.VariableCount):
              name = Project.Variables.GetVariableName(i)
              Log.Message("Name: " + str(name))
              Log.Message("Value: " + str(Project.Variables.VariableByName[name]))

      After bringing up TestComplete cold, the first thing I do is run the keyword test Sandbox. This is the resulting log file, which looks like what we want: 

      I immediately run Sandbox a second time and this is the error I get:

      And this is the resulting log file: 

      And further attempts to run Sandbox have the same error

      • rraghvani's avatar
        rraghvani
        Icon for Champion Level 3 rankChampion Level 3

        From TC v15.68.8.7 and onwards, there's been a number of reported issues, when using Log.Message method in Python, as well as other issues!

        I suggest you open a support ticket via https://support.smartbear.com/testcomplete/message/ 

        Note, use the Formatted string literals  to simplify string formatting and interpolation e.g.

        def CheckForEnvVariables():
            for i in range(0, Project.Variables.VariableCount):
                name = Project.Variables.GetVariableName(i)
                Log.Message(f"Name: {name}")
                Log.Message(f"Value: {Project.Variables.VariableByName[name]}")