June 16, 2011

Check an alert dialog box and the text from it

In this post I will try to describe step by step how to create a function which checks for a dialog box and will also click on the button within it:

  Firstly we will create a class which we'll be calling on our need:

' creating the generic function
Public Function DialogBox
Set DialogBox = New QTPDialogBox
End Function

' defining the class
Class QTPDialogBox
Public Function CheckDialogBox (obj_DialogWindowId, obj_DialogWindowStyle, obj_StaticWindowId, obj_StaticText)
' defining the Dialog object's properties
Set obj_Dialog = Description.Create  
obj_Dialog ("micclass").value = "Dialog"
obj_Dialog ("nativeclass").value = "#32770"
obj_Dialog ("text").value= "Microsoft Internet Explorer"
obj_Dialog ("window id").value= obj_DialogWindowId
obj_Dialog ("windowstyle").value= obj_DialogWindowStyle

' defining the Static object's properties
Set obj_Static = Description.Create  
obj_Static ("micclass").value = "Static"
obj_Static ("window id").value= obj_StaticWindowId
obj_Static ("text").value= obj_StaticText
With Browser("URL:=" & Environment.Value("baseUrl"))
'check if the dialog box exists
If .Dialog(obj_Dialog).Static(obj_Static).exist Then
' set a variable for the "text" property of Static object to be compared with the text value inserted by a user
DialogMessage = .Dialog(obj_Dialog).Static(obj_Static).GetRoproperty("text")
Dim Comparison
If Comparison = StrComp(Trim(obj_StaticText), Trim(DialogMessage), 0)  Then
Reporter.ReportEvent micPass, "PASS", "The Dialog box is present on the screen, and the text from it is: '" & DialogMessage & "', it is the same as: '"  & obj_StaticText & "'"
' click OK in dialog box
.Dialog(obj_Dialog).WinButton("text:=OK", "regexpwndclass:=Button", "window id:=2").Click
Reporter.ReportEvent micFail, "Fail", "The Dialog box is present on the screen, and the text from it is: '" & DialogMessage & "', it is not the same as: '" & obj_StaticText "'"
End If
Reporter.ReportEvent micFail, "FAIL", "There is no Dialog box present on the screen!"
End If
End with
End Function
End Class

Environment.Value("baseUrl") = it's your URL, which is defined into a XML config file, for e.g.: http://google.com/.*; see the XML variable below:

<Description>This variable  is used to identify the browser's URL</Description>
In this XML file, we can also have the user name, password, and a lot of general data which can be also called when it need.
To create the descriptions for Static and Dialog objects, use the Object Repository Manager.

 Now we can call this Function within our test in the next way:
' check if the dialog box contains the correct text and click on OK button
DialogBox.CheckDialogBox "0", "-1798831675", "65535", "Functionality not implemented in this release"
' (obj_DialogWindowId, obj_DialogWindowStyle, obj_StaticWindowId, obj_StaticText)  --- those are the properties added using Object Repository Manager

Bookmark It
Hide Sites


  1. I agree with your post, the Introduction of automation testing product shortens the development life cycle. It helps the software developers and programmers to validate software application performance and behavior before deployment. You can choose testing product based on your testing requirements and functionality. QTP Training In Chennai

  2. Hi, you have given really informative post. Thanks for sharing this post to our vision. Suppose if anyone interested to get QTP Training in Chennai, reach us besant technologies. Rated as No.1 IT training and placement institute in Chennai.

  3. Your article about software testing is awesome. It helped me to understand the career prospects in software testing industry. software testing training in Chennai