Forum Discussion

samirc23's avatar
samirc23
Contributor
11 years ago

Can I call already created DDT driver from one unit to other units?

I have created DDT function in unit3 and I would like to use it in unit1, unit2 and etc, is it possible and how?



I tried it following way, but it’s not working.



Unit3:

function abcfunction1()

{

{

  var mycsv;

   mycsv = DDT.CSVDriver("C:\\path\\file.txt");

   mycsv.DriveMethod("Unit3.Unit1");

   mycsv.DriveMethod("Unit3.Unit2");

}

}



In unit1 and unit2 I added ‘add unit references’ of unit3.



Unit1:

function test()

 {

Object. SetText(DDT.CurrentDriver.Value("yourname"));

 }

 

Unit2:

function xyztest()

 {

Object. SetText(DDT.CurrentDriver.Value("name"));

 }



Please let me know how to use already created DDT function in other units.

 

  • Well, of course you get an error, since inside the abcfunction1 function you declare a new one (local) variable, which is destroyed after the function exits.



    But this question isn't related to TestComplete or JScript, this is a common programming question, any developer could answer it. I suggest you to start learning basics of procedure programming (global and local variables, functions, passing parameters by value and reference), otherwise you will have to ask a lot of questions and get all of them answered in so much details.



    For instance, I will not spend time teaching anyone basic programming concepts, sorry. There are many tutorials and books dedicated to this. 



    Testing automation requires understanding programming, that's where you should start from.
  • karkadil's avatar
    karkadil
    Valued Contributor
    Well, of course you get an error, since inside the abcfunction1 function you declare a new one (local) variable, which is destroyed after the function exits.



    But this question isn't related to TestComplete or JScript, this is a common programming question, any developer could answer it. I suggest you to start learning basics of procedure programming (global and local variables, functions, passing parameters by value and reference), otherwise you will have to ask a lot of questions and get all of them answered in so much details.



    For instance, I will not spend time teaching anyone basic programming concepts, sorry. There are many tutorials and books dedicated to this. 



    Testing automation requires understanding programming, that's where you should start from.
  • jorgesimoes1983's avatar
    jorgesimoes1983
    Regular Contributor
    You must include your unit at the top of the file, like this:



    //USEUNIT name_of_File



    ...

    code...

    ...
  • karkadil's avatar
    karkadil
    Valued Contributor
    Looking at your 1st example I can see an incorrect invoke of the functions



    mycsv.DriveMethod("Unit3.Unit1");

    mycsv.DriveMethod("Unit3.Unit2");



    This parameter is a complex name, which includes name of the unit and name of the function in that unit which is called.



    Do you have functions with names Unit1 and Unit2 inside your Unit3? I believe you don't.



    Most probably (basing on your examples) you meant something like this




    mycsv.DriveMethod("Unit1.test");

    mycsv.DriveMethod("Unit2.xyztest");



    P.S. Next time when you describe a problem, please mention what exactly error you see, it will save the time. Simply saying "it's not working" isn't informative




  • Hi Jorge,



    Thank you very much for your reply and Sorry I forgot to mention but when we add reference then it automatically add into code (e.g USEUNIT Unit3), but still its not working.  

  • simon_glet's avatar
    simon_glet
    Regular Contributor
    Hi Sam,



    You have a scope problem with your variable.



    Try something like this:



    Unit1:

    var globalVar;


    function setGlobalVar(value)

    {

      globalVar = value;

    }



    Unit2:

    //USEUNIT Unit1

    function testGlobalVar()

    {

      Log.Message("Global var value:" + globalVar);

      setGlobalVar(2);

      Log.Message("Global var value:" + globalVar); 

    }



    You should get the following log:

    Global var value:undefined 10:39:26 Normal  

    Global var value:2 10:39:26 Normal



    Sincerely  

     


  • Hi Jorge,



    its //USEUNIT file, but i will try as Simon mentioned.



    Many Thanks,

    Sam
  • Hi Simon,



    I tried it following way, but it’s not working (error 'mycsv' is undefined).



    Unit3:

    var globalvar ;

    globalvar = mycsv;

    function abcfunction1()

    {

    {

        globalvar = mycsv;

       mycsv = DDT.CSVDriver("C:\\path\\file.txt");

       mycsv.DriveMethod("Unit3.Unit1");

       mycsv.DriveMethod("Unit3.Unit2");

    }

    }



    In unit1 and unit2 I added ‘add unit references’ of unit3.



    Unit1:

    //USEUNIT Unit3


    function abcfunction1()

    {

    {


       var globalvar;


       globalvar = mycsv;


      mycsv.DriveMethod("Unit3.test");




    }


    function test()

     {

    Object. SetText(DDT.CurrentDriver.Value("yourname"));

     }

     



  • Hi Gena,



    I tried it following way, but getting error 'mycsv' is undefined.



    Unit3:

    var globalvar ;

    globalvar = mycsv;

    function abcfunction1()

    {

    {

       var globalvar = mycsv;

       mycsv = DDT.CSVDriver("C:\\path\\file.txt");

       mycsv.DriveMethod("Unit3.test");

      

    }

    }



    In unit1 I added ‘add unit references’ of unit3.



    Unit1:

    //USEUNIT Unit3

    function abcfunction1()

    {

    {

       var globalvar;

       globalvar = mycsv;

      mycsv.DriveMethod("Unit3.test"); //also tried mycsv.DriveMethod("Unit1.test");



    }



    function test()

     {

    Object. SetText(DDT.CurrentDriver.Value("yourname"));

     }

     

  • Hi Gina,

    I thought its something to do with TC, but since it is programming than I ll sort it out.

    Thanks,

    Sam