示例#1
0
        public override void Execute(TestState ts)
        {
            AppHandle app = Application;

            app.Exit(false);
            Assertion.AssertNull("Non-null process", app.Process);
        }
示例#2
0
 public ApplicationContext()
 {
     m_tag          = "on-application";
     m_source       = null;
     m_app          = null;
     m_run          = null;
     m_title        = null;
     m_exeName      = null;
     m_GuiModel     = null;
     m_GuiModelPath = null;
     m_close        = false;
     m_args         = null;
     m_work         = null;
     m_gui          = null;
     m_model_root   = null;
     // get the model root if there is one
 }
示例#3
0
		public ApplicationContext()
		{
			m_tag      = "on-application";
			m_source   = null;
			m_app      = null;
			m_run      = null;
			m_title    = null;
			m_exeName  = null;
			m_GuiModel = null;
			m_GuiModelPath = null;
			m_close    = false;
			m_args     = null;
			m_work     = null;
			m_gui      = null;
			m_model_root = null;
			// get the model root if there is one
		}
示例#4
0
        public override void Execute(TestState ts)
        {
            bool   fStarted = true;
            string fullPath = m_path + @"\" + m_exeName;
            // if the app is already running, close it first
            AppHandle app = Application;

            if (app != null && app.ExePath == fullPath)
            {
                if (app.Process != null)
                {
                    app.Process.Kill();
                    app.Process.WaitForExit(3000);
                }
            }

            m_proc = Process.Start(fullPath);
            if (m_proc == null)
            {
                fStarted = false;
            }

            m_proc.WaitForInputIdle();
            while (Process.GetProcessById(m_proc.Id).MainWindowHandle == IntPtr.Zero)
            {
                Thread.Sleep(100);
            }
            if (m_proc.HasExited)
            {
                fStarted = false;
            }

            m_proc.WaitForInputIdle();
            Win32.SetForegroundWindow(m_proc.MainWindowHandle);

            Assertion.AssertEquals(true, fStarted);
            Assertion.AssertNotNull("Null process", m_proc);
            Assertion.AssertNotNull("Null window handle", m_proc.MainWindowHandle);
        }
示例#5
0
        /// <summary>
        /// Find the application main window accessibility object.
        /// </summary>
        /// <param name="maxWait">The maximum time to find it in milliseconds.</param>
        /// <returns>True if the app was found.</returns>
        private bool FindApp(int maxWait)
        {
            bool found = false;

            if (m_source != null)
            {
                // We had a ref to another Application Context element.
                if (m_source is ApplicationContext)
                {
                    m_app = ((ApplicationContext)m_source).Application;
                    found = m_app != null;
                    isNotNull(m_app, "Source for on-application is null");
                    m_ah = m_app.MainAccessibilityHelper;
                    isNotNull(m_app.Process, "Source for on-application lost its process");
                    m_app.Process.WaitForInputIdle();
                    Win32.SetForegroundWindow(m_app.Process.MainWindowHandle);
                }
            }
            if (!found && m_exeName != null)
            {
                int       pos    = m_exeName.IndexOf('.', m_exeName.Length - 5);
                string    procId = m_exeName.Substring(0, pos);
                Process[] proc   = Process.GetProcessesByName(procId);
                if (proc != null && proc.Length > 0)
                {
                    Logger.getOnly().paragraph(procId + ": has " + (proc.Length - 1) + " siblings.");
                    proc[0].WaitForInputIdle();                     // doesn't wait long enough
                    IntPtr hwnd = waitForWindow(maxWait, proc[0]);
                    if (0 == hwnd.ToInt32())
                    {
                        return(false);
                    }
                    m_title = proc[0].MainWindowTitle;
                    //AccessibilityHelper ah = new AccessibilityHelper(m_title);
                    AccessibilityHelper ah = new AccessibilityHelper(hwnd);
                    found = ah != null;
                    if (found)
                    {
                        m_app = new AppHandle(null, proc[0], ah);
                        found = m_app != null;
                        if (found)
                        {
                            m_ah = ah;

                            // make the found window show itself
                            proc[0].WaitForInputIdle();
                            string image;
                            if (m_ah != null)
                            {
                                AccessibleRole ar;
                                string         ars, an;
                                try { ar = m_ah.Role; ars = ar.ToString(); }
                                catch (Exception e) { ars = e.Message; }
                                try { an = m_ah.Name; }
                                catch (Exception e) { an = e.Message; }
                                image = @" ah=""" + ars + @":" + an + @"""";
                            }
                            else
                            {
                                image = @"ah=""null""";
                            }
                            Logger.getOnly().paragraph("about to set focus on " + image);
                            Win32.SetForegroundWindow(proc[0].MainWindowHandle);
                            Logger.getOnly().paragraph("Focus set on " + m_title);
                        }
                    }
                }
            }
            return(found);
        }
示例#6
0
        private bool LaunchApp()
        {
            // Launch the Application
            bool fStarted = true;

            isNotNull(m_path, "No application @path specified or in GtdConfig.xml");
            isNotNull(m_exeName, "No application executable name @exe specified or in GtdConfig.xml");
            string fullPath = m_path + @"\" + m_exeName;
            //Process proc = Process.Start(fullPath);
            // Need to set the working directory to m_path
            Process proc = new Process();

            // can proc ever be null?
            isNotNull(proc, "Could not create a new process for this application.");
            proc.StartInfo.FileName = fullPath;
            if (m_args != null)
            {
                proc.StartInfo.Arguments = m_args;
            }
            //proc.StartInfo.Arguments = "/r:System.dll /out:sample.exe stdstr.cs";
            proc.StartInfo.UseShellExecute = false;
            //compiler.StartInfo.RedirectStandardOutput = true;
            if (m_work != null)
            {
                proc.StartInfo.WorkingDirectory = m_work;
            }
            else
            {
                proc.StartInfo.WorkingDirectory = m_path;
            }
            proc.Start();

            if (proc == null)
            {
                fStarted = false;
            }
            // can this happen?
            isNotNull(proc, "Process became null when launched.");

            proc.WaitForInputIdle();
            isNotNull(proc.MainWindowHandle, "Window handle was not grasped");

            // proc.MainWindowHandle is always IntPtr.Zero
            // so, get another process that has proc.Id
            Process pOfId = Process.GetProcessById(proc.Id);

            isNotNull(pOfId, "Grabbed null process");
            isNotNull(pOfId.MainWindowHandle, "Grabbed process with no handle");
////			while (pOfId.MainWindowHandle == IntPtr.Zero)
////			{
////				Thread.Sleep(100);
////				pOfId = Process.GetProcessById(proc.Id);
////				isNotNull(pOfId,"Grabbed null process");
////				isNotNull(pOfId.MainWindowHandle,"Grabbed process with no handle");
////			}
            if (proc.HasExited)
            {
                fStarted = false;
            }

            // make the window show itself
            pOfId.WaitForInputIdle();
            isNotNull(pOfId.MainWindowTitle, "Window has no title");
            Win32.SetForegroundWindow(pOfId.MainWindowHandle);

            // get a new accessibility object as it has more nodes in it now.
            m_ah = new AccessibilityHelper(pOfId.MainWindowHandle);
            isNotNull(m_ah, "Can't access app with title" + pOfId.MainWindowTitle);
            m_app = new AppHandle(null, pOfId, m_ah);
            isNotNull(m_app, "Null application handle for window with title" + pOfId.MainWindowTitle);

            // Call StartUpContext.ExecuteOnDemand(ts) if there is one.
            // Otherwise, wait for and find the main app window.
            StartUpContext startUp = null;

            foreach (Instruction ins in m_components)
            {
                string stType = ins.GetType().ToString();
                if (stType == "GuiTestDriver.StartUpContext")
                {
                    startUp = (StartUpContext)ins;
                    break;
                }
            }
            if (startUp != null)
            {            // pass higher log levels to this child
                if (startUp.LogLevel < LogLevel)
                {
                    startUp.LogLevel = LogLevel;
                }
                startUp.ExecuteOnDemand();
            }

            // Now find the App in case we got the splash screen or something
            fStarted = FindApp(60000);             // look for upto a minute

            return(fStarted);
        }
示例#7
0
        /// <summary>
        /// Execute the context instructions
        /// </summary>
        public override void Execute()
        {
            Wait();

            m_Rest = 0;             // don't wait later when base.Execute is invoked
            string failed = null;

            m_path    = Utilities.evalExpr(m_path);
            m_exeName = Utilities.evalExpr(m_exeName);
            m_title   = Utilities.evalExpr(m_title);
            if (m_args != null)
            {
                m_args = Utilities.evalExpr(m_args);
            }
            if (m_work != null)
            {
                m_work = Utilities.evalExpr(m_work);
            }
            string fullPath = m_path + @"\" + m_exeName;

            // Read and load the top-level variables from the model
            // as child instructions of this context.
            // Some may replace previously defined vars.
            ReadModelVars();
            // make "shells" for the child instructions.
            // start-up is needed when the app is launched.
            base.PrepareChildren(true);             // add more children

            bool found = false;

            if ("top" == m_run)
            {             // get the top window as the application context
                m_ah = new AccessibilityHelper();
                isNotNull(m_ah, "Top window is not accessible");
                Process proc = Process.GetCurrentProcess();
                isNotNull(proc, "Top window is not the current process");
                m_title = proc.MainWindowTitle;
                m_app   = new AppHandle(null, proc, m_ah);
                isNotNull(m_app, "Lost top window application handle for window with title" + m_title);
                found = true;
            }
            // Is the app running already?
            if (found == false)
            {
                found = FindApp(1000);
            }
            string contextPass, contextFail;

            PassFailInContext(OnPass, OnFail, out contextPass, out contextFail);                // out m_onPass, out m_onFail);
            if ("no" == m_run && !found)
            {
                if (m_title != null)
                {
                    failed = "Application '" + m_title + "' not found.";
                }
                else if (m_exeName != null)
                {
                    failed = "Application '" + m_exeName + "' not found.";
                }
                else
                {
                    failed = "Application not found.";
                }
            }
            if ("yes" == m_run && found)
            {
                m_app.Exit(false);                 // close all app windows
                m_app = null;
                found = false;
            }
            if (!found && failed == null && "no" != m_run)
            {
                if (!LaunchApp())
                {
                    failed = "Application '" + fullPath + "' not launched.";
                }
            }

            if (contextFail == "assert" && failed != null && failed != "")
            {
                fail(failed);
            }
            if (contextPass == "assert" && failed == null)
            {
                fail("run='" + m_run + "' exe='" + m_exeName + "' passed");
            }
            if (failed != null && failed != "")
            {             // OK to fail, but should note it
                m_log.paragraph(failed);
            }
            else
            {
                base.Execute();
                if (m_close)
                {
                    m_app.Exit(false);                          // close all app windows
                }
                m_finished = true;
                resultImage();
            }
            // return focus back to the previous application if there was one
            AppHandle aph = base.Application;

            if (aph != null)
            {
                isNotNull(aph.Process, "Parent application lost its process");
                aph.Process.WaitForInputIdle();
                Win32.SetForegroundWindow(aph.Process.MainWindowHandle);
            }
        }
示例#8
0
        /// <summary>
        /// Find the application main window accessibility object.
        /// </summary>
        /// <param name="maxWait">The maximum time to find it in milliseconds.</param>
        /// <returns>True if the app was found.</returns>
        private bool FindApp(int maxWait)
        {
            m_log.paragraph("Trying to find the application.");
            bool found = false;

            if (m_source != null)
            {
                // We had a ref to another Application Context element.
                if (m_source is OnApplication)
                {
                    m_app = ((OnApplication)m_source).Application;
                    found = m_app != null;
                    m_log.isNotNull(m_app, "Source for on-application is null");
                    m_ah = m_app.MainAccessibilityHelper;
                    m_log.isNotNull(m_app.Process, "Source for on-application lost its process");
                    m_app.Process.WaitForInputIdle();
                    SetForegroundWindow(m_app.Process.MainWindowHandle);
                    m_log.paragraph("Found app via @source");
                }
            }
            if (!found)
            {               // m_exeName is set via finishCreation()->Configure() from model data
                if (m_exeName != null)
                {
                    int       pos    = m_exeName.IndexOf('.', m_exeName.Length - 5);
                    string    procId = m_exeName.Substring(0, pos);
                    Process[] proc   = Process.GetProcessesByName(procId);
                    if (proc != null && proc.Length > 0)
                    {                       // don't use m_log because that instance may have exited with the previous app
                        m_log = Logger.getOnly();
                        m_log.paragraph(procId + ": has " + (proc.Length - 1) + " siblings.");
                        proc[0].WaitForInputIdle();                         // doesn't wait long enough
                        IntPtr hwnd = waitForWindow(maxWait, proc[0]);
                        if (0 == hwnd.ToInt32())
                        {
                            return(false);
                        }
                        m_title = proc[0].MainWindowTitle;
                        //AccessibilityHelper ah = new AccessibilityHelper(m_title);
                        AccessibilityHelper ah = new AccessibilityHelper(hwnd);
                        found = ah != null;
                        if (found)
                        {
                            m_app = new AppHandle(null, proc[0], ah);
                            found = m_app != null;
                            if (found)
                            {
                                m_ah = ah;

                                // make the found window show itself
                                proc[0].WaitForInputIdle();
                                string image;
                                if (m_ah != null)
                                {
                                    AccessibleRole ar;
                                    string         ars, an;
                                    try { ar = m_ah.Role; ars = ar.ToString(); }
                                    catch (Exception e) { ars = e.Message; }
                                    try { an = m_ah.Name; }
                                    catch (Exception e) { an = e.Message; }
                                    image = @" ah=""" + ars + @":" + an + @"""";
                                }
                                else
                                {
                                    image = @"ah=""null""";
                                }
                                m_log.paragraph("about to set focus on " + image);
                                SetForegroundWindow(proc[0].MainWindowHandle);
                                m_log.paragraph("Focus set on " + m_title);
                            }
                        }
                    }
                }
                else                 // no way to know what to look for
                {
                    m_log.paragraph("Can't find the application without an @exe name.");
                }
            }
            return(found);
        }
示例#9
0
        private bool LaunchApp()
        {
            // Launch the Application
            m_log.paragraph("Trying to launch the application.");
            bool fStarted = true;

            m_log.isNotNull(m_path, "No application @path specified or in GtdConfig.xml");
            m_log.isNotNull(m_exeName, "No application executable name @exe specified or in GtdConfig.xml");
            string fullPath = m_path + @"\" + m_exeName;

            //Process proc = Process.Start(fullPath);
            // Need to set the working directory to m_path
            m_log.paragraph("Launching " + fullPath + " as an Application");
            Process proc = new Process();

            // can proc ever be null?
            m_log.isNotNull(proc, "Could not create a new process for this application.");
            proc.StartInfo.FileName = fullPath;
            if (m_args != null)
            {
                proc.StartInfo.Arguments = m_args;
            }
            //proc.StartInfo.Arguments = "/r:System.dll /out:sample.exe stdstr.cs";
            proc.StartInfo.UseShellExecute = false;
            //compiler.StartInfo.RedirectStandardOutput = true;
            if (m_work != null)
            {
                proc.StartInfo.WorkingDirectory = m_work;
            }
            else
            {
                proc.StartInfo.WorkingDirectory = m_path;
            }
            m_log.paragraph("Starting " + fullPath);

            proc.Start();

            Thread.Sleep(2000);             // let it start or fail
            if (proc == null)
            {
                fStarted = false;
            }
            // can this happen?
            m_log.isNotNull(proc, "Process became null when launched.");
            proc.Refresh();                          // update the state
            IntPtr wnd = waitForWindow(60000, proc); // look for upto a minute

            //if (proc.WaitForInputIdle()) // true iff proc has a window (*.bat don't)
            //m_log.isNotNull(proc.MainWindowHandle, "Window handle was not grasped");

            // proc.MainWindowHandle can be IntPtr.Zero until the window or splash forms
            // so, get another process that has proc.Id
            //Process pOfId = Process.GetProcessById(proc.Id); // get same proc??
            //while (pOfId.MainWindowHandle == IntPtr.Zero)
            //{
            //	Thread.Sleep(100);
            //	pOfId.Refresh(); // update the state
            //	pOfId = Process.GetProcessById(proc.Id);
            //	isNotNull(pOfId,"Grabbed null process");
            //	isNotNull(pOfId.MainWindowHandle,"Grabbed process with no handle");
            //}
            if (proc.HasExited)
            {
                fStarted = false;
            }

            // make the window show itself
            proc.WaitForInputIdle();
            m_log.isNotNull(proc.MainWindowTitle, "Window has no title");
            SetForegroundWindow(proc.MainWindowHandle);

            // get a new accessibility object as it has more nodes in it now.
            m_ah = new AccessibilityHelper(proc.MainWindowHandle);
            m_log.isNotNull(m_ah, "Can't access app with title" + proc.MainWindowTitle);
            m_app = new AppHandle(null, proc, m_ah);
            m_log.isNotNull(m_app, "Null application handle for window with title" + proc.MainWindowTitle);

            // Call OnStartup.ExecuteOnDemand(ts) if there is one.
            // Otherwise, wait for and find the main app window.
            OnStartup startUp = null;

            foreach (Instruction ins in m_instructions)
            {
                string stType = ins.GetType().ToString();
                if (stType == "GuiTestDriver.OnStartup")
                {
                    startUp = (OnStartup)ins;
                    break;
                }
            }
            if (startUp != null)
            {            // pass higher log levels to this child
                if (startUp.Log < Log)
                {
                    startUp.Log = Log;
                }
                startUp.ExecuteOnDemand();
            }

            // Now find the App in case we got the splash screen or something
            //fStarted = FindApp(60000); // look for upto a minute

            return(fStarted);
        }
示例#10
0
        private bool LaunchApp()
        {
            // Launch the Application
            m_log.paragraph("Trying to launch the application.");
            bool fStarted = true;
            m_log.isNotNull(m_path, "No application @path specified or in GtdConfig.xml");
            m_log.isNotNull(m_exeName, "No application executable name @exe specified or in GtdConfig.xml");
            string fullPath = m_path + @"\" + m_exeName;
            //Process proc = Process.Start(fullPath);
            // Need to set the working directory to m_path
            m_log.paragraph("Launching " + fullPath + " as an Application");
            Process proc = new Process();
            // can proc ever be null?
            m_log.isNotNull(proc, "Could not create a new process for this application.");
            proc.StartInfo.FileName = fullPath;
            if (m_args != null) proc.StartInfo.Arguments = m_args;
            //proc.StartInfo.Arguments = "/r:System.dll /out:sample.exe stdstr.cs";
            proc.StartInfo.UseShellExecute = false;
            //compiler.StartInfo.RedirectStandardOutput = true;
            if (m_work != null) proc.StartInfo.WorkingDirectory = m_work;
            else                proc.StartInfo.WorkingDirectory = m_path;
            m_log.paragraph("Starting " + fullPath);

            proc.Start();

            Thread.Sleep(2000); // let it start or fail
            if (proc == null) fStarted = false;
            // can this happen?
            m_log.isNotNull(proc, "Process became null when launched.");
            proc.Refresh(); // update the state
            IntPtr wnd = waitForWindow(60000, proc); // look for upto a minute

            //if (proc.WaitForInputIdle()) // true iff proc has a window (*.bat don't)
                //m_log.isNotNull(proc.MainWindowHandle, "Window handle was not grasped");

            // proc.MainWindowHandle can be IntPtr.Zero until the window or splash forms
            // so, get another process that has proc.Id
            //Process pOfId = Process.GetProcessById(proc.Id); // get same proc??
            //while (pOfId.MainWindowHandle == IntPtr.Zero)
            //{
            //	Thread.Sleep(100);
            //	pOfId.Refresh(); // update the state
            //	pOfId = Process.GetProcessById(proc.Id);
            //	isNotNull(pOfId,"Grabbed null process");
            //	isNotNull(pOfId.MainWindowHandle,"Grabbed process with no handle");
            //}
            if (proc.HasExited) fStarted = false;

            // make the window show itself
            proc.WaitForInputIdle();
            m_log.isNotNull(proc.MainWindowTitle, "Window has no title");
            SetForegroundWindow(proc.MainWindowHandle);

            // get a new accessibility object as it has more nodes in it now.
            m_ah = new AccessibilityHelper(proc.MainWindowHandle);
            m_log.isNotNull(m_ah, "Can't access app with title" + proc.MainWindowTitle);
            m_app = new AppHandle(null, proc, m_ah);
            m_log.isNotNull(m_app, "Null application handle for window with title" + proc.MainWindowTitle);

            // Call OnStartup.ExecuteOnDemand(ts) if there is one.
            // Otherwise, wait for and find the main app window.
            OnStartup startUp = null;
            foreach ( Instruction ins in m_instructions)
            {
                string stType = ins.GetType().ToString();
                if (stType == "GuiTestDriver.OnStartup")
                {
                    startUp = (OnStartup)ins;
                    break;
                }
            }
            if (startUp != null)
            {// pass higher log levels to this child
                if (startUp.Log < Log)
                    startUp.Log = Log;
                startUp.ExecuteOnDemand();
            }

            // Now find the App in case we got the splash screen or something
            //fStarted = FindApp(60000); // look for upto a minute

            return fStarted;
        }
示例#11
0
        /// <summary>
        /// Find the application main window accessibility object.
        /// </summary>
        /// <param name="maxWait">The maximum time to find it in milliseconds.</param>
        /// <returns>True if the app was found.</returns>
        private bool FindApp(int maxWait)
        {
            m_log.paragraph("Trying to find the application.");
            bool found = false;
            if (m_source != null)
            {
                // We had a ref to another Application Context element.
                if (m_source is OnApplication)
                {
                    m_app = ((OnApplication)m_source).Application;
                    found = m_app != null;
                    m_log.isNotNull(m_app, "Source for on-application is null");
                    m_ah = m_app.MainAccessibilityHelper;
                    m_log.isNotNull(m_app.Process, "Source for on-application lost its process");
                    m_app.Process.WaitForInputIdle();
                    SetForegroundWindow(m_app.Process.MainWindowHandle);
                    m_log.paragraph("Found app via @source");
                }
            }
            if (!found)
            {   // m_exeName is set via finishCreation()->Configure() from model data
                if (m_exeName != null)
                {
                    int pos = m_exeName.IndexOf('.', m_exeName.Length - 5);
                    string procId = m_exeName.Substring(0, pos);
                    Process[] proc = Process.GetProcessesByName(procId);
                    if (proc != null && proc.Length > 0)
                    {   // don't use m_log because that instance may have exited with the previous app
                        m_log = Logger.getOnly();
                        m_log.paragraph(procId + ": has " + (proc.Length - 1) + " siblings.");
                        proc[0].WaitForInputIdle(); // doesn't wait long enough
                        IntPtr hwnd = waitForWindow(maxWait, proc[0]);
                        if (0 == hwnd.ToInt32()) return false;
                        m_title = proc[0].MainWindowTitle;
                        //AccessibilityHelper ah = new AccessibilityHelper(m_title);
                        AccessibilityHelper ah = new AccessibilityHelper(hwnd);
                        found = ah != null;
                        if (found)
                        {
                            m_app = new AppHandle(null, proc[0], ah);
                            found = m_app != null;
                            if (found)
                            {
                                m_ah = ah;

                                // make the found window show itself
                                proc[0].WaitForInputIdle();
                                string image;
                                if (m_ah != null)
                                {
                                    AccessibleRole ar;
                                    string ars, an;
                                    try { ar = m_ah.Role; ars = ar.ToString(); }
                                    catch (Exception e) { ars = e.Message; }
                                    try { an = m_ah.Name; }
                                    catch (Exception e) { an = e.Message; }
                                    image = @" ah=""" + ars + @":" + an + @"""";
                                }
                                else image = @"ah=""null""";
                                m_log.paragraph("about to set focus on " + image);
                                SetForegroundWindow(proc[0].MainWindowHandle);
                                m_log.paragraph("Focus set on " + m_title);
                            }
                        }
                    }
                }
                else // no way to know what to look for
                {
                    m_log.paragraph("Can't find the application without an @exe name.");
                }
            }
            return found;
        }
示例#12
0
        /// <summary>
        /// Execute the context instructions
        /// </summary>
        public override void Execute()
        {
            WaitMsec();

            m_Rest = 0; // don't wait later when base.Execute is invoked
            string failed = null;
            m_path    = Utilities.evalExpr(m_path);
            m_exeName = Utilities.evalExpr(m_exeName);
            m_title   = Utilities.evalExpr(m_title);
            if (m_args != null) m_args = Utilities.evalExpr(m_args);
            if (m_work != null) m_work = Utilities.evalExpr(m_work);
            string fullPath = m_path + @"\" + m_exeName;
            bool batch = false; // true if launched a batch file

            // Read and load the top-level variables from the model
            // as child instructions of this context.
            // Some may replace previously defined vars.
            ReadModelVars();
            // make "shells" for the child instructions.
            // start-up is needed when the app is launched.
            base.PrepareChildren(true); // add more children

            bool found = false;
            if ("top" == m_run)
            { // get the top window as the application context
                m_ah = new AccessibilityHelper();
                m_log.isNotNull(m_ah, "Top window is not accessible");
                Process proc = Process.GetCurrentProcess();
                m_log.isNotNull(proc, "Top window is not the current process");
                m_title = proc.MainWindowTitle;
                m_app = new AppHandle(null,proc,m_ah);
                m_log.isNotNull(m_app, "Lost top window application handle for window with title" + m_title);
                found = true;
            }
            // Is the app running already?
            if (found == false) found = FindApp(1000);
            string contextPass, contextFail;
            PassFailInContext(OnPass, OnFail, out contextPass, out contextFail);	// out m_onPass, out m_onFail);
            if ("no" == m_run && !found)
            {
                if (m_title != null)        failed = "Application '"+ m_title +"' not found.";
                else if (m_exeName != null) failed = "Application '"+ m_exeName +"' not found.";
                else                        failed = "Application not found.";
            }
            if ("yes" == m_run && found)
            {
                m_app.Exit(false); // close all app windows
                m_app = null;
                found = false;
            }
            if (!found && failed == null && "no" != m_run)
            {
                if (m_exeName != null && m_exeName != "")
                {
                    string [] parts = m_exeName.Split('.');
                    if (parts[parts.GetLength(0) - 1].ToLower().Equals("bat"))
                        batch = LaunchBat();
                }
                if (!batch && !LaunchApp())
                    failed = "Application '"+ fullPath +"' not launched.";
            }

            if (contextFail == "assert" && failed != null && failed != "") m_log.fail(makeNameTag() + failed);
            if (contextPass == "assert" && failed == null) m_log.fail(makeNameTag() + "run='" + m_run + "' exe='" + m_exeName + "' passed");
            if (failed != null && failed != "")
            { // OK to fail, but should note it
                m_log.paragraph(failed);
            }
            else if (!batch)
            {
                base.Execute();
                if (m_close) m_app.Exit(false); // close all app windows
                m_finished = true;
                resultImage();
            }
            // return focus back to the previous application if there was one
            AppHandle aph = base.Application;
            if (aph != null)
            {
                m_log.isNotNull(aph.Process, "Parent application lost its process");
                aph.Process.WaitForInputIdle();
                SetForegroundWindow(aph.Process.MainWindowHandle);
            }
        }
示例#13
0
		/// <summary>
		/// Find the application main window accessibility object.
		/// </summary>
		/// <param name="maxWait">The maximum time to find it in milliseconds.</param>
		/// <returns>True if the app was found.</returns>
		private bool FindApp (int maxWait)
		{
			bool found = false;
			if (m_source != null)
			{
				// We had a ref to another Application Context element.
				if (m_source is ApplicationContext)
				{
					m_app = ((ApplicationContext)m_source).Application;
					found = m_app != null;
					isNotNull(m_app,"Source for on-application is null");
					m_ah = m_app.MainAccessibilityHelper;
					isNotNull(m_app.Process,"Source for on-application lost its process");
					m_app.Process.WaitForInputIdle();
					Win32.SetForegroundWindow(m_app.Process.MainWindowHandle);
				}
			}
			if (!found && m_exeName != null)
			{
				int pos = m_exeName.IndexOf('.',m_exeName.Length-5);
				string procId = m_exeName.Substring(0,pos);
				Process[] proc = Process.GetProcessesByName(procId);
				if (proc != null && proc.Length > 0)
				{
					Logger.getOnly().paragraph(procId+": has "+(proc.Length-1)+" siblings.");
					proc[0].WaitForInputIdle(); // doesn't wait long enough
					IntPtr hwnd = waitForWindow(maxWait, proc[0]);
					if (0 == hwnd.ToInt32()) return false;
					m_title = proc[0].MainWindowTitle;
					//AccessibilityHelper ah = new AccessibilityHelper(m_title);
					AccessibilityHelper ah = new AccessibilityHelper(hwnd);
					found = ah != null;
					if(found)
					{
						m_app = new AppHandle(null,proc[0],ah);
						found = m_app != null;
						if (found)
						{
							m_ah = ah;

							// make the found window show itself
							proc[0].WaitForInputIdle();
							string image;
							if (m_ah != null)
							{
								AccessibleRole ar;
								string ars, an;
								try {ar = m_ah.Role; ars = ar.ToString();}
								catch(Exception e){ars = e.Message;}
								try {an = m_ah.Name;}
								catch(Exception e){an = e.Message;}
								image = @" ah="""+ars+@":"+an+@"""";
							}
							else image = @"ah=""null""";
							Logger.getOnly().paragraph("about to set focus on "+image);
							Win32.SetForegroundWindow(proc[0].MainWindowHandle);
							Logger.getOnly().paragraph("Focus set on "+m_title);
						}
					}
				}
			}
			return found;
		}
示例#14
0
		private bool LaunchApp ()
		{
			// Launch the Application
			bool fStarted = true;
			isNotNull(m_path,"No application @path specified or in GtdConfig.xml");
			isNotNull(m_exeName,"No application executable name @exe specified or in GtdConfig.xml");
			string fullPath = m_path + @"\" + m_exeName;
			//Process proc = Process.Start(fullPath);
			// Need to set the working directory to m_path
			Process proc = new Process();
			// can proc ever be null?
			isNotNull(proc, "Could not create a new process for this application.");
			proc.StartInfo.FileName = fullPath;
			if (m_args != null) proc.StartInfo.Arguments = m_args;
			//proc.StartInfo.Arguments = "/r:System.dll /out:sample.exe stdstr.cs";
			proc.StartInfo.UseShellExecute = false;
			//compiler.StartInfo.RedirectStandardOutput = true;
			if (m_work != null) proc.StartInfo.WorkingDirectory = m_work;
			else                proc.StartInfo.WorkingDirectory = m_path;
			proc.Start();

			if (proc == null) fStarted = false;
			// can this happen?
			isNotNull(proc, "Process became null when launched.");

			proc.WaitForInputIdle();
			isNotNull(proc.MainWindowHandle,"Window handle was not grasped");

			// proc.MainWindowHandle is always IntPtr.Zero
			// so, get another process that has proc.Id
			Process pOfId = Process.GetProcessById(proc.Id);
			isNotNull(pOfId,"Grabbed null process");
			isNotNull(pOfId.MainWindowHandle,"Grabbed process with no handle");
////			while (pOfId.MainWindowHandle == IntPtr.Zero)
////			{
////				Thread.Sleep(100);
////				pOfId = Process.GetProcessById(proc.Id);
////				isNotNull(pOfId,"Grabbed null process");
////				isNotNull(pOfId.MainWindowHandle,"Grabbed process with no handle");
////			}
			if (proc.HasExited) fStarted = false;

			// make the window show itself
			pOfId.WaitForInputIdle();
			isNotNull(pOfId.MainWindowTitle,"Window has no title");
			Win32.SetForegroundWindow(pOfId.MainWindowHandle);

			// get a new accessibility object as it has more nodes in it now.
			m_ah = new AccessibilityHelper(pOfId.MainWindowHandle);
			isNotNull(m_ah,"Can't access app with title"+pOfId.MainWindowTitle);
			m_app = new AppHandle(null,pOfId,m_ah);
			isNotNull(m_app,"Null application handle for window with title"+pOfId.MainWindowTitle);

			// Call StartUpContext.ExecuteOnDemand(ts) if there is one.
			// Otherwise, wait for and find the main app window.
			StartUpContext startUp = null;
			foreach ( Instruction ins in m_components)
			{
				string stType = ins.GetType().ToString();
				if (stType == "GuiTestDriver.StartUpContext")
				{
					startUp = (StartUpContext)ins;
					break;
				}
			}
			if (startUp != null)
			{// pass higher log levels to this child
				if (startUp.LogLevel < LogLevel)
					startUp.LogLevel = LogLevel;
				startUp.ExecuteOnDemand();
			}

			// Now find the App in case we got the splash screen or something
			fStarted = FindApp(60000); // look for upto a minute

			return fStarted;
		}