Commits

Sylvain Hellegouarch committed 912470f

robotframework lib example for CherryPy functional testing

Comments (0)

Files changed (5)

testing/functional/robotframework_/CherryPyLib.py

+# -*- coding: utf-8 -*-
+import imp
+import os, os.path
+ 
+import cherrypy
+
+__all__ = ['CherryPyLib']
+ 
+class CherryPyLib(object):
+    def setup_cherrypy(self, conf_file=None):
+        """ Configure the CherryPy engine and server using
+        the built-in ``'embedded'`` environment mode.
+ 
+        If provided, ``conf_file`` is a path to a CherryPy
+        configuration file used in addition.
+
+        Example:
+        | Setup CherryPy |
+        | Setup CherryPy | /path/to/server.conf |
+        """
+        cherrypy.config.update({"environment": "embedded"})
+        if conf_file:
+            cherrypy.config.update(conf_file)            
+ 
+    def start_cherrypy(self):
+        """ Start a CherryPy engine.
+
+        Example:
+        | Start CherryPy |
+        """
+        cherrypy.engine.start()
+ 
+    def exit_cherrypy(self):
+        """ Terminate a CherryPy engine.
+        
+        Example:
+        | Exit CherryPy |
+        """
+        cherrypy.engine.exit()
+ 
+    def mount_application(self, appmod, appcls, path='/', config=None, directory=None):
+        """ Mount an application to be tested. ``appmod`` is 
+        a Python module containing ``appcls``. The module is
+        imported from the given directory. If not provided, we use
+        the current directory instead.
+
+        You may provide a ``config`` to the application either as
+        a dictionary of a path to the config file.
+
+        Returns an instance of ``cherypy.Application`` and
+        raises ``ImportError`` if it failed importing it.
+        
+        Example:
+        | ${app} | Mount Application | myapp | Root | / | /path/to/app.conf |
+        """
+        directory = directory or os.getcwd()
+        file, filename, description = imp.find_module(appmod, [directory])
+        mod = imp.load_module(appmod, file, filename, description)
+        if hasattr(mod, appcls):
+            cls = getattr(mod, appcls)
+            app = cls()
+            cherrypy.tree.mount(app, path, config)
+        else:
+            raise ImportError("cannot import name %s from %s" % (appcls, appmod))
+
+        return app

testing/functional/robotframework_/README

+Simple library for the Robot Framework automation tool to perform functional tests against a CherryPy server.
+
+It requires Robot Framework 2.6+ and, for this example, Robot Framework Selenium. Note that the 
+lib itself doesn't require Selenium, only the short example presented in this recipe.
+
+http://code.google.com/p/robotframework/
+http://code.google.com/p/robotframework-seleniumlibrary/
+
+Run the test as follow:
+
+$ pybot --pythonpath . myapp.txt
+
+This will use Selenium to load your default browser and run the test from within it against
+the CherryPy application. The application and server will be started and stopped by the
+test itself.

testing/functional/robotframework_/myapp.py

+# -*- coding: utf-8 -*-
+import cherrypy
+ 
+__all__ = ['Echo']
+ 
+class Echo(object):
+    @cherrypy.expose
+    def index(self):
+        return """<html>
+<head><title>Robot Framework Test for CherryPy</title></head>
+<body>
+<form method="post" action="/echo">
+<input type="text" name="message" />
+<input type="submit" />
+</form>
+</body>
+</html>"""
+ 
+    @cherrypy.expose
+    def echo(self, message):
+        return message
+    

testing/functional/robotframework_/myapp.txt

+*** Settings ***
+Library  SeleniumLibrary
+Library  CherryPyLib
+Suite Setup  Start Dependencies
+Suite Teardown  Shutdown Dependencies
+Test Setup  Mount Application  myapp  Echo
+
+*** Variables ***
+${MSG}  Hello World
+${HOST}  http://localhost:8090/
+
+*** Test Cases ***
+Echo ${MSG}
+     Open Browser  ${HOST}
+     Input text  message  ${MSG}
+     Submit form
+     Page Should Contain  ${MSG}
+     Close All Browsers
+
+*** Keywords ***
+Start Dependencies
+    Setup CherryPy  server.cfg
+    Start CherryPy
+    Start Selenium Server
+    Sleep  3s
+
+Shutdown Dependencies
+    Stop Selenium Server
+    Exit CherryPy

testing/functional/robotframework_/server.cfg

+[global]
+server.socket_host: "0.0.0.0"
+server.socket_port: 8090