Saturday, December 13, 2008

Close the AUT after certain time

In recent days, I am trying to kill the applications if they are unable to close by the tool (SilkTest). Last month, I was solving one different issue.

We are having ten years old test suite and it was not updated for last two years due to product priorities. The total execution time for this regression suite is around 70 hours. Many times, our AUT is getting hung or the current window focus is unable to shift and suite is unable to continue after that. We endup to stop the suite at that script and re-ran the remaining scripts manually. Also we are unable to use weekends fully for the regression.

I planned to run the scripts as unattended. It is intermittent problem. Often silktest not executed in different testcases and not in one particular case always. So I thought to kill the application if the application is running more than 45 minutes. Few of silktest group members have given a idea and I have implemented it.

Silktest has 'spawn' statement to create a separate thread and it is used to execute MultiTestcase. MultiTestcae helps to run the particular code in multiple systems. Also I used PsList to get the running time of an application. I have tried few scenarios with Notepad and then finally put into our suite. This solution is worked..!!!

I have called in script level and I called 'spawn' statement in ScriptEnter and break the loop, once the execution reached ScriptExit. I have given the code below...

4Test code for ScriptEnter and ScriptExit



[+] void ScriptEnter ()
[ ] // Added for Unattended execution
[ ] gbAppKill = TRUE
[+] spawn
[ ] KillApp_UsedTime ("myAut",2700)
[ ]
[+] void ScriptExit (boolean bException)
[ ]
[ ] // Added to stop kill thread
[ ] gbAppKill = FALSE
[ ]



4Test methods to find the exeuction time and killing it


[ ] // // **** Code written for killing apps if app is running more than 45 mins.
[ ] // Methods
[ ] Boolean gbAppKill
[ ]
[+] Void KillApp_psKill (STRING sApp)
[ ] List of STRING lsOutput
[ ]
[ ] STRING sCommand = "{gcsToolsPath}\pskill {sApp} "
[ ]
[+] do
[ ]
[ ] Print ("Executing command: {sCommand}")
[ ] SYS_Execute (sCommand, lsOutput)
[ ] ResPrintList ("Command Output", lsOutput)
[ ] Print ("echo App {sApp} killed at %DATE%_%TIME% >> d:\kill.txt ")
[ ] SYS_Execute ("echo App {sApp} killed at %DATE%_%TIME% >> d:\kill.txt ")
[ ] Sleep (2)
[+] except
[ ] // Do Nothing
[ ] ExceptLog ()
[ ]
[ ]
[-] VOID KillApp_UsedTime (STRING sApp, INTEGER iKillTimeLimit optional)
[ ] List of STRING lsOutput
[ ] INTEGER iItem, iTotalValue, iTimes, iCount,iSleepTime
[ ] STRING sPartial,sItem,sPID
[ ] STRING sTime, sHour, sMin, sSec
[ ] STRING sCommand = "{gcsToolsPath}\pslist {sApp}"
[ ]
[ ] //sPID = ""
[ ]
[-] do
[ ]
[ ] iSleepTime = 50 //Seconds
[+] if (IsNull (iKillTimeLimit))
[ ] iKillTimeLimit = 6 // Seconds.
[ ]
[ ]
[ ] iCount = (iKillTimeLimit/iSleepTime) * 100 + 1+10
[ ]
[-] for iTimes=1 to iCount
[+] if (! gbAppKill)
[ ] Print ("gbAppKill is FALSE and exiting from Spawn thread..")
[ ] break
[ ]
[ ]
[ ] Sleep (iSleepTime)
[ ]
[ ]
[ ] Print ("Executing command: {sCommand}")
[ ] SYS_Execute (sCommand, lsOutput)
[ ] ResPrintList ("Command Output", lsOutput)
[ ]
[+] for iItem=2 to ListCount (lsOutput)
[ ] sItem = lsOutput[iItem]
[+] if (MatchStr ("{sApp}*",sItem))
[ ]
[ ] sSec = GetField (sItem,":",5)
[ ]
[+] if (IsNull(sSec) || (Trim(sSec) == ""))
[ ] continue
[+] else
[ ] sTime = GetField (sItem,":",3)
[ ] sMin = GetField (sItem,":",4)
[ ] sHour = SubStr (sTime,Len(sTime)-3)
[ ]
[ ] Print ("Hour: {sHour}")
[ ] Print ("Minutes: {sMin}")
[ ] Print ("Seconds: {sSec}")
[ ] iTotalValue = Val (sMin) * 60 + Val(sHour) * 3600
[ ] Print ("App {sApp} - running total time: {iTotalValue} Seconds")
[ ]
[+] if (iKillTimeLimit <= iTotalValue)
[ ] Print ("Application {sApp} is running {iTotalValue} seconds -beyond expected time {iKillTimeLimit} seconds.")
[ ] KillApp_psKill (sApp)
[ ] Print ("Going to kill Application {sApp}")
[ ] // break // It is suitable for testcase level
[+] else
[ ] Print ("Application {sApp} is running {iTotalValue} seconds - not exceeding expected time {iKillTimeLimit} seconds.")
[ ] // return sPID
[ ]
[ ]
[ ]
[+] except
[ ] // Do Nothing
[ ]
[ ]
[ ]

Monday, December 8, 2008

Closing Excel instance by VBScript

We have a test suite for Excel plug-in. Silktest built-in function does not work for Excel 2007. It is working fine for Excel 2003. Silktest is unable to close the Excel 2007 instance. We used Taskkill to kill the excel. It has created few other issues. I was looking for alternative solution. So I decided to write a vb script and got success. I have given the code below.

VBS code - To close Excel

'------------------------------------------------------------------------- ' File : CloseExcel.vbs ' Author : Palani ' Purpose : To close the excel, if it is already opened. ' '' Revision History: ''$Log: CloseExcel.vbs,v $ '' '------------------------------------------------------------------------- '' Usage '' cscript D:\rpm_scripts_palani\tools\CloseExcel.vbs '' cscript CloseExcel.vbs '******** Variables Declaration Dim gsLogFile '******** Function calls call CloseExcelApps () '-------------------------------------- ' Method : CloseExcelApps ' Author : T. Palani Selvam ' Purpose : Close Excel application. ' Parameters: - Nil ' Returns : - Nil ' Caller : - Nil ' Calls : - Nil '-------------------------------------- Sub CloseExcelApps() Dim sExcelPath 'As Variant 'Excel file '********** Excel object declaration **********' ' Excel Application object Dim objExcel 'As Excel.Application Dim objExcel2 'As Excel.Workbooks Dim objXLWorkbook 'As Excel.Workbook On Error Resume Next Set objExcel = GetObject(,"Excel.Application") If Not (IsNull(objExcel) Or IsEmpty(objExcel)) Then WScript.Echo ("Excel application instance Exists..") 'Set objXLWorkbook = objExcel.ActiveWorkbook 'You can set this property to True if you want to close a modified workbook 'without either saving it or being prompted to save it. objExcel.ActiveWorkbook.Saved = True objExcel.ActiveWorkbook.Close objExcel.Application.Quit 'objExcel.Worksbooks.Close 'objExcel.Quit Set objExcel = Nothing Set objExcel2 = GetObject(,"Excel.Application") If Not (IsNull(objExcel2) Or IsEmpty(objExcel2)) Then Set objExcel2 = Nothing WScript.Echo ("FAIL. Excel application is not closed properly.") Else WScript.Echo ("PASS. Successfully closed Excel application.") End If WScript.Echo ("End - Closing excel application instance.") else WScript.Echo ("Excel application instance does not exist!....") End If End Sub