Enumerating Application Objects

The code below illustrates one way of enumerating the application objects while performing an operation on each object. The following example can be found in the EnumerateApplication.vbs and EnumerateApplication.py files located in the <Installdir>\CodeSamplesPlus folder.

VBScript sample

Copy code
Function EnumerateApp(ParentObj, Desc, OperationMethod, PostOperationMethod, RestoreMethod)
    dim ObjCol, CurrentObj, idx
    idx = 0
    ' Retrieve a collection of all the objects of the given description
    Set ObjCol = ParentObj.ChildObjects(Desc)

    Do While (idx < ObjCol.Count)
        ' Get the current object
        set CurrentObj = ObjCol.item(idx)

        ' Perform the desired operation on the object
        eval("CurrentObj." & OperationMethod)

        ' Perform the post operations (after the object operation)
        eval(PostOperationMethod & "(ParentObj, CurrentObj)")

        ' Return the application to the original state
        eval(RestoreMethod & "(ParentObj, CurrentObj)")

        idx = idx + 1
        ' Retrieve the collection of objects
        ' (Since the application might have changed)
        Set ObjCol = ParentObj.ChildObjects(Desc)
    Loop
End Function

' ********************************** An Example of usage **********************
' Report all the pages referred to by the current page
' ***********************************************************************************

Function ReportPage(ParentObj, CurrentObj)
    dim FuncFilter, PageTitle

    PageTitle = ParentObj.GetROProperty("title")
    FuncFilter = Reporter.Filter
    Reporter.Filter = 0
    Reporter.ReportEvent 0, "Page Information", "page title " & PageTitle
    Reporter.Filter = FuncFilter
End Function

Function BrowserBack(ParentObj, CurrentObj)
    BrowserObj.Back
End Function

' Save the Report Filter mode
OldFilter = Reporter.Filter
Reporter.Filter = 2 ' Enables Errors Only

' Create the description of the Link object
Set Desc = Description.Create()
Desc("html tag").Value = "A"

Set BrowserObj = Browser("creationtime:=0")
Set PageObj = BrowserObj.Page("index:=0")

' Start the enumeration
call EnumerateApp(PageObj, Desc, "Click", "ReportPage", "BrowserBack")

Reporter.Filter = OldFilter ' Returns the original filter

Python sample

Copy code
def enumerate_app(parent_obj, desc, operation_method, post_operation_method, restore_method):
    """
    Enumerates child objects of a parent object and performs operations on each of them.
    """
    idx = 0
    obj_col = parent_obj.ChildObjects(desc)

    while idx < len(obj_col):
        current_obj = obj_col[idx]
        operation_method(current_obj)
        post_operation_method(parent_obj, current_obj)
        restore_method(parent_obj, current_obj)
        idx += 1
        obj_col = parent_obj.ChildObjects(desc)


def browser_back(parent_obj, current_obj):
    BrowserObj.back()


def report_page(parent_obj, current_obj):
    page_title = parent_obj.GetROProperty("title")
    old_filter = Reporter.filter
    Reporter.filter = 0
    Reporter.ReportEvent(0, "Page Information", f"page title {page_title}")
    Reporter.filter = old_filter


old_filter = Reporter.filter
Reporter.filter = 2

desc = Description.create()
desc("html tag").Value = "A"

BrowserObj = Browser("CreationTime:=0")
PageObj = BrowserObj.page("index:=0")

enumerate_app(
    parent_obj=PageObj,
    desc=desc,
    operation_method=lambda obj: obj.click(),
    post_operation_method=report_page,
    restore_method=browser_back,
)

Reporter.filter = old_filter