使用automationelement

时间:2011-06-14 08:07:15

标签: c# unit-testing windows-forms-designer automationelement

我想测试使用Windows窗体构建的Windows应用程序。我决定使用图书馆自动化元素。

问题在于我不知道如何使用它。例如:我如何在我使用automationelement处理的文本框中书写?

代码如下:

        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"});

我想在loginUser中编写用户。我该怎么办?

非常感谢!

1 个答案:

答案 0 :(得分:3)

使用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);
}