Author: reebalazs
Date: Fri Mar 28 11:03:11 2008
New Revision: 53043

Modified:
   kukit/kss.demo/branch/1.4-kss-test/kss/demo/browser/registry.py
   kukit/kss.demo/branch/1.4-kss-test/kss/demo/interfaces.py
   kukit/kss.demo/branch/1.4-kss-test/kss/demo/registry.py
   kukit/kss.demo/branch/1.4-kss-test/kss/demo/resource.py
   kukit/kss.demo/branch/1.4-kss-test/kss/demo/selenium_utils/builder.py
Log:
Progress on selenium test resources improvement

Modified: kukit/kss.demo/branch/1.4-kss-test/kss/demo/browser/registry.py
==============================================================================
--- kukit/kss.demo/branch/1.4-kss-test/kss/demo/browser/registry.py     
(original)
+++ kukit/kss.demo/branch/1.4-kss-test/kss/demo/browser/registry.py     Fri Mar 
28 11:03:11 2008
@@ -101,7 +101,8 @@
         """Return the html resource, whose absolute filename is given,"""
         # First of all, let's check if this is one of our file.
         # (Refuse otherwise - we don't want access to all files on the server.)
-        if filename not in self.selenium_test_filenames:
+        registry = getUtility(IKSSDemoRegistry)
+        if filename not in registry.all_registered_filenames:
             raise Exception, "Nonexistent resource"
         # Return the file's content.
         self.request.response.setHeader('Content-type', 
'text/html;charset=utf-8')

Modified: kukit/kss.demo/branch/1.4-kss-test/kss/demo/interfaces.py
==============================================================================
--- kukit/kss.demo/branch/1.4-kss-test/kss/demo/interfaces.py   (original)
+++ kukit/kss.demo/branch/1.4-kss-test/kss/demo/interfaces.py   Fri Mar 28 
11:03:11 2008
@@ -2,6 +2,7 @@
 from zope.schema import (
     TextLine,
     List,
+    Object,
     )
 
 class ISimpleContent(Interface):
@@ -42,13 +43,51 @@
         required=True,
         )
 
-class IKSSSeleniumTestDirectory(Interface):
-    """Represents an actual selenium test directory"""
+class IKSSSeleniumTestLayer(Interface):
+    """Represents a selenium test layer"""
 
-    test_directory = TextLine(
-        title=u"test directory",
-        description=u'Relative directory path, contains *.html selenium tests',
-        required=False,
+class IKSSSeleniumTestCaseCollection(Interface):
+    """Abstract interface representing a set of testcases"""
+
+    def get_filenames(plugin=None, test_root_dir=None):
+        """Return the list of absolute filenames in this collection
+        Each of this files are a .html format selenium test
+
+        Either of plugin and test_root_dir must e specified.
+        They are used to acquire the absolute path of the file
+        which is acquired from the plugin resource's definition
+        place or can be overwritten explicitely by test_root_dir.
+        """
+
+class IKSSSeleniumTestSuite(Interface):
+    """Represents an actual selenium test suite"""
+
+    tests = Object(IKSSSeleniumTestCaseCollection,
+        title = u'Test case collection',
+        description = u'A test case defines an ordered collection of test 
cases.',
+        required = True,
+        )
+
+    component = TextLine(
+        title = u'Component name',
+        description = u'Import identifier of the component that defines this 
suite.',
+        required = False,
+        default = u'',
+        )
+
+    application = TextLine(
+        title = u'Application name',
+        description = u'Freeform identifier of the application that this suite 
tests.'
+                      u'It is suggested but not obligatory to use dotted names 
for this.',
+        required = False,
+        default = u'',
+        )
+
+    layer = Object(IKSSSeleniumTestLayer,
+        title = u"Test layer",
+        description = u'The test layer specifies the setup and teardown .'
+                      u'It is suggested but not obligatory to use dotted names 
for this.',
+        required = False,
         )
 
 # --
@@ -68,10 +107,13 @@
 class IKSSSeleniumTestResource(Interface):
     """An utility that a demo needs to register"""
 
-    # list of IKSSSeleniumTestDir
-    selenium_tests = List(
-        title=u"selenium tests",
-        description=u'The list of selenium test directories contained in this 
plugin',
+    # XXX in the "legacy" way, we had KSSSeleniumTestDirectories in the list.
+    # In the new way, and this is what this schema specifies now, we must have
+    # a test suite specification, which may consists of a directory.
+    # Currently, the interface accepts the legacy specification.
+    selenium_tests = Object(IKSSSeleniumTestSuite,
+        title=u"Selenium tests",
+        description=u'The selenium test cases contained in this plugin',
         required=True,
         )
 

Modified: kukit/kss.demo/branch/1.4-kss-test/kss/demo/registry.py
==============================================================================
--- kukit/kss.demo/branch/1.4-kss-test/kss/demo/registry.py     (original)
+++ kukit/kss.demo/branch/1.4-kss-test/kss/demo/registry.py     Fri Mar 28 
11:03:11 2008
@@ -10,13 +10,14 @@
     IKSSSeleniumTestResource,
     IKSSDemoRegistrationEvent,
     IKSSDemoRegistryEvent,
+    IKSSSeleniumTestSuite
     )
+from resource import KSSSeleniumTestSuite
 from zope.component.interfaces import (
     IUtilityRegistration,
     IRegistered,
     IUnregistered,
     )
-from kss.demo.selenium_utils.builder import getSeleniumTestsFromSuite
 
 # --
 # Registry implementation for use with Zope
@@ -46,8 +47,10 @@
         site.registerHandler(self.registerEarlierDemos)
         # registry for selenium tests
         self.selenium_tests = []
-        site.registerHandler(self.registerSeleniumTestsFromPlugin)
-        site.registerHandler(self.unregisterSeleniumTestsFromPlugin)
+        self.test_suites = []
+        self.all_registered_filenames = []
+        site.registerHandler(self.registerSeleniumTestSuitesFromPlugin)
+        site.registerHandler(self.unregisterSeleniumTestSuitesFromPlugin)
         # ... the rest of setup will be done from registerEarlierDemos
 
     @adapter(IKSSDemoRegistry, IUtilityRegistration, IRegistered, 
IKSSDemoRegistryEvent)
@@ -63,8 +66,9 @@
         for name, plugin in site.getUtilitiesFor(IKSSDemoResource):
             for demo in plugin.demos:
                 self.registerDemo(demo)
-            for test_filename in self._getSeleniumTestsFromPlugin(plugin):
-                self.registerSeleniumTestFile(test_filename)
+            test_suites = 
self._convertLegacySeleniumTestSuitesFromPlugin(plugin)
+            for test_suite in test_suites:
+                self.registerSeleniumTestSuite(test_suite)
 
     @adapter(IKSSDemoResource, IUtilityRegistration, IRegistered, 
IKSSDemoRegistrationEvent)
     def registerDemosFromPlugin(self, plugin, registration=None, event=None, 
new_event=None):
@@ -129,38 +133,54 @@
     # --
 
     @staticmethod
-    def _getSeleniumTestsFromPlugin(plugin):
-        test_filenames = []
-        for selenium_test_suite in plugin.selenium_tests:
-            filenames = getSeleniumTestsFromSuite(plugin,  selenium_test_suite)
-            test_filenames.extend(filenames)
-        return test_filenames
+    def _convertLegacySeleniumTestSuitesFromPlugin(plugin):
+        # XXX This is only needed because in the "legacy" way, we had test
+        # directories. Now we always have test suites. So when we encounter
+        # a directory, we wrap it as a test suite.
+        test_suites = []
+        for test_suite in plugin.selenium_tests:
+            if not IKSSSeleniumTestSuite.providedBy(test_suite):
+                # legacy way, need to wrap it to be a suite
+                test_suite = KSSSeleniumTestSuite(tests=test_suite)
+            test_suites.append(test_suite)
+        return test_suites
 
     @adapter(IKSSSeleniumTestResource, IUtilityRegistration, IRegistered, 
IKSSDemoRegistrationEvent)
-    def registerSeleniumTestsFromPlugin(self, plugin, registration=None, 
event=None, new_event=None):
-        """Add a demo collection to the registry.
+    def registerSeleniumTestSuitesFromPlugin(self, plugin, registration=None, 
event=None, new_event=None):
+        """Add a list of test suites to the registry.
         """
-        test_filenames = self._getSeleniumTestsFromPlugin(plugin)
-        for test_filename in test_filenames:
-            self.registerSeleniumTestFile(test_filename)
+        test_suites = self._convertLegacySeleniumTestSuitesFromPlugin(plugin)
+        for test_suite in test_suites:
+            self.registerSeleniumTestSuite(test_suite)
 
     @adapter(IKSSSeleniumTestResource, IUtilityRegistration, IUnregistered, 
IKSSDemoRegistrationEvent)
-    def unregisterSeleniumTestsFromPlugin(self, plugin, registration=None, 
event=None, new_event=None):
-        """Remove a demo collection from the registry.
+    def unregisterSeleniumTestSuitesFromPlugin(self, plugin, 
registration=None, event=None, new_event=None):
+        """Remove a list of test suites from the registry.
         """
-        for test_filename in self._getSeleniumTestsFromPlugin(plugin):
-            self.registerSeleniumTestFile(test_filename)
-
-    def registerSeleniumTestFile(self, test_filename):
-        """Register a selenium test by absolute filename
-        """
-        if test_filename in self.selenium_tests:
-            raise Exception, 'The selenium test for %s has already been 
registered. Cannot add.' % (test_filename, )
-        self.selenium_tests.append(test_filename)
+        test_suites = self._convertLegacySeleniumTestSuitesFromPlugin(plugin)
+        for test_suite in test_suites:
+            self.unregisterSeleniumTestSuite(test_suite)
+
+    def registerSeleniumTestSuite(self, test_suite):
+        """Register a selenium suite."""
+        # Register the suite in different registries.
+        self.test_suites.append(test_suite)
+        # Check if any of the filenames are registered elsewhere.
+        # This also assures by the way, that suites cannot be duplicated.
+        # The filenames are also used for 
+        test_filenames = test_suite.tests.get_filenames()
+        for test_filename in test_filenames:
+            if test_filename in self.selenium_tests:
+                raise Exception, 'The selenium test file for %s has already 
been registered. Cannot add.' % (test_filename, )
+            self.selenium_tests.append(test_filename)
+            # All registered filenames are used to protect the resource
+            # and prevent a malicious attempt to serve something we should not.
+            self.all_registered_filenames.append(test_filename)
+        # XXX TODO need different other registries for lookup of categories.
 
-    def unregisterSeleniumTestFile(self, test_filename):
-        """Unregister the given selenium test."""
+    def unregisterSeleniumTestSuite(self, test_suite):
+        """Unregister the given selenium suite."""
         try:
-            del self.selenium_tests[test_filename]
+            self.test_suites.remove(test_suite)
         except KeyError:
-            raise Exception, 'The selenium test for %s is yet unregistered. 
Cannot remove.' % (test_filename, )
+            raise Exception, 'The selenium test suite %s for is yet 
unregistered. Cannot remove.' % (test_suite )

Modified: kukit/kss.demo/branch/1.4-kss-test/kss/demo/resource.py
==============================================================================
--- kukit/kss.demo/branch/1.4-kss-test/kss/demo/resource.py     (original)
+++ kukit/kss.demo/branch/1.4-kss-test/kss/demo/resource.py     Fri Mar 28 
11:03:11 2008
@@ -1,8 +1,12 @@
 from zope.interface import implements
 from interfaces import (
     IKSSDemo,
-    IKSSSeleniumTestDirectory,
+    IKSSSeleniumTestCaseCollection,
+    IKSSSeleniumTestSuite,
     )
+import itertools
+import sys, os.path
+from kss.demo.selenium_utils.builder import getRootDirOfModule
 
 # --
 # Reference implementations for the described elements
@@ -24,16 +28,100 @@
     def __getitem__(self, key):
         return getattr(self, key)
 
-class KSSSeleniumTestDirectory(object):
+class KSSSeleniumTestSuite(object):
+    """Represents a single selenium test case,
+    specified by its filename.
+    """
+    implements(IKSSSeleniumTestSuite)
+ 
+    # convenience access for page templates
+    __allow_access_to_unprotected_subobjects__ = 1
+
+    tests = ()
+    component = u''
+    application = u''
+    layer = None
+
+    def __init__(self, tests=None, component=u'', application=u'', layer=None):
+        if tests is not None:
+            self.tests = tests
+        if component:
+            self.component = component
+        if application:
+            self.application = application
+        if layer is not None:
+            self.layer = layer
+
+# --
+# IKSSSeleniumTestCaseCollection implementations
+# --
+
+class KSSSeleniumTestCaseCollectionBase(object):
+    "Abstract base class to help collection implementation"
+
+    def find_test_root_dir(self, plugin=None, test_root_dir=None):
+        # XXX This must be called directly from the __init__ of the class
+        # and it is able to find out the directory where the class
+        # was defined from.
+        #
+        # Zope CA magic used to find where the class is defined from
+        frame = sys._getframe(2)
+        locals = frame.f_locals
+        # Try to make sure we were called from a class def
+        if (locals is frame.f_globals) or ('__module__' not in locals):
+            raise TypeError("classProvides can be used only from a class 
definition.")
+        # store it
+        self.test_root_dir = getRootDirOfModule(locals['__module__'])
+
+    def get_absolute_path(self, filename):
+        "Returns the absolute path of the filename based on the test root 
directory."
+        return os.path.join(self.test_root_dir, filename)
+
+class KSSSeleniumTestCase(KSSSeleniumTestCaseCollectionBase):
+    """Represents a single selenium test case,
+    specified by its filename.
+    """
+    implements(IKSSSeleniumTestCaseCollection)
+
+    def __init__(self, test_filename):
+        self.find_test_root_dir()
+        self.test_filename = test_filename
+
+    def get_filenames(self):
+        test_filename = self.get_absolute_path(self.test_filename)
+        return [test_filename]
+
+class KSSSeleniumTestDirectory(KSSSeleniumTestCaseCollectionBase):
     """Represents a selenium test directory.
+    It contains a set of tests.
     """
-    implements(IKSSSeleniumTestDirectory)
+    implements(IKSSSeleniumTestCaseCollection)
+
     def __init__(self, test_directory):
+        self.find_test_root_dir()
         self.test_directory = test_directory
 
-    # convenience access for page templates
-    __allow_access_to_unprotected_subobjects__ = 1
-    def __getitem__(self, key):
-        return getattr(self, key)
+    def get_filenames(self):
+        test_filenames = []
+        test_root_dir = self.get_absolute_path(self.test_directory)
+        for test_filename in os.listdir(test_root_dir):
+            if test_filename.lower().endswith('.html') or \
+                    test_filename.endswith('.htm'):
+                test_filenames.append((test_filename, 
os.path.join(test_root_dir, test_filename)))
+        # sort the testfilenames
+        test_filenames.sort()
+        # return only the absolute filenames
+        test_filenames = (filename[1] for filename in test_filenames)
+        return test_filenames
+
+class KSSSeleniumTestCaseList(KSSSeleniumTestCaseCollectionBase):
+    """Recursively collects a list of collectiors.
+    """
+    implements(IKSSSeleniumTestCaseCollection)
 
+    def __init__(self, *cases):
+        self.find_test_root_dir()
+        self.cases = cases
 
+    def get_filenames(self):
+        return itertools.chain(*[case.get_filenames() for case in self.cases])

Modified: kukit/kss.demo/branch/1.4-kss-test/kss/demo/selenium_utils/builder.py
==============================================================================
--- kukit/kss.demo/branch/1.4-kss-test/kss/demo/selenium_utils/builder.py       
(original)
+++ kukit/kss.demo/branch/1.4-kss-test/kss/demo/selenium_utils/builder.py       
Fri Mar 28 11:03:11 2008
@@ -16,27 +16,6 @@
 def getRootDirOfInstance(obj):
     return getRootDirOfClass(obj.__class__)
 
-def getSeleniumTestsFromSuite(owner_instance, suite):
-    """Get absolute filenames of all tests from the suite.
-    Suite contains the directory that we need to find.
-    owner_instance is used for the directory finding,
-    the suite's directory is traversed relative from that.
-    """
-    test_filenames = []
-    # The owner instance is only used to traverse the sute directory
-    # relative from it.
-    root_dir = getRootDirOfInstance(owner_instance)
-    tests_root_dir = os.path.join(root_dir, suite.test_directory)
-    for test_filename in os.listdir(tests_root_dir):
-        if test_filename.lower().endswith('.html') or \
-                test_filename.endswith('.htm'):
-            test_filenames.append((test_filename, os.path.join(tests_root_dir, 
test_filename)))
-    # sort the testfilenames
-    test_filenames.sort()
-    # return only the absolute filenames
-    test_filenames = [filename[1] for filename in test_filenames]
-    return test_filenames
-
 template = Template(textwrap.dedent('''\
         from seleniumtestcase import SeleniumTestCase
         import unittest, time
@@ -69,8 +48,6 @@
             arguments.append("self.getVar('%s')"%matched.group('varname'))
     return 'self.%s(%s)' % (command, ', '.join(arguments))
 
-
-
 def cookSeleniumTests(filenames):
     """Cook selenium tests
     """
_______________________________________________
Kukit-checkins mailing list
[email protected]
http://codespeak.net/mailman/listinfo/kukit-checkins

Reply via email to