【问题标题】:Calling .Net function on UI test instance in TestComplete在 TestComplete 中的 UI 测试实例上调用 .Net 函数
【发布时间】:2012-02-17 17:09:11
【问题描述】:

我有一个简单的 wpf 应用程序,它有一个在点击时增加值的按钮。我还有一个返回最新值的函数。默认值为 5。我在 testcomplete 中还有一个 UI 测试,它点击了 3 次按钮(所以 8)。我需要调用 .Net 函数来获取这个值并断言它。下面是我的测试代码。

经过一番搜索,我找出了 CLRbridge 的详细信息并实施了它。但是,正如您在下面看到的,UI 测试实例和我在其上调用函数的实例是不同的。所以,函数返回 5。

我的问题是,如何从 testcomplete 加载的同一个实例中调用该函数。还是我为此走错了路?我用 if..then 尝试了脚本和 UI 测试,但没有任何效果。我已经尝试过直接实例和调用appdomain,似乎都不起作用。

注意: 我明白我可以在 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();
}

【问题讨论】:

    标签: wpf testcomplete gui-testing ui-testing


    【解决方案1】:

    应该可以从 TestComplete 做你需要的事情。但首先,为避免误解,让我解释一下您尝试的方法存在的问题:

    1. 通过“dotNET”对象寻址一个类。 当您执行此操作时,TestComplete 在其服务进程中初始化 .NET,将指定的程序集加载到其中,并使用加载到 TestComplete 的 AppDomain 的该程序集的类(尽管存在于单独的进程中)。这意味着您的程序集的这个实例与您测试的应用程序无关。因此,您无法通过 dotNET 对象访问应用程序的数据。

    2. 通过测试应用程序的 AppDomain 寻址 Incrementer 程序集。 好的,在这种情况下,您更接近解决方案 - 您使用测试应用程序的 AppDomain,因此您可以访问应用程序的数据。但是,在您的代码中,您创建了一个 Incr1 类的新实例(通过调用 zctor)。这意味着新类实例将在构造函数中初始化其计数器,它将为 5。这就是您在代码中获得的值。

    所以,正确的做法是:

    除非包含当前计数器值的Incr1类的计数器字段是静态字段,否则需要寻址Incr1类的现有对象来获取属性的当前值,而不是创建新的类实例。实际实现将取决于您在应用程序中存储 Incr1 类实例引用的位置。假设,您将引用存储在 MainWindow 对象的 Counter 属性中:

    // 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();
    

    在上述情况下,您将能够在 TestComplete 脚本中获取当前计数器值,如下所示:

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

    如果您的设置不同,请描述一下 - 我会尽力提供相应的帮助。

    【讨论】:

    • 感谢您的清晰解释。我不明白我正在从另一个实例访问一个值,该实例将初始化它自己的值。主要是因为我找不到访问由测试创建的实例的方法。但是,您的建议会起作用,因为它是 wpf,所以绑定会创建 incr1 的实例。 XAML 是这样的:<Grid> <Button Content="Inc" Command="{Binding IncrCommand}"/> <Label Content="{Binding Increment}"/></Grid>Incement 方法 只是包装了 binding Increment 属性 并返回值。我没有在这里明确地创建实例。
    • 顺便说一句,我刚刚使用了您的建议,我从 MainWindow 访问了 Incr1 实例并可以获得更新的值。这工作正常。我刚刚从 MainWindow.cs 访问 DataContext 对象并将值设置为属性并访问它。它现在工作正常。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多