Exception: Unable to find the main window of the underlying process from CUITe_BrowserWindow.FromProcess(process);

Jun 19, 2013 at 2:49 PM
Hey,

first of all thx for creating CUITe - so far it worked like a charm! Anyhow just recently I stumbled accross a weird issue, where I don't really know how to debug/procced.

Within my testing I need to have the browser process running as different users. I use the code below to do it. The user who is starting the test has administrator priviledges (not even sure, if this would be required).

Anyhow - the code below works for some users, and for others it doesn't. When it's not working, I receive a "Unable to find the main window of the underlying process." Exception, although I observe the browser window was opening up and is actually in front of my screen.

When this happens, the method CUITe_BrowserWindow.FromProcess takes ages before it actually throws that exception.

Before I start digging/debugging that method, I would ask for any hints, what might go wrong.

I didn't find any specials/connections for user that works and others that doesn't. It also seems not "consistent", so a user where I got this exception once, could work in the next test run...

So it looks a little mystical to me ...

Any thoughts/suggestions would be great?!

Kind regards

Michael
   System.Diagnostics.ProcessStartInfo myPSI = new System.Diagnostics.ProcessStartInfo();
   myPSI.Password = tmpPass;
   myPSI.UserName = _User;
   myPSI.Domain = _Domain;
   myPSI.FileName = @"C:\Program Files (x86)\Internet Explorer\iexplore.exe";
   myPSI.Arguments = this.web_config.Home();
   myPSI.UseShellExecute = false;
   myPSI.WorkingDirectory = @"C:\";
   myPSI.LoadUserProfile = true;
   myPSI.WindowStyle = ProcessWindowStyle.Maximized;

   // Launch the Browser - it is yet an application
   CUITe_BrowserWindow.Launch(myPSI);                

   // Impersonate via process table into BrowserWindow
   Process[] processes = Process.GetProcessesByName("iexplore");
   foreach (Process process in processes) {
       if (string.IsNullOrEmpty(process.MainWindowTitle)) {
           continue;
       }

       win = CUITe_BrowserWindow.FromProcess(process);
       break;                              
    }

    if (win == null) {
        Assert.Fail("Method OpenBrowser failed: could not take over browser");
    }
Coordinator
Jun 26, 2013 at 12:34 AM
Hi Michael,

Sorry, I have not tested this functionality before.

What is win declared as?

Thank you.
Jun 28, 2013 at 7:12 PM
Hi icnocop,

win is of type BrowserWindow; anyhow the exception comes within the CUITE_BrowserWindow.FromProcess(process) function. So I am not sure, why the type would make any difference. Actually it could be even a var... ;)

Any further hints/ideas?

Kind regards
Michael
Coordinator
Jul 16, 2013 at 7:20 AM
Hi Michael,

CUITe_BrowserWindow inherits from BrowserWindow, so I'm thinking calling BrowserWindow.FromProcess(process); directly will produce the same results.

If so, then it doesn't seem like a CUITe specific issue and you may be better off asking your question in the appropriate msdn forum here.

Thank you.
Jul 20, 2013 at 10:09 AM
Hi icnocop,

I have to admit, that this error just existed over two days on a remote development machine.
I'm still confused about it, as it since that never happened again. No clue what was going on with this machine...

Anyhow, another issue I stumbled across and might be valuable for other users how to solve:
When using the approach above you can't execute JavaScript using the RunScript method, as the CUITe code below fails with a null-pointer exception. (ShellWindows just grabs IE instances for the current user, which is not the IE instance)

You can circumvent this for IE by injecting javascript and using the following method:

win.NavigateToUrl("javascript:alert('hello world');");
 public static void RunScript(BrowserWindow browserWindow, string code)
        {
            SHDocVw.InternetExplorer internetExplorer = null;
            ShellWindows shellWindows = new ShellWindows();
            foreach (SHDocVw.InternetExplorer shellWindow in shellWindows)
            {
                if (shellWindow.HWND == browserWindow.WindowHandle.ToInt32())
                {
                    internetExplorer = shellWindow;
                    break;
                }
            }
            internetExplorer.Document.parentWindow.execScript(code);
        }