Forum Discussion

sugia279's avatar
sugia279
Occasional Contributor
9 years ago

How to use FindAllChildren function by C#

Hi All,

I try to use FindAllChildren function by C#, but the exception NulReferenceException is threw like the below image.

I used code at https://support.smartbear.com/viewarticle/82015/?q=C%23+script#_ga=1.55551336.2128836049.1463127932.

 

Code:

 

Why? Please help me to solve this issue.

Thanks and regards,

Hien Su.

 

  • HKosova's avatar
    HKosova
    9 years ago

    Just to clarify.

     

    If you want to run TestComplete projects from C#, you should use the TestComplete COM object, TestComplete.TestCompleteApplication. See examples here.

    If you want to use the TestComplete engine to interact with applications and objects -- find objects, click objects, input text and so on, -- that's a different thing. Assuming you use TestComplete 12, the recommended way to do that is to use the TestComplete REST API. You can call it by using any C# REST client, such as RestSharp. This way you can write your own tool like TestLeft (TestLeft is built around this API). It requires some knowledge of REST APIs and JSON, though.

     

    To enable the REST API in TestComplete:

    • Launch TestComplete.
    • Go to File | Install Extensions and enable the REST API plugin in the Common category.

    After that you can browse the API documentation at:

    http://localhost:2377/1/docs/index.html

     

    Note that the API works only when TestComplete is running.

     


    The first API call should be

    POST http://localhost:2377/1/initialize

    to initialize the test engine. Then you can query the object tree (the Sys tree), get object properties, call object methods, etc.

    For example, a call to

    Sys.FindAllChildren("UserName", user_name, 1)

    can be done with the REST API as follows:

    POST http://localhost:2377/1/sys/4a9f9237-c1f7-4b80-9573-8440d7b37dc6/find_all?depth=1
    { "UserName": user_name }

    Data is passed and returned as JSON, which is easy enough to parse.

     

    Hope this helps!

  • baxatob's avatar
    baxatob
    Community Hero

    Hi,

     

    Can you provide the details of the exception?

    Click on "Copy exception detail to the clipboard" and then paste it here.

    • sugia279's avatar
      sugia279
      Occasional Contributor

      Here is the exception detail:

      System.NullReferenceException was caught
      HResult=-2147467261
      Message=Object reference not set to an instance of an object.
      Source=AutomatedQA.script
      StackTrace:
      at AutomatedQA.script.var.VarDelegate.CallNoPop(var[] args)
      at AutomatedQA.script.var.VarDelegate.CallStatic(var[] args)
      at Model.UITestCompleteControl.FindProcessMultiple() in d:\Work\CIPAutomationTool\Model\UIControl\UITestCompleteControl.cs:line 88
      InnerException:

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    Not exactly an answer to your question, but if you want to write tests in Visual Studio / C#, consider using TestLeft. It's based on the TestComplete engine, but supports the C# language natively.

     

    In TestLeft tests, the FindAll code is pretty short and simple:

    using System;
    using SmartBear.TestLeft;
    
    // Find all processes run from the logged in user
    IEnumerable<IProcess> processes = Driver.FindAll<IProcess>(new ProcessPattern(){}.Add("UserName", Environment.UserName));
    foreach(IProcess p in processes)
    {
        Console.WriteLine(p.CommandLine);
    }
    • sugia279's avatar
      sugia279
      Occasional Contributor

      Hi 

      The TestLeft is ok, but I have to buy the license to use it.

      And I want to build the auto tool that use TestComplete engine like TestLeft.

      Thanks,

      Hien Su

      • HKosova's avatar
        HKosova
        SmartBear Alumni (Retired)

        Just to clarify.

         

        If you want to run TestComplete projects from C#, you should use the TestComplete COM object, TestComplete.TestCompleteApplication. See examples here.

        If you want to use the TestComplete engine to interact with applications and objects -- find objects, click objects, input text and so on, -- that's a different thing. Assuming you use TestComplete 12, the recommended way to do that is to use the TestComplete REST API. You can call it by using any C# REST client, such as RestSharp. This way you can write your own tool like TestLeft (TestLeft is built around this API). It requires some knowledge of REST APIs and JSON, though.

         

        To enable the REST API in TestComplete:

        • Launch TestComplete.
        • Go to File | Install Extensions and enable the REST API plugin in the Common category.

        After that you can browse the API documentation at:

        http://localhost:2377/1/docs/index.html

         

        Note that the API works only when TestComplete is running.

         


        The first API call should be

        POST http://localhost:2377/1/initialize

        to initialize the test engine. Then you can query the object tree (the Sys tree), get object properties, call object methods, etc.

        For example, a call to

        Sys.FindAllChildren("UserName", user_name, 1)

        can be done with the REST API as follows:

        POST http://localhost:2377/1/sys/4a9f9237-c1f7-4b80-9573-8440d7b37dc6/find_all?depth=1
        { "UserName": user_name }

        Data is passed and returned as JSON, which is easy enough to parse.

         

        Hope this helps!