''' A unittest wrapper to run Python Selenium tests in Selenium RC. ''' import os from subprocess import Popen import win32api import tempfile SELSERVER = 'selenium-server.jar' JAR = os.path.join(os.path.dirname(__file__), SELSERVER) PORT = 4444 def runInSeleniumRC( func, port = PORT, jar = JAR): ''' A function wrapper for unittest.main() that runs the unittest in a Selenium RC context. Example:: if __name__ == '__main__': seleniumtest.runInSeleniumRC(unittest.main, port=SELPORT)() @param func: The function to be wrapped. @param port: The port on which to run the Selenium RC server @param jar: Where the Selenium RC jar file resides ''' def runner(*args, **kwargs): _p = Popen(('java.exe' , '-jar' , jar , '-port' , str(port)) ,env = os.environ ,stdout = tempfile.TemporaryFile() ,stderr = tempfile.TemporaryFile() ) res = func(*args, **kwargs) if sys.platform == 'win32': win32api.TerminateProcess(int(_p._handle), -1) else: os.kill(_p.pid) return ret return runner