簡體   English   中英

可以為硒測試設置時間限制嗎?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM