Androidviewclient dump()在转换屏幕上挂起

时间:2015-03-24 06:06:02

标签: python emulation dump androidviewclient

我在Windows上使用最新的AndroidViewClient版本。在脚本单击按钮后,应用程序连接到远程服务器并等待响应,在此期间有一个类似的等待进度条"屏幕上。问题是等待时间是随机的。我使用while循环等待下一页屏幕的特定视图,类似于这篇文章" Waiting for a specific view on androidviewclient"。但是如果time.sleep()太短,脚本会永远挂在代码行vc.dump()上,恰好在代码行上"收到+ = s.recv(1024)"此方法dump()用于ViewServer。监视器ViewClient.setAlarm(120)但是signal.alarm在Windows上不起作用。为什么不在收到+ = s.recv(1024)和try / except块之前使用s.settimeout(120)来阻止Windows上的阻塞状态?

1 个答案:

答案 0 :(得分:0)

通过在函数调用上使用超时并在while循环中重试直到成功,我避免了这个问题。有关如何使用信号的信息,请参见Timeout on a function call

以下基于文本的视图等待功能:

import signal

def wait_for_text(vc, text):
    while 1:
        try:
            print('vc dump for text: ' + text)
            signal.alarm(5)
            vc.dump(window=-1, sleep=0)
            signal.alarm(0)
            print('finished: ' + text)
            if vc.findViewWithText(text) is None:
                time.sleep(0.5)
            else:
                return
        except:
            exc_type, exc_value, exc_tb = sys.exc_info()
            print(''.join(trace.format_exception(exc_type, exc_value, exc_tb)))
            time.sleep(0.5)
    raise RuntimeError('Failed waiting '+str(timeout)+'s for text: ' + text)