Forum Discussion

Adam_Zehavi's avatar
Adam_Zehavi
Occasional Contributor
14 years ago

How to invoke a C# script method, reflectively?

Hi,



I have a many functions spread over vast number of script files, I may invoke any one of them from a single "main" method using a string to describe the method. currently I use a very long switch case and I would like to know if there is a way to use the method name as the string and invoke the method reflectively?





TODAY:



function runner(methodName, params) {

switch(methodName) {

case function1:

run function1(params);

case function2:

...

...

...

and so on.





I don't like this approach, I would like to use the name to get a reference to the method:



function runner(methodName, params) {

var method = getReflectiveMethod(methodName);

method["invoke"](params);





is this possible?



Adam.

14 Replies


  • Hi Dhanapal,





    I've noticed that there is no space between the /r and /p parameters. It should be: .../r /p:MiserQA...


  • tdichter's avatar
    tdichter
    Occasional Contributor
    Hi,



    My background is in C, Java, Python development. On a new project I work on, I have to build a Test Complete framework to simulate various Windows user level scenarios to be used by the QA team.



    this is what I need to do (small part of the project):



    1.  Write a function which gets a string representing a UNC Windows path to a remote CIFS file server – I.E \\\\1.1.1.1\\share_1 – the functions name is: view_directory_listing (p_str_unc_path) – the method returns a list of the files/folders under the above UNC path. (please see below on how its implemented – as seen, very simple…)


    2.  Write a main function which is invoked externally from the DOS command line on a Test Execute client – the main will get 2 parameters, the name of the method to invoke and a string which is the UNC path to provide.


    3. Both the aboe methods are written in the same Unit in the Test Complete project.



    4.  On the Test Execute machine, I copied the content of the project to a location under drive C.


    This is the command line I invoke on the Test Execute machine:


    C:\Test_complete_proj\VSS_1\VSS_1.pjs /r /p:VSS_1 /u:vss_script_1 /rt:main view_directory_listing \\\\10.1.1.149\\dev


    As seen - I provide to the routine main the 2 parameters (maybe there is a problem in this - I didnt see in the documentations on how to invoke Test Complete methods providing external params...)


    When I run this command, nothing happens, I don’t get an error message as well.


    Note: I know that Test Execute runs well on that client, when I type: “c:\Program Files\Automated QA\TestExecute 8\bin\TestExecute.exe” – I get a message that a Test Execute instance is already running…


    Here are the two methods code:


     

    function main (p_str_methods_name,p_str_param)


    {


      if(p_str_methods_name == "view_directory_listing") {


        directory_list = view_directory_listing(p_str_param);


        Log.Message("Dir list of " + p_str_param + " is: " + directory_list)


      }


    }


    function view_directory_listing(p_str_unc_path)


    {


      var explorer;


      var comboBox;


      var wndCabinetWClass;


      explorer = Aliases.Explorer;


      explorer.wndShell_TrayWnd.btnStart.ClickButton();


      explorer.wndBaseBar.MenuSite.ToolbarWindow32.ClickItemXY("&Run...", 32, 23, false);


      comboBox = explorer.dlgRun.ComboBox;


      comboBox.SetText("p_str_unc_path");


      comboBox.Edit.Keys("[Enter]");


      wndCabinetWClass = explorer.wndCabinetWClass;


      fileList = slPacker.GetFileListFromFolder(p_str_unc_path);


      wndCabinetWClass.Close();


      return fileList;


    }


    Can you please assist me on that?

    I am sure that something small is missing here....



    Thanks,

    Tomer

  • Hi Tomer,





    You need to specify the application's executable file explicitly - otherwise, the parameters are not passed to TestExecute. As for the already running TestExecute instance, the only thing I can recommend here is to make sure the process is terminated first - for example, you can terminate it from a batch file before running a new instance of TestExecute. Also, you can use the /exit command line parameter to make TestExecute be closed after the test run is complete.


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



    > This is the command line I invoke on the Test Execute machine:

    > C:\Test_complete_proj\VSS_1\VSS_1.pjs /r /p:VSS_1 /u:vss_script_1 /rt:main view_directory_listing \\\\10.1.1.149\\dev



    The case is that you are not calling main() routine with two parameters, but start TestExecute with commend line that contains a set of parameters. Some of these parameters are processed by TestExecute (like /r, /p, etc.) but some are not (like view_directory_listing and \\\\10.1.1.149\\dev).

    So in your test code you should get command line used to start the test, parse it for parameters and perform actions depending on the passed parameters. See "TestComplete command line parameters" help topic for more details.



    > My background is in C [...]

    The above exactly corresponds with the behavior of the ordinary C program - the main() function gets the list of command line arguments that should be processed within main() and reacted accordingly.