Wednesday 20 July 2016

Deleting Browser Temporary files, Cookies, History with QTP-VBScript

When we are executing end to end Automation scripts(from login to logoff) in batch mode, we may come across an issue in one of the scripts and script doesn't execute logoff functionality and hence next script tries to open browser and perform login functionality  whereas application retains the session we have used previously and directly opens home page by logged in already.

If we have not done exception handling on this are then there are lot of chances to get our next scripts fails in the batch and this is blocker for batch(night mode) execution

Below are the few options to resolve this issue.

Delete the browser history, temp files, cookies while launching browser always(you can include this in login functionality, so that this will always makes sure it don't retain previous session)

Though we perform above action, if still session is getting retained then we can implement logic in Login script to verify application is launched with login page or home page, if it is displayed home page then logoff and call the login functionality again.

Below are few options to delete Browser Temporary files, Cookies, History with QTP-VBScript

'Option 1:
ClearBrowserHistory
Function ClearBrowserHistory
Dim temp
Set fso = CreateObject ("Scripting.FileSystemObject")
Set winsh = CreateObject ("Wscript.Shell")
Set temp = fso.GetFolder (winsh.ExpandEnvironmentStrings("%TEMP%"))
On Error Resume Next

For each ofile in temp.Files
fso.DeleteFile ofile
Next

For Each osubfldr in temp.subfolders
fso.DeleteFolder (osubfldr),true
Next
wscript.quit
End Function

'Option 2:
Webutil.DeleteCookies

'To clear temporary Internet files
Set WShell = CreateObject("WScript.Shell")
WShell.run "RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8"

'To Clear Browsing History
WShell.run "RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 1"

Wait (10)

'Option 3:
'To clear temporary Internet files
Set WshShell = CreateObject("WScript.Shell")
WshShell.run "RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8"

'To clear browsing cookies
WshShell.run "RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 2"

'To Clear Browsing History
WshShell.run "RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 1"

'Below are few more options to use
Browser("Browser Name").ClearCache
Browser("Google.com:").DeleteCookies "Google"
WebUtil.deletecookies '-> It will delete the entire cookies session which will present in your system.

No comments:

Post a Comment