从编码的UI测试中的文本框中读取文本

时间:2012-09-03 14:40:13

标签: winforms coded-ui-tests

Coded UI Test WinEdit类中存在错误/限制:当覆盖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

我找到了设置值here的解决方法:

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

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分: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;

谢谢,