Forum Discussion

cauline's avatar
cauline
Contributor
8 years ago
Solved

Is there a way to set a DB Table variable programmatically in JavaScript?

I am trying to programmatically setup a DB Table variable in JavaScript.  Is there a way to do this, or do I have to go through the Wizard?

 

Thx!

  • Research the DDT object and associated "driver" methods.  Basically, the variable type of DB Table that you find in Project, Project Suite, and Keyword Test variables are a UI way of creating this DDT object type.  

     

    So, for example, if you want to have a DB Table of data in an excel spreadsheet and be able to, on the fly, create that variable, you COULD do...

    function getExcelData(excelFilePath, sheetName) {
        return DDT.ExcelDriver(excelFilePath, sheetName, true);
    }

    Running that function with the necessary data will create such a driveable object.

    Then, TECHNICALLY what you could do is create a variable that has a type "Object" and assign the result of that function to it.  Like so.


    function getExcelData(excelFilePath, sheetName) {
        return DDT.ExcelDriver(excelFilePath, sheetName, true);
    }
    
    function foo3() {
        Project.Variables.dbObject = getExcelData('C:\\Source\\Sandbox\\JavaScriptProject\\Script\\Book1.xlsx', 'DataSheet1');
        while (!Project.Variables.dbObject.EOF()){
            Log.Message(Project.Variables.dbObject.Value('Value1'));
            Log.Message(Project.Variables.dbObject.Value('Value2'));
            Project.Variables.dbObject.Next();
        }
        DDT.CloseDriver(Project.Variables.dbObject.Name);
    
    }

3 Replies

  • baxatob's avatar
    baxatob
    Community Hero

    DB Table variable uses some outer source like excel file or database records.

    So if you need to assign a new value -> you need to change an appropriate file or database record.

    Of course you can do it programmatically, but approach depends on your DB Table source.

    • tristaanogre's avatar
      tristaanogre
      Esteemed Contributor

      Research the DDT object and associated "driver" methods.  Basically, the variable type of DB Table that you find in Project, Project Suite, and Keyword Test variables are a UI way of creating this DDT object type.  

       

      So, for example, if you want to have a DB Table of data in an excel spreadsheet and be able to, on the fly, create that variable, you COULD do...

      function getExcelData(excelFilePath, sheetName) {
          return DDT.ExcelDriver(excelFilePath, sheetName, true);
      }

      Running that function with the necessary data will create such a driveable object.

      Then, TECHNICALLY what you could do is create a variable that has a type "Object" and assign the result of that function to it.  Like so.


      function getExcelData(excelFilePath, sheetName) {
          return DDT.ExcelDriver(excelFilePath, sheetName, true);
      }
      
      function foo3() {
          Project.Variables.dbObject = getExcelData('C:\\Source\\Sandbox\\JavaScriptProject\\Script\\Book1.xlsx', 'DataSheet1');
          while (!Project.Variables.dbObject.EOF()){
              Log.Message(Project.Variables.dbObject.Value('Value1'));
              Log.Message(Project.Variables.dbObject.Value('Value2'));
              Project.Variables.dbObject.Next();
          }
          DDT.CloseDriver(Project.Variables.dbObject.Name);
      
      }