Selenium – Clear browser cookies

本文介绍如何在不同的浏览器中清除Cookie以确保测试环境的纯净性。针对Firefox、Chrome和Internet Explorer提供了具体的实现方法,特别关注了IE浏览器中可能出现的问题及解决方案。

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

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值