How to get an instance of White.Core.UIItems.WindowItems.Window which can automate a WPF Window that is created inside an integration test?

StackOverflow https://stackoverflow.com/questions/7353852

Question

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?

Was it helpful?

Solution

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);
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top