Forum Discussion

Kateryna's avatar
Kateryna
Contributor
13 years ago

Get names of DBTables during test execution

Hello,



    I wonder is it possible to get names of all table stored in DBTables by script?

I need such script that during test execution saves all table names in current keyword test into array so I can use them later.

Is that possible?



I will appreciate any help!

Thank you very much.



Kateryna

  • Hi Kateryna,



    The DBTables[variable_name] and DBTables.variable_name constructs aren't valid in DelphiScript.



    Here's the correct code:

    var tbl;

    ...

    tbl := tbls.Item(i).Value;

    tbl.ConnectionString := ...


    Please note that the data source table name for a DBTable item can't be changed from scripts. So, the table name in the new data source must be the same as in the original data source.
  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Hi Kateryna,



    The DBTables[variable_name] and DBTables.variable_name constructs aren't valid in DelphiScript.



    Here's the correct code:

    var tbl;

    ...

    tbl := tbls.Item(i).Value;

    tbl.ConnectionString := ...


    Please note that the data source table name for a DBTable item can't be changed from scripts. So, the table name in the new data source must be the same as in the original data source.
  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Hi Kateryna,



    So, if I understood correctly, it is possible to run tests using TestExecute on the other computer if all tests are located there and I will run this script to change connection string for the each db checkpoint.
    Yes. Just make sure that each computer has the needed database provider installed.





    But I have the last question: do I really have to put the exact name of the machine in the Data Source. Can I put there some name that will fit all machines? Like localhost or something???
    If you use a network database server, there is no need to change the connection string at all, because it's the same on all client computers. So, you can simply use the originally configured DBTable item.



    If you use local data sources (located on the same computer where the tests are run), it is recommended to move them to some network location to avoid computer-specific data in scripts. For example, instead of storing an MS Access .mdb file on each test computer, place it to a network share.



    However, if you need to use local data sources, there're some tips that can help you make scripts computer-independent:

    * Your database provider may support specific syntax for local data sources. Please refer to http://connectionstrings.com to learn the connection string syntax for your provider.

    * If the connection string includes the local computer name, use Sys.HostName in scripts instead of the hard-coded computer name.

    * If the data source file is in the project folder, you can use Project.Path in scripts to specify the project folder path.



    Hope this helps!
  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Hi Kateryna,



    You can use the aqObject.GetProperties method to iterate through the DBTables object and get the table names. Here's the example:

    procedure Test;

    var i, tbls;

    begin

      tbls := aqObject.GetProperties(DBTables);

      for i := 0 to tbls.Count - 1 do

      begin

        Log.Message(tbls.Item(i).Name);

      end;

    end;
  • And if I need to change a connection string in each db table, how should I do it?




    I tried to do like this:





    procedure



    Test;


    var i, tbls, tablename;


    DBTableElem : OleVariant;


    begin


       tbls := aqObject.GetProperties(DBTables);


     for i := 0 to tbls.Count - 1 do



       begin  


      tablename:= tbls.Item(i).Name;




      DBTableElem := DBTables[tablename];    




    DBTables.DBTableElem.ConnectionString:='Provider=SQLOLEDB.1;'+'Integrated Security=SSPI;'+'Persist Security Info=False;'+'Initial Catalog=NL3_TestComplete;'+'Data Source=ds';









    But I received an error about unknown name 'DBTableElem'










  • Thank you so much!!!

    So, if I understood correctly, it is possible to run tests using TestExecute on the other computer if all tests are located there and I will run this script to change connection string for the each db checkpoint. Am I right?
  • I tried it and it worked perfectly.

    But I have the last question: do I really have to put the exact name of the machine in the Data Source. Can I put there some name that will fit all machines? Like localhost or something???



    Thank you.

  • Hello,



    I used Sys.HostName and that really worked. Thank you. You helped me very much!



    Best regards,

    Kateryna