无法在CodedUI测试中识别WinEdit控件
本文关键字:识别 WinEdit 控件 测试 CodedUI | 更新日期: 2023-09-27 18:04:09
我试图在VS 2013中手工编写一个简单的CodedUI测试。我有一个简单的windows窗体应用程序与三个文本框(txtA, txtB和txtC)和一个按钮(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);
}
当我运行测试时,它启动应用程序,等待一段时间,然后失败,出现以下错误:
Result Message:
测试方法SemiAuto.CodedUITest1。CodedUITestMethod1抛出异常:uitestcontrolnotfoundexception:播放无法找到具有给定搜索属性的控件。额外的细节:TechnologyName:"MSAA"ControlType:"编辑"名称:"txtA"System.Runtime.InteropServices.COMException: Error HRESULT E_FAIL已经从调用COM组件返回。结果加:
在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(字符串queryId, Int32深度,布尔值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)在Microsoft.VisualStudio.TestTools.UITesting.Playback。MapAndThrowException(异常异常,IPlaybackContext)在Microsoft.VisualStudio.TestTools.UITesting.Playback。MapAndThrowException(异常异常,字符串queryId)在Microsoft.VisualStudio.TestTools.UITesting.UITestControl。FindFirstDescendant(String queryId, Int32 maxDepth, Int32&timeLeft)在Microsoft.VisualStudio.TestTools.UITesting.SearchHelper。getelelement(布尔值,useCache, isearchardment)在Microsoft.VisualStudio.TestTools.UITesting.SearchHelper。搜索(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。调用方法[T](Func ' 1函数,UITestControl控件,Boolean fireplaybackerrovent, Boolean logAsAction)在Microsoft.VisualStudio.TestTools.UITesting.UITestControl。SetProperty(字符串属性名称,对象值)在Microsoft.VisualStudio.TestTools.UITesting.WinControls.WinEdit。set_Text(字符串值)c:'Users'Dileep'Documents'Visual Studio 2013'Projects'CodedUIDemo'SemiAuto'CodedUITest1.cs:line 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
我也有同样的问题。为了解决这个问题,我必须修改我的应用程序,并设置控件的可访问性属性。
例如:新增accesessiblename = "inpt1"然后在test中输入
control.SearchProperties.Add(WinEdit.PropertyNames.Name, "inpt1");
试试这个....
[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);
}
您还需要添加控件的过滤器属性。对我来说,你必须首先使用记录器,如果你是新的,并检查哪些属性你丢失了。然后,您将更好地了解编码UI搜索机制的工作原理。