SoftwareTesting:Tools:TestRunnerPython
From MozillaWiki
Overview
Simliar to SoftwareTesting:Tools:JsshDriverPython, this script loads test cases in a jssh-enabled browser using the python unittest framework.
probably requires python 2.4
Setup
- build the browser with jssh enabled
- add "ac_add_options --enable-extensions=default,jssh" to .mozconfig
- check out mozilla/extensions/jssh (on trunk, use HEAD. On branches, use -D 2006-04-15)
- start the browser
- add "-jssh" to the browser command line to enable jssh
- perhaps disable pop-up blocking
- run test_case_runner.py on your machine
test_case_runner.py
- loads each file in the same dir as itself that matches test*html
- for each test case file
- if var setupComplete exists and is set to false, wait until it is set to true
- if function setupTest exists, run it
- if function run_test exists, run it
- if var testComplete exists and is set to false, wait until it is set to true
- examine variable testResult - if it is true, then test passes, else test fails
- if function teardownTest exists, run it
todo
- convert/merge to jsunit-style naming of functions, assertions, and semaphore stuff
- report pass/fail for each test case individually, rather than one aggregate per file
test_case_runner.py source
#!/usr/bin/env python
import re
import sys, os, glob
import telnetlib
import unittest
import time
this_dir = os.path.abspath(os.path.dirname(__file__))
class JsshDriver:
COMMAND_PROMPT = "\n> "
def __init__(self, host=None, port=9997, telnetklass=telnetlib.Telnet):
"""Ctor
Ctor
"""
self.host = host
self.port = port
self.tn = telnetklass(host,port)
self.init()
def open(self,host,port=9997):
self.tn.close()
self.host = host
self.port = port
self.tn.open(host,port)
self.init()
def init(self):
if not self.tn.get_socket():
return
self.tn.read_until(JsshDriver.COMMAND_PROMPT)
self.send_command("setProtocol('synchronous')")
def send_command(self,command):
self.tn.write(command + "\n")
print (command + "\n")
return self.tn.read_until(JsshDriver.COMMAND_PROMPT)
def send_quit(self):
self.tn.write("quit()\n")
return self.tn.read_all()
class JsshTester:
def __init__(self, host, port=9997, telnetklass=telnetlib.Telnet):
self.browser = JsshDriver(host,port,telnetklass)
def __del__(self):
self.browser.send_quit()
def get_variable_value(self,variable):
jssh_response = self.browser.send_command ('print(' + variable + ')')
m = re.compile(r"\[(?P<len>\d+)](?P<rest>.*)", re.DOTALL).search(jssh_response)
return m.group('rest')[0:int(m.group('len'))]
def get_innerHTML_from_URL(self,url):
self.browser.send_command ('var browser = getWindows()[0].getBrowser()')
if url:
self.browser.send_command ('browser.loadURI("' + url + '")')
self.browser.send_command ('var document = browser.contentDocument')
self.browser.send_command ('var window = browser.contentWindow')
return self.get_variable_value('document.documentElement.innerHTML')
class JsshTestCaseRunner(unittest.TestCase):
def setUp(self):
self.get_test_case_files()
self.tester = JsshTester("localhost")
self.tester.browser.send_command('var w = getWindows()[0]')
self.tester.browser.send_command('var wc=w.content')
def get_test_case_files(self):
self.html_files = glob.glob("%s/test*html" % this_dir)
def wait_for_var(self,variable_name):
if "false" == self.tester.get_variable_value(variable_name):
while "true" != self.tester.get_variable_value(variable_name):
time.sleep(1)
def run_if_function(self,function_name):
return self.tester.browser.send_command('if (typeof ' + function_name + ' == \'function\') { ' + function_name +'(); }')
def do_setup(self,filename):
local_url = "file:///" + filename
local_url = re.sub(r"\\", "/", local_url)
self.tester.browser.send_command('wc.location="' + local_url + '";' )
self.wait_for_var('wc.setupComplete')
self.run_if_function('wc.setupTest')
def do_runTest(self):
self.run_if_function('wc.run_test')
self.wait_for_var('wc.testComplete')
self.failUnless("true" == self.tester.get_variable_value('wc.testResult'))
self.run_if_function('wc.teardownTest')
def testRunTest(self):
for file in self.html_files:
self.do_setup(file)
self.do_runTest()
def main():
suite = unittest.makeSuite(JsshTestCaseRunner)
unittest.TextTestRunner().run(suite)
if __name__ == '__main__':
main()