Javi
Javi

Reputation: 81

How to work with AutomationElement in C# properly

I want to test a windows application that is formed with Windows Forms. I decided to work with the library AutomationElements.

The problem is that I don't know how to use it properly.

For example: How can I write in a textbox that I'm handling with AutomationElement?

The code is like:

var processStartInfo = new ProcessStartInfo(SATELITE_PATH);
var pSatelite = Process.Start(processStartInfo);
pSatelite.WaitForInputIdle();
Delay(2);
satelite = AutomationElement.RootElement.FindChildByProcessId(pSatelite.Id);
AutomationElement loginUser = satelite.FindDescendentByIdPath(new[] {"frmLogin", "txtUserName"});

I want to write the User in the loginUser. How can I do it?

Really thanks!

Upvotes: 1

Views: 2039

Answers (1)

theartwebreathe
theartwebreathe

Reputation: 371

Use ValuePattern:

var processStartInfo = new ProcessStartInfo(SATELITE_PATH);
var pSatelite = Process.Start(processStartInfo);
pSatelite.WaitForInputIdle();
Delay(2);
satelite = AutomationElement.RootElement.FindChildByProcessId(pSatelite.Id);
AutomationElement loginUser = satelite.FindDescendentByIdPath(new[] {"frmLogin", "txtUserName"});

if (loginUser != null)
{
     ValuePattern valPattern = loginUser.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
     valPattern.SetValue(username);
}

Upvotes: 3

Related Questions