Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / C#
Tip/Trick

Starting an InPrivate instance of Internet Explorer in CodedUI

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
21 Mar 2015CPOL2 min read 20.3K   5   2
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)


Written By
Software Developer (Senior)
Netherlands Netherlands
An accomplished software engineer specialized in object-oriented design and analysis on Microsoft .NET platform with extensive experience in the full life cycle of the software design process.
Experienced in agile software development via scrum and kanban frameworks supported by the TFS ALM environment and JIRA. In depth know how on all automation process leading to continuous integration, deployment and feedback.
Additionally, I have a strong hands-on experience on deploying and administering Microsoft Team Foundation Server (migrations, builds, deployment, branching strategies, etc.).

Comments and Discussions

 
QuestionWhere is browserwindow namespace? Pin
MrDeej31-Mar-15 1:55
MrDeej31-Mar-15 1:55 
I cannot find it. This is my one big problem with using the internet, there are never any reference to the namespace in any articles. Always a puzzle to find out
from Oslo

AnswerRe: Where is browserwindow namespace? Pin
Mario Majčica31-Mar-15 3:21
professionalMario Majčica31-Mar-15 3:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.