Waiting for events

Interact with UI without feedback will be pretty hard in some cases, especially you are waiting for some events, notifications or state changing on UIs. Poco provides simple poll mechanism to allow you to poll some events of UIs. This section will show how to stay in sync with UIs and poll for state changing.

The following example shows the simplest way to stay in sync with UIs.

# coding=utf-8

from poco.drivers.unity3d import UnityPoco

poco = UnityPoco()

# start and waiting for switching scene
start_btn = poco('start')
start_btn.click()
start_btn.wait_for_disappearance()

# waiting for the scene ready then click
exit_btn = poco('exit')
exit_btn.wait_for_appearance()
exit_btn.click()

The following example shows how to poll any of UIs in one time.

The fish and bomb will come up from bottom right to left. Click the fish and avoid the bomb.

../../../_images/wait_any_ui.gif
# coding=utf-8

from poco.drivers.unity3d import UnityPoco
from poco.exceptions import PocoTargetTimeout

poco = UnityPoco()

bomb_count = 0
while True:
    blue_fish = poco('fish_emitter').child('blue')
    yellow_fish = poco('fish_emitter').child('yellow')
    bomb = poco('fish_emitter').child('bomb')
    fish = poco.wait_for_any([blue_fish, yellow_fish, bomb])
    if fish is bomb:
        # skip the bomb and count to 3 to exit
        bomb_count += 1
        if bomb_count > 3:
            return
    else:
        # otherwise click the fish to collect.
        fish.click()
    time.sleep(2.5)

The following example shows how to poll all of UIs in one time.

Wait until all 3 fishes appear on the screen.

../../../_images/wait_all_ui.gif
# coding=utf-8

import time
from poco.drivers.unity3d import UnityPoco

poco = UnityPoco()

poco(text='wait UI 2').click()

blue_fish = poco('fish_area').child('blue')
yellow_fish = poco('fish_area').child('yellow')
shark = poco('fish_area').child('black')

poco.wait_for_all([blue_fish, yellow_fish, shark])
poco('btn_back').click()
time.sleep(2.5)

See also: