Python实现 等待条件成立或超时后返回


最近用robotframework 自带的selenium库关键字进行页面脚本编写测试,发现有很多等待关键字,等待条件成立或时间结束后返回,本人之前一直在思考等待命令执行一定时间在接着执行,
认为可以借鉴次代码,将此处代码复制粘贴在博客上。

   def _wait_until_worker(self, condition, timeout, error):
        max_time = time.time() + timeout
        not_found = None
        while time.time() < max_time:
            try:
                if condition():
                    return
            except ElementNotFound as err:
                not_found = str(err)
            else:
                not_found = None
            time.sleep(0.2)
        raise AssertionError(not_found or error)


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM