编码 UI:已从对 COM 组件的调用返回 HRESULT E_FAIL

Coded UI: HRESULT E_FAIL has been returned from a call to a COM component

我正在尝试创建运行我的应用程序并在字段中输入文本的测试 CodedUI.This 是我的代码:

 var app = ApplicationUnderTest.Launch(@"C:\app.exe");
 var loginEdit = new WinEdit();
 loginEdit.SearchProperties.Add(WinEdit.PropertyNames.ControlName, "textEditLogin");
 loginEdit.Text = "test005";

错误:System.NotSupportedException: SetProperty "Text" is not supported on control type: Window.

如果我使用此代码:

Keyboard.SendKeys(loginEdit, "test005");

我有错误:Microsoft.VisualStudio.TestTools.UITest.Extension.PlaybackFailureException: (Failed to get the message for an exception of type Microsoft.VisualStudio.TestTools.UITest.Extension.PlaybackFailureException due to an exception.) ---> System.Runtime.InteropServices.COMException: HRESULT E_FAIL has been returned from a call to a COM component

这是我的 WinEdit 的属性: WinEdit

可能是什么问题?

问题出在对象浏览器中,我在那里没有看到我需要的控件。 我使用记录器定位我的 WinEdit 并编写了这段代码:

var loginWindow = new WinWindow();


loginWindow.SearchProperties[WinWindow.PropertyNames.Name] = "layoutControl1";
loginWindow.SearchProperties.Add(new PropertyExpression(WinWindow.PropertyNames.ClassName, "WindowsForms10.Window", PropertyExpressionOperator.Contains));

var loginEdit = new WinEdit(loginWindow);
loginEdit.WindowTitles.Add("layoutControl1");

Keyboard.SendKeys(loginEdit, "test005");