Torbjörn Kalin
Torbjörn Kalin

Reputation: 1986

Read text from textbox in Coded UI Tests

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

Upvotes: 2

Views: 3504

Answers (2)

Aqdas
Aqdas

Reputation: 940

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,

Upvotes: 1

Torbjörn Kalin
Torbjörn Kalin

Reputation: 1986

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

Upvotes: 4

Related Questions