Back to index

plone3  3.1.7
createpythontests.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 raise SystemExit, 'Due to changes in the plugin registry, command line ' \
00004     'method is disabled until kss.demo is ported to kss.base.\n' \
00005     '    (Hint: call .../@@kss_demo_registry_admin/cookSeleniumTests from zope.)'
00006 
00007 # Generate selenium test controller files from HTML selenium tests
00008 
00009 from elementtree import HTMLTreeBuilder, ElementTree
00010 import glob
00011 from string import Template
00012 import os
00013 import re
00014 
00015 template = Template('''
00016 from seleniumtestcase import SeleniumTestCase
00017 import unittest, time
00018 
00019 class seltest_$testname(SeleniumTestCase):
00020 
00021 $tests
00022 
00023 def test_suite():
00024     return unittest.makeSuite(seltest_$testname)
00025 
00026 if __name__ == "__main__":
00027     unittest.main()
00028 ''')
00029 
00030 variable_regexp = re.compile('\$\{(?P<varname>\w*)\}')
00031 
00032 def formatcommand(command, *args):
00033     if not command:
00034         return '' # Change this to raise an exception?
00035 
00036     arguments = []
00037     for arg in args:
00038         if not arg:
00039             continue
00040         matched = variable_regexp.match(arg)
00041         if matched is None:
00042             arguments.append('"%s"'%arg)
00043         else:
00044             arguments.append("self.getVar('%s')"%matched.group('varname'))
00045     return 'self.%s(%s)' % (command, ', '.join(arguments))
00046 
00047 htmlparser = HTMLTreeBuilder.TreeBuilder()
00048 tests = []
00049 for filename in glob.glob('*.html'):
00050     tree = HTMLTreeBuilder.parse(filename)
00051     root = tree.getroot()
00052 
00053     try:
00054         testname = root.find('.//title').text
00055     except AttributeError:
00056         continue
00057     commands = []
00058     for row in root.findall('.//tbody/tr'):
00059         commands.append(formatcommand(*[td.text for td in row.findall('td')]))
00060 
00061     testfilename = 'seltest_%s.py' % testname
00062     testbody='    def test_%s(self):\n'%testname+' '*8+'\n        '.join(commands)+'\n'
00063     tests.append(testbody)
00064 
00065 f = open('seltest_all.py', 'wb')
00066 f.write(template.substitute(dict(
00067     testname=testname,
00068     tests='\n'.join(tests),
00069     )))
00070 f.close()