2

Coded UI TestWinEdit类中存在错误/限制:在覆盖OnKeyDown方法或订阅KeyDown文本框中的事件时,无法使用该WinEdit.Text属性。

也就是说,当你有这个...

private void myTextbox_KeyDown(object sender, KeyEventArgs e)
{
    // ...
}

...这行不通:

var edit = new WinEdit(ancestor);
edit.SearchProperties[WinControl.PropertyNames.ControlName] = "myTextbox";
edit.Text = "New value"; // This doesn't work

我找到了在这里设置值的解决方法:

var edit = new WinEdit(ancestor);
edit.SearchProperties[WinControl.PropertyNames.ControlName] = "myTextbox";
Mouse.Click(edit);
System.Windows.Forms.SendKeys.SendWait("New value");

我的问题:有人知道读取值的解决方法吗?

var edit = new WinEdit(Window);
edit.SearchProperties[WinControl.PropertyNames.ControlName] = "myTextbox";
string actual = edit.Text; // This doesn't work
4

2 回答 2

4

我自己找到了一个解决方法:

[DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
public static extern bool SendMessage(IntPtr hWnd, int msg, int wParam, StringBuilder lParam);

const int WM_GETTEXT = 0x000D;

var edit = new WinEdit(Window);
edit.SearchProperties[WinControl.PropertyNames.ControlName] = "myTextbox";
var sb = new StringBuilder(1024);
SendMessage(edit.WindowHandle, WM_GETTEXT, sb.Capacity, sb);
string actual = sb.ToString();
于 2012-09-04T06:35:08.080 回答
1

解决方案是:

假设您有一个带有一个文本框的窗口窗体。

//Launch your Application
ApplicationUnderTest mainWindow = 
ApplicationUnderTest.Launch(@"D:\Samples\YourApplication.exe");

//Search Text box in your windows Form
var username = new WinWindow(mainWindow);
username.SearchProperties[WinControl.PropertyNames.ControlName] = "txtUserName";

//To Set Text or get, Initialize WinEdit object and asign searched object username to WinEdit object editUsername
WinEdit editUsername = new WinEdit(username) {Text = "Pakistan"};

//get text from textbox username
string text = editUserName.Text;

谢谢,

于 2016-04-15T14:18:07.367 回答