等待UI¶
一些复杂的测试用例中,不可能只是不断地主动控制或者读取属性。通过被动地获取UI状态改变的事件,这样有助于写出不混乱的测试脚本。Poco提供了简单的轮询机制去同时轮询1个或多个UI控件,所谓轮询就是依次判断UI是否存在。本节将详细介绍UI同步方法,以让你的脚本时刻保持与当前UI状态同步,适应各种各样的运行环境。
下面例子展示的是最简单的UI同步
# 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()
下面例子展示轮询UI时等待 任意一个 UI出现就往下走
The fish and bomb will come up from bottom right to left. Click the fish and avoid the bomb.

# 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)
下面例子展示轮询UI时等待 所有 UI出现才往下走
Wait until all 3 fishes appear on the screen.

# 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)
更多示例: