简体   繁体   中英

Is it possible to set a time limit for a Selenium test?

We develop extensions for Chrome, Firefox and Safari and we test our Chrome and Firefox extensions with Selenium. But the problem is, some of the Firefox tests get stuck for hours, and they only stop when we kill the display sudo killall Xvfb (we could also kill the test process). Is it possible to set a time limit (15 minutes) for the Selenium tests, and the test will fail if it reached the time limit? I tried to set page load timeout but it doesn't solve the problem:

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)

We are using Selenium 2.45.0 . Do we need to upgrade?

My class inherits from unittest.TestCase .

you can try timeout decorator for your test

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()

For more detail refer: timeout-decorator

Well, a weird work around would be to use threading. Threading is a way of running two parts of a Python program at the same time. You could potentially run a timer alongside your other code and, when the timer runs out, run the kill command. For example

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

other resources:

http://www.tutorialspoint.com/python/python_multithreading.htm

Happy coding! and best of luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM