cancel
Showing results for 
Search instead for 
Did you mean: 

Possible to use csv file for variables in script?

SOLVED
Occasional Contributor

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

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Hero

Re: Possible to use csv file for variables in script?

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


Robert Martin
[Community Expert Group]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Extensions available

View solution in original post

3 REPLIES 3
Highlighted
Community Hero

Re: Possible to use csv file for variables in script?

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


Robert Martin
[Community Expert Group]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Extensions available

View solution in original post

Occasional Contributor

Re: Possible to use csv file for variables in script?

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

Occasional Contributor

Re: Possible to use csv file for variables in script?

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!