0

I'm automating tests using UFT 12.52 for an application.

I captured the objects for a repository and then started to create a sequence of actions on the objects.

However, there is a frequent problem. Before calling an action on an object it needs to exist, be visible, and in some cases be enabled (e.g. textboxes).

To solve this problem I tried testing if the object exists, and is visible (and enabled, if applicable), before the call to any action on the object. If those are true, then make a action with the object. Testing these properties of the object is done until a timeout has been exceeded. If the timeout is exceeded an exception should be thrown.

I developed the code below, but the result is very unstable behavior:

  • Non-recognition of the current object being tested
  • No recognition of the parent object
  • UFT starts to test an object of the 2nd line, before executing the action on the object of the 1st line ...

I have the following questions:

  • Is there a UFT/VBScript solution that solves this problem?
  • Are there problems in my algorithm?

Here is my code:

Function test() 
    clickObj SwfWindow("<parent object name 1 >").SwfButton("<child object name 1>"), true  
    clickObj SwfWindow("<parent object name 2 >").SwfButton("<child object name 2>"), false
End Function

Function clickObj(obj, isEnabled)   
    If (prepareObj(obj, isEnabled) = true) Then     
        obj.Click       
    Else        
        Err.Raise 424 'Error 424 = "Object required"        
    End if  
End Function

Function prepareObj(obj, isEnabled) 
    If isEnabled = true Then        
        If ((testObjProperty(obj,"exist") = true) And (testObjProperty(obj,"visible") = true) And (testObjProperty(obj,"enabled") = true)) Then     
            prepareObj = true   
        else            
            prepareObj = false          
        End If      
    Else    
        If ((testObjProperty(obj,"exist") = true) And (testObjProperty(obj,"visible") = true)) Then     
            prepareObj = true                   
        else            
            prepareObj = false          
        End If  
    End If          
End Function

Function testObjProperty(obj, propertyName) 
    Dim startTime : startTime = Timer
    Dim elapsedTime : elapsedTime = 0
    Dim propertyOk : propertyOk = false
    Dim timeout : timeout = 45

    Do      
        Select Case propertyName            
            Case "exist"                
                propertyOk = obj.Exist(1)                           
            Case "visible"              
                propertyOk = obj.GetROProperty("visible")                           
            Case "enabled"              
                propertyOk = obj.GetROProperty("enabled")                                       
        End Select

        If propertyOk = true Then           
            testObjProperty = true                                  
            Exit Function
        Else            
            elapsedTime = Timer - startTime                         
        End If              
    Loop While timeout > elapsedTime

    testObjProperty = false         
End Function
1

There is a logic error in this line of code:

If ((testObjProperty(obj,"exist") = true) And (testObjProperty(obj,"visible") = true) And (testObjProperty(obj,"enabled") = true)) Then

There is a logic statement A And B And C, and VBScript is computing each parameter A, B, C; regardless of the value of A, B and C, even if A and B is False, C is still evaluated.

If the first statement (testObjProperty(obj,"exist") = true) is False, VBScript is still going to execute the second statement (testObjProperty(obj,"visible") = true) and it will definitely fail here one day because the object may not exist.

You should perform the check below (pseudo-code):

If Object.Exist Then
    If Object.Visible And Object.Enabled Then
        ...
    Else
        ...
    End If
Else
    ...
End If

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.