public static void BuildTree(AutomationTree tree, Stack <string> windowStack, int depth = 0) { if (tree.CurrentNode is AutomationWindowNode) { AutomationWindowNode windowNode = tree.CurrentNode as AutomationWindowNode; AutomationWindow window = windowNode.Window; windowStack.Push(windowNode.Window.Name); depth++; while (!windowNode.Terminated) { Console.WriteLine(string.Format("\nDepth: {0}", string.Join(" -> ", windowStack.Reverse()))); window.PrintActions(windowNode.Terminatable); //List<AutomationWindow> previousWindows = FindPrevious(window); int index = GetChoice(windowNode.Terminatable); if (index == -1) { windowNode.Terminated = true; return; } AutomationAction action = Actions[window.Actions[index], window.Name]; action.Compile = true; AutomationActionNode actionNode = new AutomationActionNode(action, windowNode) { Depth = depth }; actionNode.GetArguments(); windowNode.Add(actionNode); windowNode.Terminated = action.Terminating; tree.CurrentNode = actionNode; BuildTree(tree, windowStack, depth); } depth--; windowStack.Pop(); } else if (tree.CurrentNode is AutomationActionNode) { AutomationActionNode actionNode = tree.CurrentNode as AutomationActionNode; AutomationWindowNode parentWindowNode = actionNode.Parent as AutomationWindowNode; AutomationAction action = actionNode.Action; depth++; foreach (string windowName in action.Windows) { AutomationWindow window = Windows[windowName]; AutomationWindowNode windowNode = new AutomationWindowNode(window, actionNode) { Depth = depth }; actionNode.Add(windowNode); tree.CurrentNode = windowNode; BuildTree(tree, windowStack, depth); } depth--; } }
public static void Run() { // Define All Windows Windows = new AutomationWindowCollection { new MainWindow("NotepadWindow", "notepad.exe", "WriteText", "OpenFile"), new AutomationWindow("SaveWindow", "Save", "DontSave", "Cancel"), new AutomationWindow("OpenFileWindow", "OpenFile", "Cancel") }; // Define All Actions Actions = new AutomationActionCollection { new AutomationAction("WriteText", "NotepadWindow", false, "Text") { Source = @" UIADocument textEditor = window.GetControl<UIADocument>(new Identifier() { Name = ""Text Editor"" }); textEditor.Write(args[0]); " }, new AutomationAction("OpenFile", "NotepadWindow", false) { Source = "Console.WriteLine(\"In OpenFile\");", Windows = new [] { "OpenFileWindow", "SaveWindow" } }, new AutomationAction("Save", "SaveWindow", true) { Source = "Console.WriteLine(\"In Save\");", Windows = new [] { "OpenFileWindow" } }, new AutomationAction("DontSave", "SaveWindow", true) { Source = "Console.WriteLine(\"In DontSave\");", Windows = new [] { "OpenFileWindow" } }, new AutomationAction("Cancel", "SaveWindow", true) { Source = "Console.WriteLine(\"In Cancel\");" }, new AutomationAction("OpenFile", "OpenFileWindow", true, "File Path") { Source = "Console.WriteLine(\"In OpenFile\");" }, new AutomationAction("Cancel", "OpenFileWindow", true) { Source = "Console.WriteLine(\"In OpenFile\");" } }; // Verify Windows and Actions if (!VerifyRelationships()) { return; } // Create and build Automation Tree AutomationTree tree = new AutomationTree(Windows["NotepadWindow"] as MainWindow); BuildTree(tree, new Stack <string>()); Console.WriteLine(); // Print Automation Tree Console.WriteLine(tree); Console.WriteLine(); // Build Compilation Set CompilerResults compilerResults = Compile(Actions); PrintStatus("Compiling", compilerResults.Errors.Count == 0, null); foreach (CompilerError ce in compilerResults.Errors) { Console.WriteLine(ce.ErrorNumber + ": " + ce.ErrorText); } Console.WriteLine("Run Tree"); tree.Run(compilerResults.CompiledAssembly); }