如何获取 White.Core.UIItems.WindowItems.Window 的实例,它可以自动化在集成测试中创建的 WPF 窗口?

发布于 2024-12-03 09:53:50 字数 565 浏览 0 评论 0原文

我使用 White 自动化框架创建了一个集成测试。这是集成测试的开始:

var app       = Application.Launch("WPFIntegrationTest.exe");
var window    = app.GetWindow("MainWindow");

但是,我希望获得 White.Core.UIItems.WindowItems.Window 的实例,而无需在新进程中“启动”应用程序。然后,这将允许我注入 MainWindow 具有的任何依赖项,以便我可以有选择地模拟/存根它们。

作为我正在寻找的示例,这是我希望可以编写的代码:

var myWindow  = new MainWindow();
var window    = White.Core.UIItems.WindowItems.Window(myWindow);

有什么方法可以使用 White 自动化框架来实现此目的吗?

I have created an integration test using the White automation framework. This is the start of the integration test:

var app       = Application.Launch("WPFIntegrationTest.exe");
var window    = app.GetWindow("MainWindow");

However, I would like to get an instance of White.Core.UIItems.WindowItems.Window without 'launching' the application in a new process. This would then allow me to inject any dependencies that MainWindow has, so that I can selectively mock/stub them out.

As an example of what I'm looking for, this is the code I wish I could write:

var myWindow  = new MainWindow();
var window    = White.Core.UIItems.WindowItems.Window(myWindow);

Is there any way I can achieve this using the White automation framework?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟若柳尘 2024-12-10 09:53:50

听起来您所描述的是在同一进程中运行白色测试代码和被测应用程序 - 是这样吗?

据我所知,白人作者不鼓励这样做 - 请参阅此白人常见问题解答页面上的问题 #9:

http://white.codeplex.com/wikipage?title=Other%20questions

白色测试可以与应用程序在同一进程中运行吗?

<块引用>

没有。白色不适合在这种模式下工作,因为这可能会导致线程问题。原则上,将测试代码与被测应用程序代码结合起来并不是一个好主意,因为它们将彼此独立地发展。

不过,也许下面的文章将帮助您做您想做的事:

http://msdn .microsoft.com/en-us/magazine/cc163864.aspx

在上面的文章中,检查“图 5 - 启动应用程序”中的代码。

作者的方法似乎有点像您所描述的 - 他的代码似乎加载一个程序集,使用它创建 Form 对象的实例,然后启动一个单独的线程,在该线程中调用 Application.Run 来启动 Form。

一旦 AUT 运行,您应该能够在主线程中使用 White 附加它。

也许该技术会让您开始按照您希望的方式操纵 AUT?

为了方便起见,这里使用文章中的图 5:

static Form LaunchApp(string exePath, string formName)
{
  Thread.Sleep(delay);
  Assembly a = Assembly.LoadFrom(exePath);
  Type formType = a.GetType(formName);
  Form resultForm = (Form)a.CreateInstance(formType.FullName);
  Thread t = new Thread(new ThreadStart(new AppState(resultForm).RunApp));
  t.ApartmentState = ApartmentState.STA;
  t.IsBackground = true;
  t.Start();
  return resultForm;
}

private class AppState
{
  public AppState(Form f) { FormToRun = f; }
  public readonly Form FormToRun;
  public void RunApp()
  {
    Application.Run(FormToRun);
  }
}

It sounds like what you're describing is running your White test code and the application under test in the same process - is that the case?

From what I can tell the White author discourages that - see question #9 on this White FAQ page:

http://white.codeplex.com/wikipage?title=Other%20questions

Can white test run in the same process as the application?

No. White is not designed to work in this mode as this can cause threading issues. All in principle it is not a good idea to couple test code with application under test code as they would evolve independent from each other.

Still, maybe the article below will help you do what you want:

http://msdn.microsoft.com/en-us/magazine/cc163864.aspx

In the above article, check the code in "Figure 5 - Launching the App".

The author's approach seems a little like what you're describing - his code appears to load an assembly, use it to create an instance of a Form object, then launch a separate thread in which Application.Run is called to launch the Form.

Once your AUT is running, you should be able to attach to it using White in the main thread.

Perhaps that technique will get you started on manipulating the AUT the way you were hoping to do?

Just for convenience here's Figure 5 from the article:

static Form LaunchApp(string exePath, string formName)
{
  Thread.Sleep(delay);
  Assembly a = Assembly.LoadFrom(exePath);
  Type formType = a.GetType(formName);
  Form resultForm = (Form)a.CreateInstance(formType.FullName);
  Thread t = new Thread(new ThreadStart(new AppState(resultForm).RunApp));
  t.ApartmentState = ApartmentState.STA;
  t.IsBackground = true;
  t.Start();
  return resultForm;
}

private class AppState
{
  public AppState(Form f) { FormToRun = f; }
  public readonly Form FormToRun;
  public void RunApp()
  {
    Application.Run(FormToRun);
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文