user2706838
user2706838

Reputation: 1181

Event handler for the new specific object appearence on squish using python?

Let's say I'd like to hit button contained in the new dialog just in time this dialog will appear. How should my event handler look?

Example:

def handleMyNewDialogAppeared():
    mouseClick(waitForObject(":MyButtonOnNewDialog"), MouseButton.LeftButton)

def main():
    startApplication("myapp")
    installEventHandler("if dialog :MyNewDialog appeared", 
                        "handleMyNewDialogAppeared")

Upvotes: 0

Views: 1302

Answers (2)

Alex B.
Alex B.

Reputation: 1

There's a specific event named "DialogOpened" for that, however it hooks every dialog. You can then check in the handler whether it is the dialog you need, like this:

def handleMyNewDialogAppeared(Dialog):
    if str(Dialog.windowTitle) == "My Dialog's Title":  # whatever suits your needs
        mouseClick(waitForObject(":MyButtonOnNewDialog"), MouseButton.LeftButton)

def main():
    startApplication("myapp")
    installEventHandler("DialogOpened", "handleMyNewDialogAppeared")

I, however, only have squish for Qt so I cannot verify this for windows.

Upvotes: 0

Eugen
Eugen

Reputation: 805

I've never user the eventHandler from Squish. Instead of this (my problems is that all my objects are dynamic) I've created a custom wait function of mine. It works for all objects, no matter of their form/type.

This functions waits until an object is true.

def whileObjectFalse(objectID, log = True):
    # functions enters in a while state, as long as expected object is FALSE, exiting when object is TRUE 
    # (default timeout of [20s])

    start = time.time()# START timer
    counter = 40
    object_exists = object.exists(objectID)
    while object_exists == False:
        object_exists = object.exists(objectID)
        snooze(0.5)
        counter -= 1
        if counter == 0:
            test.fail(" >> ERR: in finding the object [%s]. Default timeout of [20s] reached!" % objectID)
            return
    if log == True:
        elapsed = (time.time() - start)# STOP timer
        test.log("    (whileObjectFalse(): waited [%s] after object [%s])" % (elapsed, objectID))
    snooze(0.5)

So basicaly, your code will be like this:

def whileObjectFalse(objectID, log = True):
        start = time.time()# START timer
        counter = 40
        object_exists = object.exists(objectID)
        while object_exists == False:
            object_exists = object.exists(objectID)
            snooze(0.5)
            counter -= 1
            if counter == 0:
                test.fail(" >> ERR: in finding the object [%s]. Default timeout of [20s] reached!" % objectID)
                return
        if log == True:
            elapsed = (time.time() - start)# STOP timer
            test.log("    (whileObjectFalse(): waited [%s] after object [%s])" % (elapsed, objectID))
        snooze(0.5)


def main():
    startApplication("myapp")
    whileObjectFalse("NewDialog")
    mouseClick(waitForObject(":MyButtonOnNewDialog"), MouseButton.LeftButton)

Upvotes: 0

Related Questions