if your cases need login with different role, they will encounter some problems if you don’t clear browser cookies.
Below is way how to do it. Got it from http://automationoverflow.blogspot.com/
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