Tuesday, July 30, 2013

Clean session in Internet Explorer

Between tests and before starting test on client machine the browser should be in clean session in order for test not to interfere with previous session data. Otherwise tests might fail. There is a method in webdriver API called DeleteCookies to clear cookies of browser

However, this work perfectly fine for Firefox and Chrome but not always for Internet Explorer. For Firefox and Chrome, a new temporary profile while creating new browser instance in Webdriver and this is not the case in IE hence we might sometime see previous session details still persisting in new session.

QTP allows access to IE tools menu and options window to clear cookies and cache, however webdriver doesn't support interacting with menu. Hence we have to opt for command line option to clear cookies in IE.

Here is Code for same:

public void DeleteCookies()
        {
             if (_driver == null) return;
                _driver.Manage().Cookies.DeleteAllCookies();

                if (_driver.GetType() == typeof(OpenQA.Selenium.IE.InternetExplorerDriver))
                {
                     ProcessStartInfo psInfo = new ProcessStartInfo();
                     psInfo.FileName = Path.Combine(Environment.SystemDirectory, "RunDll32.exe");
                     psInfo.Arguments = "InetCpl.cpl,ClearMyTracksByProcess 2";
                     psInfo.CreateNoWindow = true;
                     psInfo.UseShellExecute = false;
                     psInfo.RedirectStandardError = true;
                     psInfo.RedirectStandardOutput = true;
                     Process p = new Process { StartInfo = psInfo };
                     p.Start();
                     p.WaitForExit(10000);
                }
        }

Only in case of IE, RunDll32.exe is invoked with arguments InetCpl.cpl,ClearMyTracksByProcess 2 to clear cookies and cache. To clear entire history including temp files, history, form data, passwords etc.. change arguments to InetCpl.cpl,ClearMyTracksByProcess 255. This should work on IE 7 and above on all OS.

Following are different arguments supported by InetCpl.cpl:
echo Clear Temporary Internet Files:
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8

echo Clear Cookies:
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 2

echo Clear History:
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 1

echo Clear Form Data:
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 16

echo Clear Saved Passwords:
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 32

echo Delete All:
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 255

echo Delete All w/Clear Add-ons Settings:
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 4351


3 comments:

  1. As of version 2.33.0.9 of IEDriverServer.exe, you can set the "ie.ensureCleanSession" capability to true for the IE driver, and it will perform this action for you. See the CHANGELOG[1] for full details. You can get a prebuilt binary containing this change in the Selenium Git repository[2], or you can wait for 2.34.0.0, which will provide the public release of this feature.

    [1] http://selenium.googlecode.com/git/cpp/IEDriverServer/CHANGELOG
    [2] http://selenium.googlecode.com/git/cpp/prebuilt/Win32/Release/

    ReplyDelete
    Replies
    1. Thanks Jim for the update, that would definitely make the task easier and reliable.

      Any idea when would be release of 2.34.0?

      Delete
  2. This was exactly what I needed. Thanks!

    ReplyDelete