cancel
Showing results for 
Search instead for 
Did you mean: 

reading ini without using Storages

SOLVED
Highlighted
Contributor

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.

4 REPLIES
Community Hero

Re: reading ini without using Storages

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

 

Regards,
Alex
[Community Expert Group]
____
[Community Expert Group] members are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Postings made by [Community Expert Group] members
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================
Contributor

Re: reading ini without using Storages

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();  
}
Community Hero

Re: reading ini without using Storages

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

Smiley Happy (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.

 

Regards,
Alex
[Community Expert Group]
____
[Community Expert Group] members are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Postings made by [Community Expert Group] members
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================
Contributor

Re: reading ini without using Storages

Holy cow, that would make it much faster!

 

Copy ini to folder with new name

Open it

Append [Root]

Close it

 

Yeah. That would definitely be faster than copy the contents.

 

I  should have tried putting [Root] at the end first, it just didn't seem to make sense that it would work.

 

UPDATE TO PREVIOUS CODE:

function CreateTCCompatibleINI(sourceINI,targetINI)
{
    var didCopy;
    
    //Deletes the ini file if it exists 
    aqFile.Delete(targetINI);
//-------------------------------------------------  

    // Copy source ini to target folder
    if (aqFile.Exists(sourceINI)) 
    { 
      didCopy = aqFile.Copy(sourceINI,targetINI, false); 
    }
    else
    {
      Log.Error("Cannot find " + sourceINI);
    }
//-------------------------------------------------

    // Opens targetINI for writing
    if (didCopy) 
    {  
      var targetINIFile = aqFile.OpenTextFile(targetINI, aqFile.faWrite, aqFile.ctANSI);
      //Writes root to the bottom of the ini file for TC compatibility.
      targetINIFile.WriteLine("[Root]");
    }
    else
    { 
      Log.Error(sourceINI + " did not copy to " + targetINI);
    }
//-------------------------------------------

  //Closes files so they can be manipulated again.
  targetINIFile.Close();  
}

Way faster!

Share Your Stories
Read Latest News
Top Kudoed Authors