Search code examples
pythongotosquish

how to work around go-to in python?


I've a scenario where I've called particular block of code after a set of actions. I used go-to and label in Python. It works as expected.

Is there any other better alternative for this?

This is Python code for automation using Squish-for-QT.

label .mylabel 

while (cond1):
        print("inside cond1")
        Function1(x,y,z)
else:
    if (object.exists("obj1")):
        Screen1 = waitForObject("obj1")
        print ("Inside Screen1")

        while (Screen1.visible):
            Function1(a,b,c)
        else:
            goto .mylabel 

Solution

  • In this particular case, wrapping the whole thing in a while True will achieve the same behavior:

    while True:
        while (cond1):
                print("inside cond1")
                Function1(x,y,z)
        else:
            if (object.exists("obj1")):
                Screen1 = waitForObject("obj1")
                print ("Inside Screen1")
    
                while (Screen1.visible):
                    Function1(a,b,c)
            else:
                break
    

    Checking this branch-by-branch:

    If cond1 is met, we continually execute Function1(x, y, z)

    Once cond1 is not met, we fall into else.

    If obj1 exists, we wait for obj1, otherwise, we break out of the while True loop.

    After waiting for obj1, we continue to run Function1(a,b,c) while Screen1 is visible, and then go back to the beginning of the while True loop (consistent with the original goto).