Forum Discussion

rmnrdi's avatar
rmnrdi
Contributor
6 years ago

reading ini without using Storages

I need to be able to read from a .ini file as it stores information needed for testing.

 

The problem is, it doesn't contain a [Root] section (which is mandatory) and one will not be added.

 

Is there any other way to read from a .ini file and get those values into TC? Well preferably without writing my own application to do so.

  • As always, I just created my own function to handle this.

    :) (Y)

     

    P.S. Possible performance improvement for large INIs: as it is pefrectly fine if [Root] section is at the end of the file, you may just make a copy of the file and append the [Root] line at its end.

     

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi Rob,

     

    Option a):

    -- Use some simplified script-based parser that fits your needs. For example: http://www.robvanderwoude.com/vbstech_files_ini.php, https://stackoverflow.com/questions/21825192/read-data-from-ini-file or https://stackoverflow.com/questions/3870019/javascript-parser-for-a-string-which-contains-ini-data;

     

    Option b):

    -- Use GetPrivateProfileString() WinAPI function (https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-getprivateprofilestring) and call it via the DLL object provided by TestComplete (see TestComplete help for more information and DLL code samples.) It might be impossible to call this function from your JavaScript code because JavaScript does not support out parameters used by the GetPrivateProfileString() function. As a possible workaround, you may implement the code as Script Extension using VBScript.

     

    P.S. There seem to be no simple and acceptable solution using .Net for this: https://stackoverflow.com/questions/217902/reading-writing-an-ini-file and https://code.msdn.microsoft.com/windowsdesktop/Reading-and-Writing-Values-85084b6a

     

    • rmnrdi's avatar
      rmnrdi
      Contributor

      As always, I just created my own function to handle this.

       

      The only thing that makes it incompatible is the [Root] subsection, so I created a procedure that created a new version of an ini and added [Root] to the top.

       

      function CreateTCCompatibleINI(sourceINI,targetINI)
      {
          //Deletes the file if it exists 
          aqFile.Delete(targetINI);
      //-------------------------------------------------  
          // Opens source ini for reading
          if (aqFile.Exists(sourceINI)) 
          { 
            var iniFile = aqFile.OpenTextFile(sourceINI, aqFile.faRead, aqFile.ctANSI);      
          }
          else
          {
            Log.Error("Cannot find " + sourceINI);
          }
      //-------------------------------------------------
          // Opens targetINI for writing
          if (!aqFile.Exists(targetINI)) 
          {  
            aqFile.Create(targetINI);
            var testINIFile = aqFile.OpenTextFile(targetINI, aqFile.faWrite, aqFile.ctANSI);
            //Writes root to the top of the ini file to make it compatible with Test Complete
            testINIFile.WriteLine("[Root]");
          }
          else
          { 
            var testINIFile = aqFile.OpenTextFile(targetINI, aqFile.faWrite, aqFile.ctANSI);
          }
      //-------------------------------------------------
      
        //Reads lines from source ini and writes them to target ini
        while(!iniFile.IsEndOfFile())
        {
          var line = iniFile.ReadLine();
          testINIFile.WriteLine(line);
          Log.Message(line);
        }
      //-------------------------------------------------
        //Closes files so they can be manipulated again.
        iniFile.Close();
        testINIFile.Close();  
      }
      • AlexKaras's avatar
        AlexKaras
        Champion Level 3

        As always, I just created my own function to handle this.

        :) (Y)

         

        P.S. Possible performance improvement for large INIs: as it is pefrectly fine if [Root] section is at the end of the file, you may just make a copy of the file and append the [Root] line at its end.