是否可以为Selenium测试设置时间限制?

时间:2015-08-25 12:33:40

标签: python firefox selenium selenium-webdriver

我们为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

2 个答案:

答案 0 :(得分:2)

您可以尝试使用超时装饰器进行测试

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

答案 1 :(得分:1)

嗯,一个奇怪的工作是使用线程。 线程是一种同时运行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

快乐的编码!祝你好运!

相关问题