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.

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"

Tuesday 19 July 2016

Mercury DeviceReplay option in QTP

Apart from SendKeys option, DeviceReplay is another alternative way to take control on objects which are not getting recognized.

The Device Replay feature is used to perform mouse and keyboard actions against screen coordinates that are provided. The Device Replay functions are not automatically recorded, but must be programmed manually in the Expert View.
1. Create the Device Replay Object.
Example:
Set DeviceReplay = CreateObject(“Mercury.DeviceReplay”)

2. Call the desired Device Replay function.
Example:
DeviceReplay.SendString(“Hello, Automation Testing World”)

3. When done with the Device Replay object, release it.
Example:
Set DeviceReplay = Nothing

The functions that can be used with the Device Replay object are (all coordinates are relative to the top left corner of the screen)

The functions that can be used with the Device Replay object are (all coordinates are relative to the top left corner of the screen):
FunctionDescription
MouseMove x, yMove the mouse to the screen coordinate (x,y).
MouseClick x, y, buttonMove the mouse to the screen coordinate (x,y) and click the button
(0=left; 1=middle; 2=right).
MouseDblClick x, y, buttonMove the mouse to the screen coordinate (x,y) and double-click the button
(0=left; 1=middle; 2=right).
DragAndDrop x, y, dropx, dropy, buttonDrag the mouse from screen coordinate (x,y) to (dropx,dropy) with the button
(0=left; 1=middle; 2=right) pressed.
PressKey keyPress a key using the ASCII code of the key.
For example, Chr(13), vbCR and vbTab.
MouseDown x, y, buttonPress the mouse button on screen coordinate (x,y).
MouseUp x, y, buttonRelease the mouse button on screen coordinate (x,y).
KeyDown keyPress a key using the ASCII code of the key.
For example, Chr(13), vbCR and vbTab.
KeyUp keyRelease a key using the ASCII code of the key.
For example, Chr(13), vbCR and vbTab.
SendString stringType a string.

UFT/QTP using SendKeys option

When we are automating an application, sometimes objects may not be recognized. If we don't have any other option to recognize objects with QTP then we can chose option to take the keyboard control and automate the pending part.

Now a days, almost all applications are designed in such a way to follow W3C standards. For example, if we want to click on a button and currently cursor is focused on another object, we can use keyboard Tab option till that button focused. and then later we can hit on Enter button from keyboard to achieve our goal(clicking on button which is not recognized with QTP)

When this approach is recommended:
If we have automated almost 95%  of our script and only less then 5% steps are not able to automate because of objects not getting recognized then we can go ahead with keyboard sendkeys option.

To achieve this option we have a COM object available in VBScript.

Set objWshShell = WScript.CreateObject("WScript.Shell")

once above object is created then with this object
objWshShell.SendKeys "{TAB}"
objWshShell.SendKeys "{ENTER}"

Or

objWshShell.SendKeys "{TAB}~"

this will resolve above problem to click on button.

Below are few keystokes with equivalent to keyboard buttons available in VBScript

KeystrokeEquivalent
Alt%
Backspace{BACKSPACE}
Break{BREAK}
Caps Lock{CAPSLOCK}
Ctrl^
Delete{DELETE}
Down Arrow{DOWN}
End{END}
Esc{ESC}
Help{HELP}
Home{HOME}
Insert{INSERT}
Left Arrow{LEFT}
Num Lock{NUMLOCK}
Page Down{PGDN}
Page Up{PGUP}
Print Screen{PRTSC}
Right Arrow{RIGHT}
Scroll Lock{SCROLLOCK}
Shift+
Tab{TAB}
UP Arrow{UP}
F1{F1}
F2{F2}
F3{F3}
F4{F4}
F5{F5}
F6{F6}
F7{F7}
F8{F8}
F9{F9}
F10{F10}
F11{F11}
F12
{F12}