我试图在VS 2013中手动编写一个简单的CodedUI测试。我有一个简单的Windows窗体应用程序,在窗体上有三个TextBoxes(txtA,txtB和txtC)和一个Button(btnAdd)。点击按钮,应用程序将添加在txtA和txtB中输入的数字,并在txtC中显示结果,如下所示:
var a = Convert.ToInt32(txtA.Text);
var b = Convert.ToInt32(txtB.Text);
txtC.Text = (a + b).ToString();
我的编码UI测试如下:
[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);
}
当我运行测试时,它会启动应用程序,等待一段时间然后失败并显示以下错误:
结果讯息:
测试方法SemiAuto.CodedUITest1.CodedUITestMethod1抛出异常: Microsoft.VisualStudio.TestTools.UITest.Extension.UITestControlNotFoundException:播放无法找到具有给定搜索属性的控件。额外细节: TechnologyName:' MSAA' ControlType:'编辑' 姓名:' txtA' ---> System.Runtime.InteropServices.COMException:错误HRESULT已从调用COM组件返回E_FAIL。 结果StackTrace:
在Microsoft.VisualStudio.TestTools.UITest.Playback.Engine.IScreenElement.FindAllDescendants(String bstrQueryId,Object& pvarResKeys,Int32 cResKeys,Int32 nMaxDepth) 在Microsoft.VisualStudio.TestTools.UITest.Playback.ScreenElement.FindAllScreenElement(String queryId,Int32 depth,Boolean singleQueryId,Boolean throwException,Boolean resetSkipStep) 在Microsoft.VisualStudio.TestTools.UITest.Playback.ScreenElement.FindScreenElement(String queryId,Int32 depth,Boolean resetSkipStep) 在Microsoft.VisualStudio.TestTools.UITesting.UITestControl.FindFirstDescendant(String queryId,Int32 maxDepth,Int32& timeLeft) ---内部异常堆栈跟踪结束--- 在Microsoft.VisualStudio.TestTools.UITesting.Playback.MapControlNotFoundException(COMException ex,IPlaybackContext context) 在Microsoft.VisualStudio.TestTools.UITesting.Playback.MapAndThrowComException(COMException innerException,IPlaybackContext context) 在Microsoft.VisualStudio.TestTools.UITesting.Playback.MapAndThrowException(异常异常,IPlaybackContext上下文) 在Microsoft.VisualStudio.TestTools.UITesting.Playback.MapAndThrowException(异常异常,String queryId) 在Microsoft.VisualStudio.TestTools.UITesting.UITestControl.FindFirstDescendant(String queryId,Int32 maxDepth,Int32& timeLeft) 在Microsoft.VisualStudio.TestTools.UITesting.SearchHelper.GetElement(Boolean useCache,ISearchArgument searchArg) 在Microsoft.VisualStudio.TestTools.UITesting.SearchHelper.Search(ISearchArgument searchArg) 在Microsoft.VisualStudio.TestTools.UITesting.UITestControl.FindInternal() 在Microsoft.VisualStudio.TestTools.UITesting.UITestControl.FindControlIfNecessary() 在Microsoft.VisualStudio.TestTools.UITesting.UITestControl.SetPropertyPrivate(String propertyName,Object value) 在Microsoft.VisualStudio.TestTools.UITesting.UITestControl。<> c__DisplayClass3e.b__3d() 在Microsoft.VisualStudio.TestTools.UITesting.CodedUITestMethodInvoker.InvokeMethod [T](Func`1函数,UITestControl控件,Boolean firePlaybackErrorEvent,Boolean logAsAction) 在Microsoft.VisualStudio.TestTools.UITesting.UITestControl.SetProperty(String propertyName,Object value) 在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:第41行
我尝试使用SetProperty
方法,但这也不起作用。
如果我运行录制器并生成代码,它会将控件包装在WinWindow控件中。例如,txtA
TextBox
控件在UIMap
类中包含如下:
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;
}
我不明白为什么会这样做。有人可以解释一下吗?
由于 Dileep Krishnan
答案 0 :(得分:0)
您还需要添加控件的过滤器属性。对我来说,如果你是新手,你必须首先使用记录器,并检查你缺少哪些属性,然后你会更好地了解编码的UI搜索机制是如何工作的。
答案 1 :(得分:0)
我遇到了同样的问题。 要修复它,我必须修改我的应用程序,并为控件设置辅助功能属性。
例如: 添加了AcceessibleName =“inpt1” 然后在测试中我可以通过
进行搜索control.SearchProperties.Add(WinEdit.PropertyNames.Name, "inpt1");
答案 2 :(得分:0)
试试这个......
[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.ControlName, "txtA");
txtB.SearchProperties.Add(WinEdit.PropertyNames.ControlName, "txtB");
txtC.SearchProperties.Add(WinEdit.PropertyNames.ControlName, "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);
}