class DemoDelayedWindow
{
static DateTime eventStartTime;
public static void NoUISyncLeadToErrorDemo()
{
CacheRequest cacheRequest = new CacheRequest();
cacheRequest.TreeScope = TreeScope.Element;
cacheRequest.Add(AutomationElement.NameProperty);
using (cacheRequest.Activate())
{
AutomationElement wpfRoot = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "Window1"));
AutomationElement btnOpenNewWindow = wpfRoot.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "ButtonOpenFlashWindow"));
InvokePattern invokPtn = (InvokePattern)btnOpenNewWindow.GetCurrentPattern(InvokePattern.Pattern);
invokPtn.Invoke();
Console.WriteLine("Button Clicked...");
AutomationElement newWindow = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "FlashWindow"));
Console.WriteLine(newWindow.Current.Name);
}
Console.WriteLine("Test finishes...................");
}
public static void SimpleSleepSyncDemo()
{
DateTime startTime = DateTime.Now;
CacheRequest cacheRequest = new CacheRequest();
cacheRequest.TreeScope = TreeScope.Element;
cacheRequest.Add(AutomationElement.NameProperty);
using (cacheRequest.Activate())
{
AutomationElement wpfRoot = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "Window1"));
AutomationElement btnOpenNewWindow = wpfRoot.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "ButtonOpenFlashWindow"));
InvokePattern invokPtn = (InvokePattern)btnOpenNewWindow.GetCurrentPattern(InvokePattern.Pattern);
invokPtn.Invoke();
Console.WriteLine("Button Clicked...");
Console.WriteLine("Sleeping 6 seconds to wait...");
System.Threading.Thread.Sleep(1000 * 6);
AutomationElement newWindow = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "FlashWindow"));
Console.WriteLine(newWindow.Current.Name);
}
Console.WriteLine("Test finishes...................");
Console.WriteLine("Test cost {0} seconds", (DateTime.Now - startTime).TotalSeconds);
}
public static void PollingSyncDemo()
{
DateTime startTime = DateTime.Now;
CacheRequest cacheRequest = new CacheRequest();
cacheRequest.TreeScope = TreeScope.Element;
cacheRequest.Add(AutomationElement.NameProperty);
using (cacheRequest.Activate())
{
AutomationElement wpfRoot = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "Window1"));
AutomationElement btnOpenNewWindow = wpfRoot.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "ButtonOpenFlashWindow"));
InvokePattern invokPtn = (InvokePattern)btnOpenNewWindow.GetCurrentPattern(InvokePattern.Pattern);
invokPtn.Invoke();
Console.WriteLine("Button Clicked...");
int timeout = 10 * 1000;
while (true)
{
AutomationElement newWindow = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "FlashWindow"));
if (newWindow == null)
{
Console.WriteLine("Cannot find expected window. Sleep for a short time");
System.Threading.Thread.Sleep(500);
timeout -= 300;
if (timeout <= 0)
{
throw new TimeoutException("Window probing process times out after 10 seconds.");
}
}
else
{
Console.WriteLine(newWindow.Current.Name);
break;
}
}
}
Console.WriteLine("Test finishes...................");
Console.WriteLine("Test cost {0} seconds", (DateTime.Now - startTime).TotalSeconds);
}
public static void EventSyncDemo()
{
eventStartTime = DateTime.Now;
CacheRequest cacheRequest = new CacheRequest();
cacheRequest.TreeScope = TreeScope.Element;
cacheRequest.Add(AutomationElement.NameProperty);
using (cacheRequest.Activate())
{
AutomationElement wpfRoot = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "Window1"));
AutomationElement btnOpenNewWindow = wpfRoot.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "ButtonOpenFlashWindow"));
InvokePattern invokPtn = (InvokePattern)btnOpenNewWindow.GetCurrentPattern(InvokePattern.Pattern);
Console.WriteLine("Register Event");
AutomationEventHandler eventHandler = new AutomationEventHandler(OnWindowOpenOrClose);
Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent, AutomationElement.RootElement, TreeScope.Children, eventHandler);
Automation.AddAutomationEventHandler(WindowPattern.WindowClosedEvent, AutomationElement.RootElement, TreeScope.Subtree, eventHandler);
invokPtn.Invoke();
Console.WriteLine("Button Clicked and Wait...");
Console.ReadLine();
}
}
static void OnWindowOpenOrClose(object src, AutomationEventArgs e)
{
Console.WriteLine("OnWindowOpenOrClose event triggers");
if (e.EventId != WindowPattern.WindowOpenedEvent)
{
Console.WriteLine("It is NOT WindowOpenedEvent.Ignore");
return;
}
AutomationElement sourceElement;
try
{
sourceElement = src as AutomationElement;
if (sourceElement.Current.AutomationId == "FlashWindow")
{
Console.WriteLine(sourceElement.Current.Name);
}
}
catch (ElementNotAvailableException)
{
return;
}
Console.WriteLine("Test finishes...................");
Console.WriteLine("Test cost {0} seconds", (DateTime.Now - eventStartTime).TotalSeconds);
}
}