示例#1
0
        public static CompilerResults Compile(AutomationActionCollection Actions)
        {
            string compileString = string.Empty;
            Dictionary <string, AutomationActionCollection> compileSet = new Dictionary <string, AutomationActionCollection>();

            foreach (AutomationAction action in Actions)
            {
                if (!action.Compile)
                {
                    continue;
                }
                if (compileSet.ContainsKey(action.Window))
                {
                    compileSet[action.Window].Add(action);
                }
                else
                {
                    compileSet.Add(action.Window, new AutomationActionCollection {
                        action
                    });
                }
            }
            foreach (KeyValuePair <string, AutomationActionCollection> pair in compileSet)
            {
                string classBody = string.Empty;
                foreach (AutomationAction action in pair.Value)
                {
                    classBody += MethodString(action.Name, action.Source);
                }
                compileString += ClassString(pair.Key, classBody);
            }

            using (CSharpCodeProvider cscp = new CSharpCodeProvider())
            {
                CompilerParameters compilerParams = new CompilerParameters()
                {
                    GenerateInMemory = true
                };

                foreach (String dependency in Constants.DEPENDENCIES)
                {
                    compilerParams.ReferencedAssemblies.Add(dependency);
                }

                //compileString = NamespaceString("AutomateMatePOC", compileString);
                compileString = Constants.USING + compileString;

                //Console.WriteLine("Source:\n" + compileString);

                return(cscp.CompileAssemblyFromSource(compilerParams, compileString));
            }


            string MethodString(string name, string body)
            {
                return(Constants.METHOD.Replace(Constants.METHOD_NAME, name).Replace(Constants.METHOD_BODY, body));
            }

            string ClassString(string name, string body)
            {
                return(Constants.CLASS.Replace(Constants.CLASS_NAME, name).Replace(Constants.CLASS_BODY, body));
            }

            string NamespaceString(string name, string body)
            {
                return(Constants.NAMESPACE.Replace(Constants.NAMESPACE_NAME, name).Replace(Constants.NAMESPACE_BODY, body));
            }
        }
示例#2
0
        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);
        }