[英]Is it possible to set a time limit for a Selenium test?
我们开发了针对Chrome,Firefox和Safari的扩展程序,并使用Selenium测试了我们的Chrome和Firefox扩展程序。 但是问题是,某些Firefox测试卡住了几个小时,并且只有在我们终止显示sudo killall Xvfb
时才会停止(我们也可以终止测试过程)。 是否可以为Selenium测试设置一个时限(15分钟),如果达到该时限,测试将失败? 我试图设置页面加载超时,但不能解决问题:
self.driver = webdriver.Firefox(firefox_profile=firefox_profile)
time.sleep(30)
self.driver.set_window_size(width=1920, height=1080)
size = self.driver.get_window_size()
print("Window size: width = {}px, height = {}px.".format(size["width"], size["height"]))
self.driver.set_page_load_timeout(time_to_wait=45)
我们正在使用Selenium 2.45.0
。 我们需要升级吗?
我的课继承自unittest.TestCase
。
您可以尝试超时装饰器进行测试
import time
import timeout_decorator
@timeout_decorator.timeout(5)
def mytest():
print "Start"
for i in range(1,10):
time.sleep(1)
print "%d seconds have passed" % i
if __name__ == '__main__':
mytest()
有关更多详细信息,请参阅: timeout-decorator
好吧,一个怪异的解决方法是使用线程。 线程是同时运行Python程序的两个部分的一种方式。 您可能会与其他代码一起运行计时器,并且当计时器用完时,运行kill
命令。 例如
from thread import start_new_thread
import os
import time
def your_code():
# Your code
def timer(): # Say the time limit is 15 minutes
for i in range(16):
for z in range(60):
time.sleep(1)
print i,z
if i >= 15:
os.system("sudo killall Xvbf")
start_new_thread(your_code)
start_new_thread(timer)
while 1:
pass
其他资源:
http://www.tutorialspoint.com/python/python_multithreading.htm
编码愉快! 祝你好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.