Frage

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();
}
War es hilfreich?

Lösung

It should be possible to do what you need from TestComplete. But first of all, to avoid misunderstanding, let me explain the problems with the approaches you tried:

  1. Addressing a class through the "dotNET" object. When you do this, TestComplete initializes .NET in its service process, loads the specified assembly into it, and works with the classes of this assembly loaded to TestComplete's AppDomain (though living in a separate process). This means that this instance of your assembly has nothing to do with your tested application. So, you can't access your application's data through the dotNET object.

  2. Addressing the Incrementer assembly through the tested application's AppDomain. OK, in this case you are closer to a solution - you work with the AppDomain of the tested application, so you can access the application's data. However, in your code, you create a new instance of the Incr1 class (via calling zctor). This means that the new class instance will initialize its counter in the constructor, and it will be 5. And this is the value you are getting in your code.

So, the right approach:

Unless the counter field of the Incr1 class containing the current counter value is a static field, you need to address an existing object of the Incr1 class to get the current value of the property, not to create a new class instance. The actual implementation will depend on where you are storing the Incr1 class instance reference in your application. Let's suppose, you store the reference in the Counter property of the MainWindow object:

// Creating an instance of the class somewhere in your code
MainWindow.Counter = new Incr1();

// ...    

// And this line of code is in the button click handler
MainWindow.Counter.Increment();

In the described case, you will be able to get the current counter value in your TestComplete script as follows:

  var MainWnd = Aliases.TCompTest.HwndSource_MainWindow.MainWindow;
  Log.Message(MainWnd.Counter.IntValue(true));

If your setup is different, please describe it - I will try to help accordingly.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top