Exemplo n.º 1
0
        public Window LaunchIE(string args)
        {
            // when IE launches it creates a new process instead of the process we create, so we have to track that jump.
            // so this means watching for a new process to appear.
            HashSet <int> runningProcesses = new HashSet <int>();

            foreach (Process e in Process.GetProcesses())
            {
                Debug.WriteLine("Found Process " + e.Id + " : " + e.ProcessName);
                if (e.ProcessName == "iexplore")
                {
                    try
                    {
                        e.Kill();
                    }
                    catch { }
                }
                else
                {
                    runningProcesses.Add(e.Id);
                }
            }

            ProcessStartInfo info = new ProcessStartInfo();

            info.FileName  = Environment.GetEnvironmentVariable("ProgramFiles") + "\\Internet Explorer\\iexplore.exe";;
            info.Arguments = args;

            Process p = new Process();

            p.StartInfo = info;
            if (!p.Start())
            {
                string msg = "Error launching " + info.FileName;
                MessageBox.Show("Error Creating Process", msg,
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw new Exception(msg);
            }

            // find the new process that has a window whose ClassName is "IEFrame".
            Process ie    = null;
            int     retry = 5;

            while (retry-- > 0)
            {
                foreach (Process np in Process.GetProcesses())
                {
                    if (!runningProcesses.Contains(np.Id))
                    {
                        Debug.WriteLine("Checking Process " + np.Id + " : " + np.ProcessName);
                        AutomationWrapper wrapper = Window.FindWindowForProcessId(np.Id, "IEFrame", null);
                        if (wrapper != null)
                        {
                            // found it!
                            ie = np;
                            p  = np;
                            break;
                        }
                    }
                }
                if (ie != null)
                {
                    break;
                }
                Sleep(500);
            }

            if (ie != null)
            {
                Window w = new Window(p, "IEFrame", null);
                w.TestBase = this;
                return(w);
            }
            throw new Exception("Not finding the new IE process, perhaps you need to shutdown existing IE instances");
        }