Forum Discussion

JackSparrow's avatar
JackSparrow
Frequent Contributor
9 years ago

Regarding Project Variables Creations and Deletion {-Python-}

Hi All,

 

I have all the reusable functions in one Unit and all the test case scripts in other units.

How to use those variable values from reusable functions  in  my test scripts (Units)for further testing purpose.

 

If am using for loops with  ProjectSuite Variables for storing the List ,only the last value of the loop getting stored and I want to know how can we delete the ProjectSuite Temp variables after it comes out of function so that we can optimize the memory allocations.

 

Please guide me in this .

4 Replies

  • baxatob's avatar
    baxatob
    Community Hero

    TestComplete does not have native support for such data type like a list (in the python context). However you can save and store your data in the file using json format:

     

    import json
    
    dataToSave = [1,2,3,4,5]
    
    with open("C:\\yourFile.json", "w") as file_:    # "w" token to write new file
        file_.write(json.dumps(dataToSave))

    Now you can use it:

     

    import json
    
    with open("C:\\yourFile.json", "r") as file_:    # "r" token to read data from file
        dataToLoad = json.loads(file_.read())
    
    # dataToLoad == [1,2,3,4,5]

     

     

    If you don't need to store the data persistently, you can just return it from your function to anywhere:

     

    Unit1
    
    def foo():
        list = [1,2,3]
        return list
    
    
    Unit2
    
    from Unit1 import foo
    
    data = foo()

    # data == [1,2,3]

     

    Python's interpreter will automatically allocate required memory and then clear it.

    • JackSparrow's avatar
      JackSparrow
      Frequent Contributor

      baxatob that's a great info thanks,

       

      I 100% believe that my code is wrong because of it am facing the issue i.e

       

      In the Library Functions Unit the below Code .

      def DB_Extraction():
        
        AConnection = ADO.CreateADOConnection()
        # Specify the connection string
        AConnection.ConnectionString = "Provider=MSDASQL.1;" + \
        "Data Source=Teacher_DB";
        AConnection.LoginPrompt = False
        AConnection.Open()
        # Execute a simple query
        RecSet  =  AConnection.Execute_('select name,code from demo')
        RecSet.MoveFirst();
        data_container = []
        r= []
        while not RecSet.EOF:  
         # rows1 = ""
          rows = RecSet.Fields.Item["name"].Value
          data_container.append(rows)
          # rows = rows.append(",")
          #rows.split(",")
          #Log.Message(rows)
          
          RecSet.MoveNext()  
             
        AConnection.Close()
        Log.Message(str(data_container))
        dbcount = len(data_container)
        Log.Message("The Database Count of the Demography List is  :"+str(dbcount))
        ProjectSuite.Variables.AddVariable("Var11","String")
        ProjectSuite.Variables.Var11 = str(data_container)
        return

      And in the TestScript Unit my testcase i.e

       

      import Library
      def demog_Deal():
        Library.DB_Extraction()
      data_container = ProjectSuite.Variables.Var11 demograph =Aliases.Teachers_debug.frmMain.dxDockSite.TdxDockPanel2.frmListDealNav.TdxDockSiteAutoHideContainer.TdxDockPanel.frmFindDeal.FrameFindDeal1.gbxDealOptions.esggraph demograph.keys("[Hold]~[Down]") RCount =Aliases.Teachers_debug.TcxComboBoxPopupWindow.TcxExtLookupGrid.TcxGridSite.GridView.ViewData.RecordCount val=[] for i in range(0,RCount): demolst = Aliases.Teachers_debug.TcxComboBoxPopupWindow.TcxExtLookupGrid.TcxGridSite.GridView.ViewData.Records[i].DisplayTexts[0] val.append(demolst) Log.Message(str(val)) a=[]

      c= set(val).union(set(data_container))
      d=set(val).intersection(set(data_container))
      a = c - d rowcount = RCount-1 Log.Message("The No of values in the Application List is :" +str(rowcount)) Log.Message("The Mismatch value is " +str(k)) Log.SaveResultsAs("C:\\Work\\Log8\\",lsHTML) ProjectSuite.Variables.RemoveVariable("Var11"

       

      Normally I used use this

      a=[]
      c= set(val).union(set(data_container))
      d=set(val).intersection(set(data_container))
      a = c - d

       

      To compare both values and print which are the mismatch values , but only in this test case the both set(val) and set(data_container) are showing as empty due to this all the values being displayed as mismatch values.

      Can you please let me know .

      • baxatob's avatar
        baxatob
        Community Hero

        Your should optimize your code.

         

        First of all you don't need all of those conversions from list to string and back. 

         

        Let me show what you did in your code:

         

        1. You have extracted a list data to data_container variable.

        2. You have converted list to string and assign new string to some project variable var11

        3. Then you have called var11 from another function. And finally you try to operate it as a list, but it's still the string! Of course you can revert the string back to the list, but it is not a trivial action.

         

        Better if your DB_Extraction() method will return extracted data as is:

         

         

        def DB_Extraction():
        # extraction return data_container

         

        And then you can call your method from another places:

         

         

        import Library
        
        def demog_Deal():
            
            data_container = Library.DB_Extraction()
        
            # other steps