private static UIAutomation ProcessXmlElement(XElement rootNode, StringDictionary macros)
        {
            VerifyMacrosAreValid(macros);

            _uiAutomation = new UIAutomation();
            
            var xmlData = from el in rootNode.Elements()
                          select el;

            foreach (XElement element in xmlData)
            {
                switch (element.Name.LocalName)
                {
                    case "FormHandle":
                        ProcessFormHandleElement(element);
                        break;

                    case "VerifyScreen":
                        ProcessVerifyScreenElement(element);
                        break;

                    case "InputActions":
                        ProcessInputActionsElement(element, macros);
                        break;

                    case "Messages":
                        ProcessMessagesElement(element);
                        break;
                }
            }

            return _uiAutomation;
        }
        private bool IsValidWindow(UIAutomation uiAutomation, IntPtr windowHandle)
        {
            if (uiAutomation.VerifyScreenOptions.VerifyScreenType == VerifyScreenTypes.PixelColor)
            {
                if (!windowHandle.Equals(IntPtr.Zero))
                {
                    Image windowImage = CaptureWindow(windowHandle);
                    Bitmap bmp = new Bitmap(windowImage);

                    Color pixelColor =
                        bmp.GetPixel(uiAutomation.VerifyScreenOptions.VerifyScreenPixelLocation.X,
                                     uiAutomation.VerifyScreenOptions.VerifyScreenPixelLocation.Y);

                    Color testColor = uiAutomation.VerifyScreenOptions.VerifyScreenPixelColor;

                    return (pixelColor == testColor);
                }

                return false;
            }

            return true;
        }
        /// <summary>
        /// Execute the automation process based on the supplied business object with the configuration information
        /// </summary>
        /// <param name="uiAutomation">The business object which contains configuration data and the steps to execute</param>
        public void ExecuteAutomation(UIAutomation uiAutomation)
        {
            IntPtr windowHandle = IntPtr.Zero;

            if (!string.IsNullOrEmpty(uiAutomation.FormHandleTitleBar))
            {
                windowHandle = FindWindow(null, uiAutomation.FormHandleTitleBar);
            }
            else if (!string.IsNullOrEmpty(uiAutomation.FormHandleClassName))
            {
                windowHandle = FindWindow(uiAutomation.FormHandleClassName, null);
            }
            else if (!string.IsNullOrEmpty(uiAutomation.FormHandleTitleBarRegex))
            {
                Regex regex = new Regex(uiAutomation.FormHandleTitleBarRegex);

                foreach (Process process in Process.GetProcesses())
                {
                    if (regex.IsMatch(process.MainWindowTitle))
                    {
                        windowHandle = FindWindow(null, process.MainWindowTitle);
                        
                        break;
                    }
                }
            }
            else
            {
                throw new Exception("No window handle defined");
            }


            if (!windowHandle.Equals(IntPtr.Zero))
            {
                ShowWindow(windowHandle, SW_RESTORE); 
                SetFocus(windowHandle);
                SetForegroundWindow(windowHandle);

                if (IsValidWindow(uiAutomation, windowHandle))
                {
                    foreach (InputBatch batch in uiAutomation.InputBatches)
                    {
                        if (batch.Timeout > 0)
                        {
                            Thread.Sleep(batch.Timeout);
                        }

                        SimulateKeystrokes(windowHandle, batch);
                    }
                }
            }
        }