Forum Discussion

prasannak's avatar
prasannak
Occasional Contributor
13 years ago

Calling .Net function on UI test instance in TestComplete

 I have a simple wpf app which has a button that increments a value on clicking. I also have a function that returns the latest value. The default value is 5. I also have a UI test in testcomplete that  clicks the button 3 times (so 8). I need to call the .Net function to get this value and assert it. Below is my test code.



 After some search I figured out the CLRbridge details and implemented it. However, As you can see below, the UI test instance and the instance on which I am claling the function are different. So, the function returns 5.



 My question is, how do I invoke the function from the same instance loaded by testcomplete. Or am I going completely the wrong way for this? I tried both script and UI test with if..then, nothing worked. I have tried both direct instance and calling on the appdomain, both doesnt seem to work.



**NOTE:** *I do understand that I can display the value in a UI control and validate the control. However, i am specifically trying this out for a more complex testing functionality we need in a project.*



    function Test2()

    {

      var Increment;

      Increment = 0;

      //Runs the "TCompTest" tested application.

      TestedApps.TCompTest.Run();

      //Clicks the 'button1' button.

      Aliases.TCompTest.HwndSource_MainWindow.MainWindow.Grid.button1.ClickButton();

      //Clicks the 'button1' button.

      Aliases.TCompTest.HwndSource_MainWindow.MainWindow.Grid.button1.ClickButton();

      //Clicks the 'button1' button.

      Aliases.TCompTest.HwndSource_MainWindow.MainWindow.Grid.button1.ClickButton();

      //Increment = dotNET.Incrementer.Incr1.zctor().IntValue(true);

        

    Increment = dotNET.Incrementer.Incr1.zctor().IntValue(true);

    

    **OR**

    

     Increment = Sys.Process("TCompTest").AppDomain("TCompTest.exe").dotNET.Incrementer.Incr1.zctor().IntValue(true)

        

     // if(Increment == 8)

     // {//Posts an information message to the test log.

      Log.Message(Increment);

    //  }

      //Closes the 'HwndSource_MainWindow' window.

      Aliases.TCompTest.HwndSource_MainWindow.Close();

    }

6 Replies

  • prasannak's avatar
    prasannak
    Occasional Contributor
    I can post more information here than the stackoverflow comment. :)



    Thanks for a clear explanation. I undestand that I am accessing a value from a different instance which will initalize its own value. I just couldn't find a way to access the instance that is created by the test. What you have suggested would work.However, since it's wpf, the binding creates the instance of incr1.



    The button is bound to a  backing command



    <Grid> 

        <Button Content="Increment" Command="{Binding IncrCommand}"/> 

        <Label Content="{Binding Increment}"/>

    </Grid>.



    The Incement method just wraps the binding Increment property and returns the value. I don't explicitly create the instance here.  I have attached the project zip here for reference.
  • prasannak's avatar
    prasannak
    Occasional Contributor
    I fixed this with your suggestion. I just accessed the DataContext object from the MainWindow.xaml.cs and set it to a property. I then could access it from the testComplete test and assert the value.



    MainWindow.xaml.cs:

    private Incr1 m_increment;

    public Incr1 Increment {

    get { return m_increment; }

    set { m_increment = value; }}

     

    public MainWindow()

    {

      InitializeComponent();

      Increment = (Incr1) this.DataContext;

    }



    My TestComplete test:

    var Increment = Aliases.TCompTest.HwndSource_MainWindow.MainWindow.Increment;

    Log.Message(Increment.IntValue(true));



    Thanks for your help!
  • prasannak's avatar
    prasannak
    Occasional Contributor
    However, i do have 1 question. How can i access objects that are not directly loaded by the UI in test complete? Do i have to access everything through the UI object that is loaded?
  • AlexeyK's avatar
    AlexeyK
    SmartBear Alumni (Retired)

    Prasanna,


    TestComplete's hierarchy of test objects is based on UI objects and does not include non-visual class instances. So, you can access non-visual class instances through methods and properties of UI objects. A possible alternative is to create a static method or property that will return the needed value. You can add this static method or property to the Program class, for example. However, for me, the approach of using UI objects seems to be easier.

  • xiaoyuandlg's avatar
    xiaoyuandlg
    Occasional Contributor
    Very nice to see this thread and I have to say that I've got the similar problem on the UI test and don't know how to solve until I saw this. Cheers.