Back to index

plone3  3.1.7
builder.py
Go to the documentation of this file.
00001 import sys, os, re, textwrap
00002 from elementtree import HTMLTreeBuilder
00003 from string import Template
00004 
00005 # --
00006 # This is used to build the seltest_all.py file into the same directory.
00007 # --
00008 
00009 # root directory finding code
00010 def getRootDirOfModule(module_name):
00011     return os.path.dirname(sys.modules[module_name].__file__)
00012 
00013 def getRootDirOfClass(cls):
00014     return getRootDirOfModule(cls.__module__)
00015 
00016 def getRootDirOfInstance(obj):
00017     return getRootDirOfClass(obj.__class__)
00018 
00019 def getSeleniumTestsFromSuite(owner_instance, suite):
00020     """Get absolute filenames of all tests from the suite.
00021     Suite contains the directory that we need to find.
00022     owner_instance is used for the directory finding,
00023     the suite's directory is traversed relative from that.
00024     """
00025     test_filenames = []
00026     # The owner instance is only used to traverse the sute directory
00027     # relative from it.
00028     root_dir = getRootDirOfInstance(owner_instance)
00029     tests_root_dir = os.path.join(root_dir, suite.test_directory)
00030     for test_filename in os.listdir(tests_root_dir):
00031         if test_filename.lower().endswith('.html') or \
00032                 test_filename.endswith('.htm'):
00033             test_filenames.append((test_filename, os.path.join(tests_root_dir, test_filename)))
00034     # sort the testfilenames
00035     test_filenames.sort()
00036     # return only the absolute filenames
00037     test_filenames = [filename[1] for filename in test_filenames]
00038     return test_filenames
00039 
00040 template = Template(textwrap.dedent('''\
00041         from seleniumtestcase import SeleniumTestCase
00042         import unittest, time
00043 
00044         class seltest_$testname(SeleniumTestCase):
00045 
00046         $tests
00047 
00048         def test_suite():
00049             return unittest.makeSuite(seltest_$testname)
00050 
00051         if __name__ == "__main__":
00052             unittest.main()
00053         '''))
00054 
00055 variable_regexp = re.compile('\$\{(?P<varname>\w*)\}')
00056 
00057 def formatcommand(command, *args):
00058     if not command:
00059         return '' # Change this to raise an exception?
00060 
00061     arguments = []
00062     for arg in args:
00063         if not arg:
00064             continue
00065         matched = variable_regexp.match(arg)
00066         if matched is None:
00067             arguments.append('"%s"'%arg)
00068         else:
00069             arguments.append("self.getVar('%s')"%matched.group('varname'))
00070     return 'self.%s(%s)' % (command, ', '.join(arguments))
00071 
00072 
00073 
00074 def cookSeleniumTests(filenames):
00075     """Cook selenium tests
00076     """
00077     # Try to open the file for writing.
00078     output_dir =  getRootDirOfModule('kss.demo.selenium_utils')
00079     output_filename = os.path.join(output_dir, 'seltest_all.py')
00080     try:
00081         f = open(output_filename, 'wb')
00082     except IOError, exc:
00083         raise IOError, ('Cannot open file "%s" for writing. '
00084                         'Make sure zope process has write access in directory. '
00085                         '["%s"]') \
00086                         % (output_filename, exc)
00087 
00088     htmlparser = HTMLTreeBuilder.TreeBuilder()
00089     tests = []
00090 
00091     # Now, we find all filenames that are in the suite passed to us.
00092     testname = 'all'
00093 
00094     for filename in filenames:
00095         tree = HTMLTreeBuilder.parse(filename)
00096         root = tree.getroot()
00097 
00098         try:
00099             testname = root.find('.//title').text
00100         except AttributeError:
00101             continue
00102         commands = []
00103         for row in root.findall('.//tbody/tr'):
00104             commands.append(formatcommand(*[td.text for td in row.findall('td')]))
00105 
00106         testfilename = 'seltest_%s.py' % testname
00107         testbody='    def test_%s(self):\n'%testname+' '*8+'\n        '.join(commands)+'\n'
00108         tests.append(testbody)
00109 
00110     # Use the last test's name as filename (???)
00111     # XXX I think we want to change this.
00112     f.write(template.substitute(dict(
00113         testname=testname,
00114         tests='\n'.join(tests),
00115         )))
00116     f.close()