在 TestComplete 中的 UI 测试实例上调用 .Net 函数
我有一个简单的 wpf 应用程序,其中有一个按钮,可以在单击时增加值。我还有一个返回最新值的函数。默认值是 5。我还在 testcomplete 中有一个 UI 测试,单击按钮 3 次(所以 8 次)。我需要调用 .Net 函数来获取该值并断言它。下面是我的测试代码。
经过一番搜索,我弄清楚了 CLRbridge 的详细信息并实现了它。但是,正如您在下面看到的,UI 测试实例和我在其上调用函数的实例是不同的。因此,该函数返回 5。
我的问题是,如何从 testcomplete 加载的同一实例调用该函数。还是我完全走错了路?我尝试使用 if..then 进行脚本和 UI 测试,但没有任何效果。我尝试过直接实例和调用应用程序域,两者似乎都不起作用。
注意: 我确实知道我可以在 UI 控件中显示该值并验证该控件。但是,我专门尝试此方法来实现项目中所需的更复杂的测试功能。
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();
}
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
应该可以从 TestComplete 执行您需要的操作。但首先,为了避免误解,让我解释一下您尝试的方法的问题:
通过“dotNET”对象寻址类。
当您执行此操作时,TestComplete 在其服务进程中初始化 .NET,将指定的程序集加载到其中,并使用加载到 TestComplete 的 AppDomain 的该程序集的类(尽管位于单独的进程中)。这意味着程序集的这个实例与您测试的应用程序无关。因此,您无法通过 dotNET 对象访问应用程序的数据。
通过测试应用程序的 AppDomain 寻址 Incrementer 程序集。
好的,在这种情况下,您更接近解决方案 - 您使用测试应用程序的 AppDomain,因此您可以访问应用程序的数据。但是,在您的代码中,您创建了 Incr1 类的新实例(通过调用 zctor)。这意味着新的类实例将在构造函数中初始化其计数器,该计数器将为 5。这就是您在代码中获得的值。
所以,正确的做法是:
除非包含当前计数器值的Incr1类的计数器字段是静态字段,否则您需要寻址Incr1类的现有对象以获取属性的当前值,而不是创建新的类实例。实际的实现取决于您在应用程序中存储 Incr1 类实例引用的位置。假设您将引用存储在 MainWindow 对象的 Counter 属性中:
在所描述的情况下,您将能够在 TestComplete 脚本中获取当前计数器值,如下所示:
如果您的设置不同,请描述它 - 我会尝试提供相应的帮助。
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:
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.
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:
In the described case, you will be able to get the current counter value in your TestComplete script as follows:
If your setup is different, please describe it - I will try to help accordingly.