I'm trying to hand-code a simple CodedUI test in VS 2013. I have a simple windows forms app with three TextBoxes (txtA, txtB and txtC) and a Button (btnAdd) on a form. On clicking the button, the app will add the numbers entered in txtA and txtB and show the result in txtC as below:
var a = Convert.ToInt32(txtA.Text);
var b = Convert.ToInt32(txtB.Text);
txtC.Text = (a + b).ToString();
And my coded UI test is as follows :
[TestMethod]
public void CodedUITestMethod1()
{
ApplicationUnderTest app = ApplicationUnderTest.Launch(@"C:\Users\Dileep\Documents\Visual Studio 2013\Projects\CodedUIDemo\SimpleCalculator\bin\Debug\SimpleCalculator.exe");
WinEdit txtA = new WinEdit(app);
WinEdit txtB = new WinEdit(app);
WinEdit txtC = new WinEdit(app);
WinButton btnAdd = new WinButton(app);
txtA.SearchProperties.Add(WinEdit.PropertyNames.Name, "txtA");
txtB.SearchProperties.Add(WinEdit.PropertyNames.Name, "txtB");
txtC.SearchProperties.Add(WinEdit.PropertyNames.Name, "txtC");
btnAdd.SearchProperties.Add(WinButton.PropertyNames.Name, "btnAdd");
txtA.Text = "50";
txtB.Text = "50";
Mouse.Click(btnAdd);
var result = txtC.GetProperty("Text").ToString();
Assert.AreEqual("100", result);
}
When I run the test, it launches the app, waits for a while and then fails with the following error :
Result Message:
Test method SemiAuto.CodedUITest1.CodedUITestMethod1 threw exception: Microsoft.VisualStudio.TestTools.UITest.Extension.UITestControlNotFoundException: The playback failed to find the control with the given search properties. Additional Details: TechnologyName: 'MSAA' ControlType: 'Edit' Name: 'txtA' ---> System.Runtime.InteropServices.COMException: Error HRESULT E_FAIL has been returned from a call to a COM component. Result StackTrace:
at Microsoft.VisualStudio.TestTools.UITest.Playback.Engine.IScreenElement.FindAllDescendants(String bstrQueryId, Object& pvarResKeys, Int32 cResKeys, Int32 nMaxDepth) at Microsoft.VisualStudio.TestTools.UITest.Playback.ScreenElement.FindAllScreenElement(String queryId, Int32 depth, Boolean singleQueryId, Boolean throwException, Boolean resetSkipStep) at Microsoft.VisualStudio.TestTools.UITest.Playback.ScreenElement.FindScreenElement(String queryId, Int32 depth, Boolean resetSkipStep) at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.FindFirstDescendant(String queryId, Int32 maxDepth, Int32& timeLeft) --- End of inner exception stack trace --- at Microsoft.VisualStudio.TestTools.UITesting.Playback.MapControlNotFoundException(COMException ex, IPlaybackContext context) at Microsoft.VisualStudio.TestTools.UITesting.Playback.MapAndThrowComException(COMException innerException, IPlaybackContext context) at Microsoft.VisualStudio.TestTools.UITesting.Playback.MapAndThrowException(Exception exception, IPlaybackContext context) at Microsoft.VisualStudio.TestTools.UITesting.Playback.MapAndThrowException(Exception exception, String queryId) at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.FindFirstDescendant(String queryId, Int32 maxDepth, Int32& timeLeft) at Microsoft.VisualStudio.TestTools.UITesting.SearchHelper.GetElement(Boolean useCache, ISearchArgument searchArg) at Microsoft.VisualStudio.TestTools.UITesting.SearchHelper.Search(ISearchArgument searchArg) at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.FindInternal() at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.FindControlIfNecessary() at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.SetPropertyPrivate(String propertyName, Object value) at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.<>c__DisplayClass3e.b__3d() at Microsoft.VisualStudio.TestTools.UITesting.CodedUITestMethodInvoker.InvokeMethod[T](Func`1 function, UITestControl control, Boolean firePlaybackErrorEvent, Boolean logAsAction) at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.SetProperty(String propertyName, Object value) at Microsoft.VisualStudio.TestTools.UITesting.WinControls.WinEdit.set_Text(String value) at SemiAuto.CodedUITest1.CodedUITestMethod1() in c:\Users\Dileep\Documents\Visual Studio 2013\Projects\CodedUIDemo\SemiAuto\CodedUITest1.cs:line 41
I tried using the SetProperty method but that also doesn't work.
If I run the recorder and generate code, it wraps controls in a WinWindow control. For example, the txtA TextBox control is wrapped like this in UIMap class :
public class UITxtAWindow : WinWindow
{
public UITxtAWindow(UITestControl searchLimitContainer) :
base(searchLimitContainer)
{
this.SearchProperties[WinWindow.PropertyNames.ControlName] = "txtA";
this.WindowTitles.Add("Form1");
}
public WinEdit UITxtAEdit
{
get
{
if ((this.mUITxtAEdit == null))
{
this.mUITxtAEdit = new WinEdit(this);
this.mUITxtAEdit.WindowTitles.Add("Form1");
}
return this.mUITxtAEdit;
}
}
private WinEdit mUITxtAEdit;
}
I don't understand why it is done like this. Can someone explain this,please ?
Thanks Dileep Krishnan