1

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.

Uri
  • 2,992
  • 8
  • 43
  • 86
  • 1
    what's the reason for the tests getting stuck? – drkthng Aug 25 '15 at 12:36
  • @drkthng I don't know, but it happened that tests stopped (I'm not sure if they passed or failed) even after 60 minutes. – Uri Aug 25 '15 at 12:39
  • 2
    Selenium instantly stops and kills open browsers if you run `sys.exit()` (needs `import sys`) so you could start a 2nd thread in your script that sleeps 15 minutes and then executes `sys.exit()` – 576i Aug 25 '15 at 12:41
  • You need to just add timeouts to the various functions that will potentially be long running or get stuck. To find those, log messages as you enter various parts of the tests, call functions, etc. so you can see the progress of the script when it gets stuck. – JeffC Aug 25 '15 at 17:58
  • @576i `sys.exit()` from a second thread doesn't stop the main thread. – Uri Aug 26 '15 at 12:43

2 Answers2

2

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

Kavan
  • 569
  • 4
  • 21
  • I used `@timeout_decorator.timeout(900)` for 15 minutes, I want to check all browsers but I think it works. But the problem is, it doesn't work on my local computer (Windows), only on the Ubuntu server it works. On my computer I get this error message: `AttributeError: 'module' object has no attribute 'SIGALRM'` in line 66 of `timeout_decorator.py`. I can run the tests without the decorator on my computer, but is it possible to fix this error on my computer? Even if disabling the timeout when I run tests on my computer. – Uri Aug 26 '15 at 14:52
  • http://stackoverflow.com/questions/16371421/python-signal-dont-work-even-on-cygwin I don't think it will be possible in windows as signals are specific to POSIX system – Kavan Aug 27 '15 at 16:01
  • I need a solution to at least disable the decorator on Windows, but let it work on Linux. – Uri Aug 30 '15 at 06:53
  • I'm accepting this answer, and I'll post a new question how to make it work on Windows. – Uri Aug 31 '15 at 10:49
1

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!

Joseph Farah
  • 2,463
  • 2
  • 25
  • 36
  • 2
    I prefer not to kill the display (`Xvbf`) because (1) the test can be executed on Windows, and (2) the same display is for 20 tests and if I kill it they will all fail. – Uri Aug 26 '15 at 06:36
  • @Uri I honestly just used the example from your post XD you can use whichever terminate command you want. – Joseph Farah Aug 26 '15 at 16:47