Forum Discussion

minhterry's avatar
minhterry
Contributor
10 years ago

How to call method from other UNIT by using EVAL() method

Hi everyone,



I have script A and script B

in Script A i want to call a method FindMyProcess in script B using EVAL

Example:

function testTHIS()

{

  eval('//USEUNIT ScriptB;var x=1;FindMyProcess();ShowMessage(x+5);');

}



It does not work because the //USEUNIT look like a comment for that script. And the message will not display.

I don't want to add the //USEUNIT out side eval, because in future, i want to call some script from the method which belong to other script file, such as ScriptC, ScriptD...)



Can any1 suggest me how to do that. Thanks so much.



--

Regards,

MinhT

10 Replies

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3
    Hi Minh,



    What about

    function testTHIS()

    {

      eval('B.FindMyProcess();');

    }



    Does it work?

  • Philip_Baird's avatar
    Philip_Baird
    Community Expert
    Hi minh, why is it you do not want to place the //USEUNIT out side the eval? If you are wanting to access methods which belong to other script files it should just be a matter of including them all at the head of ScriptA.

     


    //USEUNIT ScriptB


    //USEUNIT ScriptC


    //USEUNIT ScriptD


     


    If you make sure you use the recommended fully qualified accessor <Script>.<Method>, e.g. ScriptB.FindMyProcess() you will have no name clash problems.


     


    Honestly, even if you could do what you are attempting I wouldn't recommend doing it simply because if you base your code around multiple eval() calls it will be an absolute nightmare to work out what went wrong if an Exception occurs.


     


    Also, the Runner.CallMethod() Denis mentioned is a much safer mechanism for doing this.


     


    The only issue is Smartbear has marked this method as obsolete, I really don't know why as in some cases being able to invoke methods in this fashion can be extremely useful and the supplied work arounds mentioned in the documentation do not provide equivalent functionality.


     


    Maybe Smartbear would consider moving Runner.CallMethod() out of obsolesence :)


     


    Regards,


    Phil Baird

  • Hello,



    Denis Gubert has a good suggestion for me and it works:



    eval('var x=1;Runner["CallMethod"]("ScriptB.FindMyProcess");ShowMessage(x+5);');



    So can anyone suggest me how to use this with aqObject["CallMethod"], because the Runner["CallMethod"] is obsolete.



    --

    Thanks all for your help

    MinhT
  • AlexKaras's avatar
    AlexKaras
    Champion Level 3
    Hi Minh,



    a) Obsolete methods exist in TestComplete for years, so I don't consider this as a real risk;

    b) Why not to call  ScriptB.FindMyProcessdirectly? I.e.:

    eval('var x=1;ScriptB.FindMyProcess;ShowMessage(x+5);');



    P.S. I would be second to Phil that it is extremely difficult to figure-out what went wrong if some problem occurs in the code called via eval(). Such code is not shown in the call stack and the error will be reported by TestComplete as occurred on the eval() line of code (unless you implement your own error handling in the code called using eval(). So you will have to either add additional logging to help you to diagnose where the problem occurred or to guess where to put the debug breakpoint.
  • Hello



    Alexei Karas,

    I cannot call directly in the eval because it does not work. You can try it :-)




    I use eval for my custom Automation framework. so that i will do error handling by my way :p



    --

    Best Regards,

    MinhT

  • Im not sure about ExecuteGlobal, just google and cannot find more info. Can you share me any info about that ?



    --

    Regards,
  • Hi Minh,



    Try this:




    Sub CallFunction()


        ExecuteGlobal aqFile.ReadWholeTextFile(<Fully qualified file name>, 21)


        call <function to be called from the above file>


    End Sub




    21 is TextCodingType. More details about its possible values and details of ReadWholeTextFile are available at following link:



    http://support.smartbear.com/viewarticle/59340/