public void Initialize() { GetTestCatgoryInformation(); EndStartedOutLook(); Thread.Sleep(10000); MessageParser.ClearSessions(); string outLookPath = ConfigurationManager.AppSettings["OutLookPath"].ToString(); waittime_window = Int32.Parse(ConfigurationManager.AppSettings["WaitTimeWindow"].ToString()); waittime_item = Int32.Parse(ConfigurationManager.AppSettings["WaitTimeItem"].ToString()); Process p = Process.Start(outLookPath); AutomationElement outlookWindow = null; var desktop = AutomationElement.RootElement; string userName = ConfigurationManager.AppSettings["Office365Account"].ToString(); var condition_Outlook = new PropertyCondition(AutomationElement.NameProperty, "Inbox - " + userName + " - Outlook"); int count = 0; while (outlookWindow == null) { outlookWindow = desktop.FindFirst(TreeScope.Children, condition_Outlook); Thread.Sleep(waittime_item / 10); count += (waittime_item / 10); if (count >= waittime_window) { break; } } Process[] pp = Process.GetProcesses(); if (pp.Count() > 0) { foreach (Process pp1 in pp) { if (pp1.ProcessName != "OUTLOOK" && pp1.ProcessName != "explorer" && pp1.MainWindowHandle != IntPtr.Zero) { AutomationElement element = AutomationElement.FromHandle(pp1.MainWindowHandle); if (element != null) { try { element.SetFocus(); } catch { continue; } } break; } } } Thread.Sleep(waittime_item); try { oApp = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application; } catch { throw new Exception("Get active outlook application failed, please check if outlook is running"); } inboxFolders = oApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox); sentMailFolder = oApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail); deletedItemsFolders = oApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderDeletedItems); draftsFolders = oApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderDrafts); }