用white做了个简单的实践,win7自带的calculator。代码如下:
{
class Launch_Cal
{
public static void launchCal()
{
//Launch Calculator
string path;
string processname;
path = @"C:/Windows/System32/calc.exe";
processname = @"calc";
//If the application have launched, Close it
Kill_Application.Close_application(processname);
Core.Application application = Core.Application.Launch(path);
Assert.IsNotNull(application);
Window Window = null;
int numWaits = 0;
//use the smart delay-loop technique instead of the arbitrarily long Sleep technique
do
{
Window = application.GetWindow("Calculator");
++numWaits;
Thread.Sleep(2000);
} while (Window == null && numWaits < 10);
if (Window == null)
{
Log.Instance.Error("Unable to get Calculator");
}
else
{
Log.Instance.Info("Found Calculator window");
}
//Press F1 button
AttachedKeyboard keyboard = Window.Keyboard;
keyboard.HoldKey(KeyboardInput.SpecialKeys.F1);
keyboard.LeaveKey(KeyboardInput.SpecialKeys.F1);
Thread.Sleep(5000);
//I have no idea how to use white to recognize the help window, so I have to use UI automation.
AutomationElement helper = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Windows Help and Support"));
AutomationElement closebutton = helper.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Close"));
InvokePattern invokepattern = closebutton.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
invokepattern.Invoke();
Window.DisplayState = DisplayState.Minimized;
Assert.AreEqual(DisplayState.Minimized, Window.DisplayState);
Thread.Sleep(5000);
Window.DisplayState = DisplayState.Restored;
Assert.AreEqual(DisplayState.Restored, Window.DisplayState);
Window.Close();
}
}
}