Watin Internet Explorer how to start in-private mode?

2k views Asked by At

I am writing a few automated web tests using C# Watin framework.

I was hoping anyone can help me out,

How do i open new IE instance in "in private" browse mode? (i.e incognito mode)

The need for "in private" browsing, is because some of the tests, require log in. (and i run a few in parallel)

I could not find any resources on the matter. (other than half patch i found in some forum)

Thanks for your help!

3

There are 3 answers

0
yaron On BEST ANSWER

The only solution i was able to find, is to open IE instance via command in incognito mode, then attach Watin to it.

    //gen random url so we can find the window later
    Random rnd = new Random();
    int id = rnd.Next(1000, 10000); 
    string url = "id" + id+".com";
    //opening explorer
    Process.Start("IExplore.exe", "-private -nomerge " + url);
    browser = Browser.AttachTo<IE>(Find.ByUrl(new Regex(url)));
    browser.GoTo("http://www.google.com");
0
Nail On

I downloaded WatiN source codes, opened IE.cs and edited the method CreateIExploreInNewProcess

    private static Process CreateIExploreInNewProcess()
    {
        var arguments = "about:blank";

        if (GetMajorIEVersion() >= 8 && Settings.MakeNewIe8InstanceNoMerge)
            arguments = "-nomerge " + arguments;

        if (Settings.OpenInIncognitoMode == true)
        {
            arguments = "-private " + arguments;
        }

        var m_Proc = Process.Start("IExplore.exe", arguments);
        if (m_Proc == null) throw new WatiNException("Could not start IExplore.exe process");

        return m_Proc;
    }

Then, added this to Settings.cs:

    /// <summary>
    /// Gets or sets a value indicating whether the browser will be opened in incognito mode.
    /// </summary>
    /// <value>
    ///     <c>true</c> if IE instance needs to be opened in incognito mode, otherwise <c>false</c>.
    /// </value>
    public static bool OpenInIncognitoMode
    {
        get { return Instance.OpenInIncognitoMode; }
        set { Instance.OpenInIncognitoMode = value; }
    }

After that, added this to ISettings.cs

    /// <summary>
    /// Gets or sets a value indicating whether the browser will be opened in incognito mode.
    /// </summary>
    /// <value>
    ///     <c>true</c> if IE instance needs to be opened in incognito mode, otherwise <c>false</c>.
    /// </value>
    bool OpenInIncognitoMode { get; set; }

Finally, edited DefaultSettings.cs like this:

    private struct settingsStruct
    {
        ...
        ...
        public bool makeNewIe8InstanceNoMerge;
        public bool closeExistingFireFoxInstances;
        public bool incognitoMode;
    }

    public bool OpenInIncognitoMode
    {
        get { return settings.incognitoMode; }
        set { settings.incognitoMode = value; }
    }

    private void SetDefaults()
    {
        settings = new settingsStruct
        {
            ...
            ...
            makeNewIe8InstanceNoMerge = true,
            closeExistingFireFoxInstances = true,
            incognitoMode = false
        };
    }

Compile it and add the new DLL to your project. After this, all you need to do in your project is this:

Settings.OpenInIncognitoMode = true;
var browser = new IE(url, true);
0
Bhaskar Jain On

The trick is to pass the argument -private to the call of IExplore.exe like here:

string argument = "-private -nomerge about:blank";
process = Process.Start("IExplore.exe", argument);
if (process == null)
    throw new InvalidOperationException("The IExplore.exe process can't be started");

Thread.Sleep(3000);
handle = process.MainWindowHandle.ToInt32();
var allBrowsers = new SHDocVw.ShellWindows();
if (handle != 0)
{
    foreach (InternetExplorer browser in allBrowsers)
        if (browser.HWND == handle)
        {
            ie = browser;
            iehandle = (IntPtr)ie.HWND;

            break;
        }
}