[Checkins] SVN: gocept.selenium/branches/gotcha-generator/ add script first step

Godefroid Chapelle gotcha at bubblenet.be
Tue Jan 4 04:07:28 EST 2011


Log message for revision 119332:
  add script first step

Changed:
  U   gocept.selenium/branches/gotcha-generator/CHANGES.txt
  A   gocept.selenium/branches/gotcha-generator/src/gocept/selenium/scripts/
  A   gocept.selenium/branches/gotcha-generator/src/gocept/selenium/scripts/__init__.py
  A   gocept.selenium/branches/gotcha-generator/src/gocept/selenium/scripts/generatetests.py

-=-
Modified: gocept.selenium/branches/gotcha-generator/CHANGES.txt
===================================================================
--- gocept.selenium/branches/gotcha-generator/CHANGES.txt	2011-01-04 08:57:19 UTC (rev 119331)
+++ gocept.selenium/branches/gotcha-generator/CHANGES.txt	2011-01-04 09:07:28 UTC (rev 119332)
@@ -4,7 +4,8 @@
 0.10 (unreleased)
 -----------------
 
-- Nothing changed yet.
+- Script that generates python tests from Selenium HTML tables.
+  Reused from KSS project, with permission of original author Jeroen Vloothuis.
 
 
 0.9 (2010-12-28)

Added: gocept.selenium/branches/gotcha-generator/src/gocept/selenium/scripts/__init__.py
===================================================================
--- gocept.selenium/branches/gotcha-generator/src/gocept/selenium/scripts/__init__.py	                        (rev 0)
+++ gocept.selenium/branches/gotcha-generator/src/gocept/selenium/scripts/__init__.py	2011-01-04 09:07:28 UTC (rev 119332)
@@ -0,0 +1 @@
+# python package

Added: gocept.selenium/branches/gotcha-generator/src/gocept/selenium/scripts/generatetests.py
===================================================================
--- gocept.selenium/branches/gotcha-generator/src/gocept/selenium/scripts/generatetests.py	                        (rev 0)
+++ gocept.selenium/branches/gotcha-generator/src/gocept/selenium/scripts/generatetests.py	2011-01-04 09:07:28 UTC (rev 119332)
@@ -0,0 +1,68 @@
+#!/usr/bin/env python
+"""
+Generate selenium test controller files from HTML selenium tests
+"""
+
+import re
+import glob
+from elementtree import HTMLTreeBuilder
+from string import Template
+
+template = Template('''
+from seleniumtestcase import SeleniumTestCase
+import unittest, time
+
+class seltest_$testname(SeleniumTestCase):
+
+$tests
+
+def test_suite():
+    return unittest.makeSuite(seltest_$testname)
+
+if __name__ == "__main__":
+    unittest.main()
+''')
+
+variable_regexp = re.compile('\$\{(?P<varname>\w*)\}')
+
+
+def formatcommand(command, *args):
+    if not command:
+        return '' # Change this to raise an exception?
+
+    arguments = []
+    for arg in args:
+        if not arg:
+            continue
+        matched = variable_regexp.match(arg)
+        if matched is None:
+            arguments.append('"%s"' % arg)
+        else:
+            arguments.append("self.getVar('%s')" % matched.group('varname'))
+    return 'self.%s(%s)' % (command, ', '.join(arguments))
+
+htmlparser = HTMLTreeBuilder.TreeBuilder()
+tests = []
+for filename in glob.glob('*.html'):
+    tree = HTMLTreeBuilder.parse(filename)
+    root = tree.getroot()
+
+    try:
+        testname = root.find('.//title').text
+    except AttributeError:
+        continue
+    commands = []
+    for row in root.findall('.//tbody/tr'):
+        commands.append(formatcommand(*[td.text for td in row.findall('td')]))
+
+    testfilename = 'seltest_%s.py' % testname
+    testbody = ('    def test_%s(self):\n' % testname + ' ' * 8 +
+        '\n        '.join(commands) + '\n')
+    tests.append(testbody)
+
+f = open('seltest_all.py', 'wb')
+f.write(template.substitute(dict(
+    testname=testname,
+    tests='\n'.join(tests),
+    )))
+f.close()



More information about the checkins mailing list