Search This Blog

Tuesday, September 21, 2010

Recovery Scenario for Unknown Window

The following code can be added in the recovery scenario as a function call. When the occurence is set to "On Error" it will check for any unknown window, if it finds this unknown window, it will click ok or cancel button or any other button which we are giving in the script. If there is not such unwindow and still the object expected by QTP is not visible, then it will also click Retry for the QTP error.

This is very important since we are handling here both application generated as well as QTP generated errors. You can also add the function for caputuring the screenshot here to make it an excellent looking Recovery Scenario.

Function UnknownWindow(Object, Method, Arguments, retVal)

Dim var

var=Array("&No","OK","Ok", "Cancel", "Close")
Set o_Desc=description.Create
Set o_Nobutn=description.Create
o_Desc("Class Name").value="Dialog"
o_Desc("ispopupwindow").value=True

If Window(o_Desc).Exist Then
Window(o_Desc).Activate
Arr_Count=Ubound(var)

For i= 0 to Arr_Count
o_Nobutn("text").value=var(i)
If Window(o_Desc).winbutton(o_Nobutn).Exist(0) Then
Enabled = Window(o_Desc).winbutton(o_Nobutn).GetROProperty("enabled")
If Enabled = "True" Then
Exit For
End If

End If
Next
Window(o_Desc).winbutton(o_Nobutn).Click
Else

QTP_Error

End If

End Function

'-----------------------------------------------------------


Public Function QTP_Error

count = 1
For Count = 1 to 2
Set WshShell =CreateObject("WScript.Shell")
Btncode = WshShell.Popup ("object is disabled", 1)
WshShell.SendKeys "{TAB}"
wait 2
WshShell.SendKeys "~"
Next
CloseApplication
ExitTestIteration

End Function

Capturing the screenshot in case of error

The following piece of code can be inserted as a fucntion call in you recovery scenario. If you set the occurence as "On Error", it will take the screenshot of your screen in the event of an error. This is helpful in understanding what exactly happend before the recovery scenario was triggered.

Dim filename

Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FolderExists("D:\Automation\Results\Screenshots")= "False" Then
FSO.CreateFolder("D:\Automation\Results\Screenshots")
End If

timenow = now
timenw= split (timenow, " ")
filename = RecipeNo &"_"&timenw(1)&".png"
filename = Replace(filename,"/","")
filename = Replace(filename,":","")
filename = "D:\Ranger Automation\Results\Screenshots"&"\"&filename
Desktop.CaptureBitmap filename,true
Reporter.ReportEvent micFail,"image","<"

Create a line chart in excel

Set Excel = CreateObject("Excel.Application")
Set Book = Excel.Workbooks.Open("E:\Performance.xls")
Set Sheet = Book.Worksheets(1)

Set Range = Sheet.UsedRange
Range.Select ' Selects the area in the excel sheet with the data

Set Charts= Excel.Charts
Charts.Add

Set objCharts = Charts(1)
objCharts.Activate

objCharts.ChartType = 65 ' This will select a line chart

objCharts.SeriesCollection(1).Border.Weight = -4138 'Used to increase the width of the line in the graph
objCharts.SeriesCollection(2).Border.Weight = -4138
objCharts.SeriesCollection(3).Border.Weight = -4138

Book.Save
Excel.Quit

For more details and other types of charts. Please refer the website:
http://technet.microsoft.com/en-us/library/ee692900.aspx

Monday, August 30, 2010

Creating, Renaming and Data Entry in Excel

The below code is for creating your own excel application, custom naming the sheets and entering data in an excel. This is very useful if you have to get some data from the application and your datatable already has some other application input data. Basically a case where you need to have more than one excel sheets.

Set Excel = CreateObject("Excel.Application")
Set Book = Excel.Workbooks.Add
Set Sheet = Book.Worksheets(1)
Sheet.Name = "Results"
Sheet.Rows(1).Columns(1).value = "This is the first Sheet"

Book.SaveAs "D:\Results.xls"
Book.Close
Excel.Quit

To Rename the second sheet you just need to modify the Worksheet id in the above code. It would look like

Set Excel = CreateObject("Excel.Application")
Set Book = Excel.Workbooks.Open ("D:\Results.xls")
Set Sheet1 = Book.Worksheets(2)
Sheet1.Name = "Output_Data"

You need not close the excel to rename the second sheet. This code can be combined with the above code also. As per your needs. But you should make sure that the name does not exceed the default length in excel

Friday, August 27, 2010

Sending mails using Outlook and SMTP

Here we will see how we can send the mail after the test run using outlook and also using SMTP. First let us see how to send the mail using Outlook. This method though useful has the following drawbacks:
1. The system or machine where you are running the scripts should have outlook configured in it
2. The email will be sent in the name of the person whose email has been configured in the outlook.
3. Finally, outlook throws up a security message when you try to send an automated mail and to take care of this you will have to download a third party software called expressclick.

Well, if you still prefer to go by this method, then here is the code

systemutil.Run "C:\Program Files\Express ClickYes\ClickYes.exe"
wait 5
set oMailobj=CreateObject("Outlook.Application")

set oSendmail=oMailobj.CreateItem(0)
oSendmail.To=""
'oSendmail.Cc=""
oSendmail.Subject="Test"
oSendmail.Body="Hi" & vbcrLf & vbCrlf & "PFA the Smoke Test Automation Results" & vbCrlf & vbCrlf & "Thanks" & vbcrlf & "Automation Team"
sAttachment = "D:\Results.zip"
If (sAttachment <> "") Then
oSendmail.Attachments.Add(sAttachment)

Else
Reporter.ReportEvent micInfo,"Sending mail:","Unable to send attachment,please verify your attachment file"
End If
oSendmail.Send
wait 3
set oSendmail=Nothing
set oMailobj=Nothing
SystemUtil.CloseProcessByName "ClickYes.exe"

All these problems can be overcome by using SMTP mail. The only thing that you need to know is the SMTP server name and the machine should be in the network. Once you use this code you will never go back to using Outlook.

Set oMessage = CreateObject("CDO.Message")

oMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

'SMTP Server
oMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") ="servername or ip"

oMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

oMessage.Configuration.Fields.Update
oMessage.Subject = "Test"
oMessage.Sender = "" ' any non-existing mail id will also work.
oMessage.To =""
'oMessage.CC = ""
'oMessage.BCC = ""

oMessage.AddAttachment "D:\Results.zip"
oMessage.TextBody = "Hi" & vbcrLf & vbCrlf & "PFA the Smoke Test Automation Results & vbCrlf & vbCrlf & "Thanks" & vbcrlf & "Automation Team" & vbCrlf & vbCrlf & vbCrlf &"*********************************************************************************************************" & vbcrlf & "This is an auto-generated email. Please do not reply to this email." & vbcrlf &"*********************************************************************************************************"
oMessage.Send

Set oMessage = Nothing

Tuesday, August 17, 2010

How to close an application opened through QTP

For opening an application through QTP we use the command SystemUtil.Run. We have a similar command for closing the opened function we as well. Both opening and closing an application called "application" is illustrated below:

Opening an application through QTP:
SystemUtil.Run "C:\Program Files\Application.exe", " ", "C:\Program Files", " "

Closing the same applcation through QTP:
SystemUtil.CloseProcessByName "Application.exe"

Other similar commands available are mentioned below and their names are self explainatory.
SystemUtil.CloseDescendentProcesses
SystemUtil.CloseProcessByHwnd
SystemUtil.CloseProcessById
SystemUtil.CloseProcessByWndTitle

Monday, August 16, 2010

How to search for data in a tab separated log file

Set Fso = CreateObject("Scripting.FileSystemObject")

Const ForReading = 1, ForWriting = 2
Set File1 = Fso.OpenTextFile ("abc.txt", ForReading, True)


Do While File1.AtEndOfStream <> True

text = File1.ReadLine()
newtext = replace (text, " ", " ")' Copy the tab from the Notepad and paste here.For details check the syntax for Replace function in QTP help

data = split (newtext, " ")
For i = Lbound(data) to UBound(data)
If data(i) = "TimeStamp" Then
msgbox "true"
End If
Next
Loop