Handling unexpected active Windows

A common problem in GUI testing is the appearance of an unexpected active window -- for example, a warning message box in an HTML browser. This topic describes how to handle these situations.

Imagine that you record a click on a secure page, and this link takes you to a page that is not secure. Assume your browser's security setting is adjusted to cause a message box to appear warning you that the next page will not be secure. You click OK to dismiss the warning message, and then you click on a check box on the unsecure page. The recorded Functional Test script would look something like this:


     LinkThatLeavesSecurePage().Click()
     Dialog_HtmlDialogButtonOK().Click() 
     CheckboxOnTheUnsecurePage().Click() 

When you play the script back against a browser with a different security setting, the script does not play back because the Dialog_HtmlDialogButtonOK()cannot be found. You can comment out the Dialog_HtmlDialogButtonOK().Click() statement, but you will have failures when the dialog does show up.

One solution is to wait for the message to appear. If it does not appear, you can continue. The solution can be achieved with the following code:


     LinkThatLeavesSecurePage().Click()
     Try 
        Dialog_HtmlDialogButtonOK().Click()
     
     Catch E As ObjectNotFoundException 
     End Try
       CheckboxOnTheUnsecurePage().Click()

This code accomplishes your primary goal. If the warning message appears, you dismiss it. If it does not appear, you eventually stop waiting and then continue. However, you may not want to wait the default amount of time for the warning message to show up. If you are sure that when the warning message does show up it will arrive within 5 seconds, you can speed this up by coding as follows:


     LinkThatLeavesSecurePage().Click()
     Try
        Dialog_HtmlDialogButtonOK().WaitForExistence(5,1)
        Dialog_HtmlDialogButtonOK().Click()
     
     Catch E As ObjectNotFoundException
     End Try
        CheckboxOnTheUnsecurePage().Click()

A reasonable objection to this approach is that you need to add this special code wherever a link on a browser might switch pages and cause a change in security. Handling this situation in a common place without changing many test scripts would be more efficient. By implementing the OnObjectNotFound exception you can handle the event whenever it occurs. By putting the implementation in a helper super script, you can handle the event for any test script that extends this helper super class.

The code in the following example implements a base class for scripts that test HTML applications. This base class implements OnObjectNotFound. The OnObjectNotFound method looks through all the HTML domains and looks for any HTML dialog boxes. Every HTML dialog box is dismissed by pressing Enter. If any dialog boxes are dismissed, the TestObject method is restarted. If no dialog boxes are dismissed, the method does nothing, and the ObjectNotFoundException is thrown as usual.


'This class provides some base capabilities for working
'with HTML.

 Imports Rational.Test.Ft.Object_Interfaces
 Public MustInherit Class HtmlScript Inherits RationalTestScript

 ' Overrides the base implementation of OnObjectNotFound. Whenever
 ' this event occurs, look through all the active domains (places
 ' where objects might be found). For HTML domains (Java
 ' and other domains are skipped) finds all the top objects.
 ' If the top object is an Html Dialog,
 ' types an Enter key to dismiss the dialog.
 ' Logs a warning when this happens.

 Public Overrides Sub OnObjectNotFound(ByVal TestObjectMethodState As ITestObjectMethodState)

 Dim DismissedAWindow As Boolean = false
 Dim Domains() As DomainTestObject = GetDomains()
 Dim I As Integer
 For I = 0 To Domains.Length - 1
    If (Domains(I).GetName().Equals("Html")) Then


     ' HTML domain is found.
     Dim TopObjects As TestObject() = Domains(I).GetTopObjects()
      If (Not(TopObjects Is Nothing)) Then
        Try
           Dim J As Integer
           For J = 0 To TopObjects.Length - 1
               If (TopObjects(J).GetProperty(".class").Equals("Html.Dialog"))
                 Then
                 'A top-level HtmlDialog is found.
                 LogWarning("HtmlScript.OnObjectNotFound - dismissing dialog.")
                 Try
                   DismissedAWindow = true
                   Dim CastTopObject As TopLevelTestObject
                   CastTopObject.InputKeys("{ENTER}")
                 Catch E As System.Exception
                 End Try
               End If
             Next J
           Catch
             'Unregister all references to top objects
             Unregister(TopObjects)
           End Try
        End If
      End If
    Next I
    If DismissedAWindow = True Then
      ' Try again
      TestObjectMethodState.FindObjectAgain()
    Else
      LogWarning( _
       "HtmlScript.OnObjectNotFound; no HTML dialog to dismiss")
    End If
 End Sub
End Class

Note that the above implementation of HtmlScript is only suitable for testing HTML. You might want to be able to use this base class for any script, including scripts testing Java. In this case, you must make sure that the TestObject is a HCL OneTest UI HTML object before dismissing the HTML dialog boxes. You can add the following code to the beginning of the OnObjectNotFound method:


     If Not (TestObjectMethodState.GetTestObject() _
          GetPropertyFromMap(IMapPropertyName.DOMAIN).Equals("Html"))

          Return
       End If