Test Custom Control without Application

Jun 19, 2009 at 3:10 AM

Hello, is there a way to test a custom control or user control without an external application?  Seems like only way to do it is by starting an external application... would like to do something like this:

 

var maskedTextBox = new TestMaskedTextBox
{
    Text = "0",
    Filter = SimpleKeyboardMask.NumbersOnly,
    Name="maskedTextBox"
};

AutomationElement autoElement = AutomationUtilities.GetMagically(maskedTextBox);

Mouse.MoveTo(autoElement.GetClickablePoint());

Microsoft.Test.Keyboard.Type("blah");

Coordinator
Jun 21, 2009 at 6:25 PM
Edited Jun 21, 2009 at 6:26 PM

Hello Dan,

You can certainly tests custom controls without strarting an external application. In fact, the vast majority of our controls tests in WPF are in-proc tests. So what you have above should work. You just need to add your TestMaskedTextBox to your window (or another container), so that UIA can discover it.

Both samples provided as part of the TestApi package demonstrate how to write in-proc tests.

Thanks,
Ivo

Jun 24, 2009 at 4:58 PM

Ivo,

thanks for the reply.  I am glad it is able to do this.  Howver I am unable to find any samples.   I opened up the MSTest sample solution and I only see 2 test files, and both use the OutOfProcessSettings and AutomatedApplication. Am I missing something, or... ?

Thanks,

dan

Aug 4, 2009 at 3:56 PM

Hello - can you give more info on using the toolkit to test custom control w/out an external app?  Thanks.

Developer
Aug 4, 2009 at 10:06 PM

Hi Dan, One thing you can try is our recently released WPF Control Verifier: http://wpf.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=30923.  It is still evolving based on customer feedback but the general idea is to have a stand alone tool that tests common aspects of custom controls.  It will test your control library directly, no need for an app.

Hope that helps!

Alexis

Aug 11, 2009 at 12:31 AM

I'm in the same boat as danblanchard: I have a custom control that I'd like to add unit tests for, but would prefer not to have to embed it in an application just for this.  I do see the sample inproc tests, but they all use an executable.

When you say, "you just need to add your TestMaskedTextBox to your window (or another container)", I'm not sure what you mean.  Can you be more specific here?

 

-mpg

 

Aug 11, 2009 at 12:34 AM

The Control Verifier looks of value, but  not for the situation here -- I (and perhaps danblanchard) have new, unique behaviour in a new control, which (naturally) the Control Verifier wouldn't know how to exercise.

 

Aug 11, 2009 at 12:22 PM

The Control Verifier's style inheritance verifications can verify new behavior including the new DPs that exist on your control.  As long as your control has DPs, it will check for the general heuristics about them.

For the "TestMaskedTextBox to your window", in your unit test you create a window in code and then add your control to it.

 

Jan 14, 2010 at 11:36 PM

Is there a sample test for usercontrol, TestAPI.InProcessApplication assumes the assembly under test has a Window.

If unit test has to create window, it has to be on a separate thread than the test, so tests can execute while the window is open.

After getting the window, for the rest of the test use TestAPI, but how?

 

 

Jan 15, 2010 at 12:40 PM

What exactly would you like to do in your test?  As Ivo was saying, you don't have to start an external application or InProcessApplication to test your custom control.  If you want to though you'll have access to InProcessApplication.MainWindow.  From there you can find your UserControl and access data about it or manipulate it.  The samples with InProcessApplication such as UIAutomationTests.cs show how to do this.  I'm not sure if this answers your question though, so could you expand a little bit about what you would like to do after you get the window. 

Jan 19, 2010 at 9:51 PM

1. The assembly under test has only UserControl, No Window.

2. The Tester assembly has to set up a dummy window to host the UserControl for testing. I cannot create a separate window for each user control.

<font size="2" color="#2b91af"><font size="2" color="#2b91af">

Window

</font></font><font size="2" color="#2b91af">

 

</font>

ucWin = = new  Window();

ucWin.Content = targetObjectUserControlUnderTest;

ucWin.Show();

 

 

3. To add the UserControl before the window is created, lot of code change is required to  WpfApplicationImpl.

 

 

 

 

 

Jan 27, 2010 at 3:57 PM

If you are doing this yourself in the test assembly, why do you need to use AutomatedApplication?

Jan 27, 2010 at 4:54 PM

Yes, that is why not sure what does it mean to write unit test with TestAPI.

Does that mean TestAPI is for BlackBox testing, where the Window is displayed and do operations with the window not knowing the implementation details.

 

Jan 28, 2010 at 3:57 PM

No, TestAPI has many uses.  With regards to AutomatedApplication, the main scenarios including both black box and white box testing of an Application.  OutOfProcessApplication is used generally for a more black box approach whereas InProcessApplication can be used for gray box/white box testing. 

For your scenario you are testing Controls instead of an Application.  You can still use AutomatedApplication to control the window startup/shutdown or you can create the window yourself like you're already doing.