Forum Discussion

Tris's avatar
Tris
Contributor
5 years ago
Solved

Possible to use csv file for variables in script?

Hi all,

 

I'm attempting to use a csv file containing variables in a scripted function in order to use the variables within the csv file to generate a connection string.

 

I've had a good look around, but cannot see anything obvious around using variables within a csv file for a script, other than for running data driven tests.

 

To be clear, I have a csv file with variables which I wish to use to form a connection string in a function, rather than using project variables or any variables within test complete.

 

The objective of this is so that when the repository is pulled, users do not have to manually change variables each time from someone elses commit and to use a fixed non-tracked file held locally to each user.

 

Many thanks,

Tristan

3 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    Rather than a CSV, consider using an INI file.

     

    https://support.smartbear.com/testcomplete/docs/reference/program-objects/storages/ini.html

     

    If you need to use a CSV, you can open a text file using OpenTextFile and other methods of the aqFIle object and read lines from the CSV.  You can then use aqString.GetListItem to grab a value from the comma separated fields in the row, assuming you set the ListSeparator property to a comma.

     

    https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqfile/opentextfile.html

    • Tris's avatar
      Tris
      Contributor

      Thanks tristaanogre,

       

      I've taken your advice and implemented a .ini file instead, this way seems more efficiant as you can just declare the variable from the file. 

       

      I can then use the declared variable to build the connection string.

       

      Many thanks again!

      Tris

      • Tris's avatar
        Tris
        Contributor

        Incase anyone else is looking for how to implement this, I scripted a simple funciton to read the ini file and take the vaIue I want from it, I then use this to define my variables, see below.

         

            function DatabaseVariables(){
                var ini = Storages.INI("C:\\Automation\\DatabaseVariables.ini");
                SQLServerName = ini.GetSubSection("DatabaseVariables").GetOption("SQLServerName", "");
                DatabaseName = ini.GetSubSection("DatabaseVariables").GetOption("DatabaseName", "");
                DatabaseUserName = ini.GetSubSection("DatabaseVariables").GetOption("DatabaseUserName", "");
                DatabasePassword = ini.GetSubSection("DatabaseVariables").GetOption("DatabasePassword", "");
                DatabaseMasterName = ini.GetSubSection("DatabaseVariables").GetOption("DatabaseMasterName", "");
                SQLServerInstance = ini.GetSubSection("DatabaseVariables").GetOption("SQLServerInstance", "");        
        }

         

        The above reads the ini file which looks like the below example

         

        ; IMPORTANT: The Root section is required
        [Root]
        
        [Settings]
        Access=ReadWrite
        Type=1
        
        [DatabaseVariables]
        SQLServerName=VALUE
        DatabaseName=VALUE
        DatabaseUserName=VALUE
        DatabasePassword=VALUE
        DatabaseMasterName=VALUE
        SQLServerInstance=VALUE

        So, given the above, all values defined in the ini would come back as VALUE (no formatting around the string, just the plain string). 

         

        Note you can just create a plain text file and change the extension to .ini and this will become an ini file.

         

        Hope this helps someone in the future, thanks again to Tristaanogre!