Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Starting an InPrivate instance of Internet Explorer in CodedUI

5.00/5 (3 votes)
21 Mar 2015CPOL2 min read 20.9K  
How to start an InPrivate instance of Internet Explorer by using BrowserWindow object.

Introduction

During the preparation of one of my CodedUI tests I came in situation in which I needed to start Internet Explorer with InPrivate Browsing set to active. There is no such a property on BrowserWindow object that can be set, probably because this is a specific Internet Explorer option (although present as functionality with a different name on other browsers).

So how do we do it?

Background

A quick search on Google, surprisingly, didn't gave any results. So I dug into this small challenge. In order to start Internet Explorer with InPrivate Browsing set to active it is necessary to pass the -private argument to the call of the executable. So how to pass an argument to the BrowserWindow instance that we are creating?

Using the code

It's not obvious as not often used but, BrowserWindow.Launch static method has, aside the usual signature accepting an Uri object as a parameter, an overload that accepts a variable number of string  arguments (params string[]). The string arguments specified in the call of this method, will be passed as arguments to the process that is going to be started by invoking the BrowserWindow.Launch, in this case to the process of Internet Explorer. This is quite handy as we can pass our URL and and the necessary -private argument to Internet Explorer and achieve the desired result.

C#
BrowserWindow.Launch("http://www.google.com/", "-private");

How does it work?

If you peek inside the BrowserWindow class, you will see that underneath it is starting a process and it passes all of the parameters as an array of string to the Arguments property.

C#
Process process = new Process {StartInfo = {FileName = "iexplore.exe"}};
StringBuilder commandLine = new StringBuilder();
Array.ForEach<string>(
    arguments, 
    str => commandLine.AppendFormat(
        CultureInfo.InvariantCulture, 
        "{0} ", 
        new object[] { str }));
process.StartInfo.Arguments = commandLine.ToString();
process.Start();
The same can be achieved also by using the following approach:
C#
Process ieInPrivate = new Process();
ieInPrivate.StartInfo.FileName = "iexplore";
ieInPrivate.StartInfo.Arguments = "http://www.google.com/ -private";
ieInPrivate.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
ieInPrivate.Start();

BrowserWindow browser = BrowserWindow.FromProcess(ieInPrivate);

At the end the result will be a successfully launched Internet Explorer with InPrivate Browsing set to active.

Image 1

There are also some other handy option that you can pass to Internet Explorer like -extoff that will start Internet Explorer in No Add-ons mode. For a complete list of options check the following page on MSDN.

Points of Interest

By exploring this kind of tasks makes you peak inside the code of the objects you are using on daily basis. It will help you to understand how all of it works and put some valuable knowledge in your toolbox. It can be very helpful in the future.

I encourage you to check these libraries with dotPeek or a .NET Decompiler of your choice in order to understand better how they relate and achieve this functionality. Classes as BrowserWindow, IEBrowserService, InternetExplorerWrapper, ShDocVw and how the relate is quite important to fully understand CodedUI.

Happy coding!

History

Initial release.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)