I think that a test script framework should be entirely self contained (or at worst, as self contained as possible). In the case of Selenium RC scripts, this means that the server should be startable automatically if it is not present. Similarily, if you did start the server, it should be shut down. Heres the code which does just that.
But before I show the code, a note about the directory structure to allow things to make more sense.
- Everything in my framework is relative to whatever directory is indicated by test_root
- There are a bunch of unlisted dirs in the jre, but consider that dir the top level of it
- Dirs in platform are what is returned by sys.platform, and hold any platform specific modules / libraries
test_root |- some other stuff |- platform | `- win32 | `- jre1.5.0_11 |- server | ` selenium-server.jar `- even more stuff
import socket, os # check that the server is running s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: s.connect(("localhost", 4444)) started = "FALSE" except socket.error: s_path = os.path.join(test_root, "platform", sys.platform, "jre1.5.0_11", "bin", "java.exe") s_args = '-jar "%s%sserver%sselenium-server.jar"' % (test_root, os.sep, os.sep) if sys.platform == "win32": s_pid = os.system('start "Selenium RC Server" "%s" %s' % (s_path, s_args)) else: print "Ummm, don't have to do any other platforms right now" started = "TRUE"
# kill the selenium server if we were the one who started it if started == "TRUE": if sys.platform == "win32": import wmi c = wmi.WMI() for p in c.Win32_Process(Description="java.exe"): if p.ExecutablePath == s_path: p.Terminate() else: print "Read the message above, we only do windows right now"