Read text from textbox in Coded UI Tests

邮差的信 提交于 2019-12-11 02:20:02

问题


There is a bug/limitation in the Coded UI Test WinEdit class: when overriding the OnKeyDown method or subscribing to the KeyDown event in a text box, it is not possible to use the WinEdit.Text property.

That is, when you have this...

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

...this won't work:

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

I've found a work-around for setting the value here:

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

My question: does anyone know a work-around for reading the value?

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

回答1:


I found a work-around myself:

[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();



回答2:


The Solution is :

Suppose you have one window form having one text box.

//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;

Thanks,



来源:https://stackoverflow.com/questions/12249948/read-text-from-textbox-in-coded-ui-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!