Monday, October 14, 2013

UFT (QTP) - Find Element by Properties

If you need to find a specific element with UFT (QTP) here is something to try:
Note: you can choose what properites to use:

Function GetAllSpecificControls(Page, MicClass, Innertext, HTMLTag)
    Dim Desc
    Set Desc = Description.Create()
    Desc("micclass").Value = MicClass
    Desc("innertext").Value = Innertext
    Desc("html tag").Value = HTMLTag
    Set GetAllSpecificControls = Page.ChildObjects(Desc)
End Function

Function FindMyObject(Page, MicClass, Innertext, HTMLTag)
    Select Case MicClass
                Case  "WebButton"      
                        Set FindMyObject = GetAllSpecificControls(Page, MicClass, Innertext, HTMLTag)  
                Case  "WebElement"
                        Set FindMyObject = GetAllSpecificControls(Page, MicClass, Innertext, HTMLTag)  
                Case  "Image"
                        Set FindMyObject = GetAllSpecificControls(Page, MicClass, Innertext, HTMLTag)  
                Case  "Link"
                        Set FindMyObject = GetAllSpecificControls(Page, MicClass, Innertext, HTMLTag)  
                Case  "Static"
                        Set FindMyObject = GetAllSpecificControls(Page, MicClass, Innertext, HTMLTag)  
                Case  "WebEdit"
                        Set FindMyObject = GetAllSpecificControls(Page, MicClass, Innertext, HTMLTag)  
                Case  "WebList"
                        Set FindMyObject = GetAllSpecificControls(Page, MicClass, Innertext, HTMLTag)  
                Case Else
                        myReport micFail, "An object of type """ & MicClass & """ could not be found!"
                        ExitIfError
    End Select
End Function

'Function call example
FindMyObject(Browser("Browser_Local").Page("Page"), "WebElement""ObjectToBeFoundInnertext""STRONG")(0).Click

No comments:

Post a Comment