2

I'm creating multiple UI tests and instead of opening a new instance of Microsoft.VisualStudio.TestTools.UITesting.BrowserWindow I'd like to check to see if a BrowserWindow object is already available - meaning IE is already open on the host machine - and get a handle to it. I've found this question but I want my BrowserWindow wrapper class to get the handle and playback has already been initialized at the point of class initialization so the suggested method won't work.

This is my existing code...

public class uiBrowserWindow : BrowserWindow {
    public static void launchUrl(string url) {
        Launch(url);
    }
}

EDIT I've got it working, it's not ideal but it works. Is there a better way?

public class uiBrowserWindow : BrowserWindow {
    public void launchUrl(string url) {
        try {
            SearchProperties[PropertyNames.ClassName] = "IEFrame";
            NavigateToUrl(new Uri(url));
        } catch (Exception) {
            Launch(url);
    }
}
Community
  • 1
  • 1
bflemi3
  • 6,311
  • 19
  • 75
  • 144

2 Answers2

0

I'm not sure about the BrowserWindow object, but if you want to find out if a specific application is running or get a handle on a specific window, you can use the WIN32 API.

/* Create a Win32.Win32Api Helper class, or include in your class  */
[DllImport("User32.dll")]
public static extern Int32 FindWindow(String lpClassName, String lpWindowName);
[DllImport("User32.dll")]
public static extern Int32 SetForegroundWindow(int hWnd);
[DllImport("User32.dll")]
public static extern Int32 SendMessage(int hWnd, UInt32 msg, int wParam, int lParam);

Here's a sample of fetching a specific process and sending it a message through the API:

int ieWindow = Win32.Win32API.FindWindow(null, "C:\\Program Files (x86)\\Internet Explorer\\iexplore.exe");
Win32.Win32API.SetForegroundWindow(ieWindow);

// Example sending message, Right Mouse Button Up message (0x0205)
Win32.Win32API.SendMessage(ieWindow, 0x0205, 0, 0);

I hope this helps.

Chris

Chris B.
  • 696
  • 5
  • 3
0

An exception is thrown if a BrowserWindow instance has not been launched when the NavigateToUrl() method is called. I decided to catch the exception and launch the url otherwise once BrowserWindow is launched just call NavigateToUrl(). This works but takes too long on the first test method when catching the exception. I'll reassign the answer if a better solution is offered...

public class uiBrowserWindow : BrowserWindow {
    public void launchUrl(string url) {
        try {
            SearchProperties[PropertyNames.ClassName] = "IEFrame";
            NavigateToUrl(new Uri(url));
        } catch (Exception) {
            Launch(url);
    }
}
bflemi3
  • 6,311
  • 19
  • 75
  • 144