A useful abstract baseModule for White

Dec 7, 2009 at 4:01 AM

public abstract class BaseModule
    {
        protected Application app;
        protected Window mainWin;
        protected Window modalWin;

        /// <summary>
        /// Author: Leo Zhang
        /// Date: 30/11/2009
        /// Description: Close all application windows and wait for a while before next test
        /// </summary>
        public static void CloseAllApplications()
        {
            Process[] appProcesses = Process.GetProcessesByName(StaticData.AspectProcessName);
            System.Threading.Thread.Sleep(1000);
            if (appProcesses.Length > 0 && appProcesses != null)
            {
                foreach (Process appProcess in appProcesses)
                {
                    appProcess.Kill();
                    System.Threading.Thread.Sleep(2000);
                }
            }
        }

        /// <summary>
        /// Author: Leo Zhang
        /// Date: 30/11/2009
        /// Description: Open application by InternetExploer with the uri 
        /// </summary>
        public static void OpenApplicationByIE(string uri)
        {
            InternetExplorer.Launch(uri);
        }

        /// <summary>
        /// Author: Leo Zhang
        /// Date: 30/11/2009
        /// Description: Open application by InternetExploer with the uri & title
        /// </summary>
        public static void OpenApplicationByIE(string uri, string title)
        {
            InternetExplorer.Launch(uri, title);
        }

        /// <summary>
        /// Author: Leo Zhang
        /// Date: 30/11/2009
        /// Description: try to find application in processed and attach it and captrue the application window
        /// </summary>
        public static Window FindAplicationAndCaptrueWindow(string processName)
        {
            //Find Aspect application in processes and capture the window
            Process[] processes = Process.GetProcessesByName(processName);
            Application host = Application.Attach(processes[0].Id);
            Window mainWindow = host.GetWindow(StaticData.AspectWindowTitleWithoutLogin, White.Core.Factory.InitializeOption.NoCache);
            mainWindow.DisplayState = White.Core.UIItems.WindowItems.DisplayState.Maximized;
            return mainWindow;
        }

        /// <summary>
        /// Author: Leo Zhang
        /// Date: 30/11/2009
        /// Description: get the modal window belong to parentWin, with the automationID & window title
        /// param: millisecond
        /// </summary>
        public static Window GetModalWindow(Window parentWin, string automationId, string winTitle)
        {
            Window modalWin;
            try
            {
                modalWin = parentWin.ModalWindow(SearchCriteria.ByAutomationId(automationId).AndByText(winTitle), White.Core.Factory.InitializeOption.NoCache);
            }
            catch (White.Core.WhiteException)
            {
                throw new Exception("Error occured");
            }
            return modalWin;
        }

        /// <summary>
        /// Author: Leo Zhang
        /// Date: 30/11/2009
        /// Description: get the modal window belong to parentWin, with the automationID & control name
        /// param: millisecond
        /// </summary>
        public static TControl GetControl<TControl>(Window parentWin, string automationId, string controlName) where TControl : UIItem
        {
            TControl tControl;
            try
            {
                tControl = parentWin.Get<TControl>(SearchCriteria.ByAutomationId(automationId).AndByText(controlName));
            }
            catch (White.Core.WhiteException)
            {
                throw new Exception("Error occured");
            }
            return tControl;

        }

        /// <summary>
        /// Author: Leo Zhang
        /// Date: 30/11/2009
        /// Description: sleep for a while
        /// param: millisecond
        /// </summary>
        public static void SecondsSystemSleep(int second)
        {
            int ms = 1000 * second;
            System.Threading.Thread.Sleep(ms);
        }

        /// <summary>
        /// Author: Leo Zhang
        /// Date: 04/12/2009
        /// Description: search window by automationId and Title, then Close it
        /// </summary>
        /// <param name="parentWin"></param>
        /// <param name="AutomationId"></param>
        /// <param name="winTitle"></param>
        public static void WindowClose(Window parentWin, string AutomationId, string winTitle)
        {
            Window win = GetModalWindow(parentWin, AutomationId, winTitle);
            Button btnOKCloseWindow = GetControl<Button>(win, "Close", "Close");
            btnOKCloseWindow.Click();
        }

 

Hope this might be helpful to some of you.
Cheers,
Leo