Login | Register
My pages Projects Community openCollabNet
[ Installation ] [ Recording and Running ] [ Editing ] Recipes [ Command Line ]

Recipes

Here are some ways you can enhance the scripts generated by MaxQ.

Adding SQL to Tests

Most web applications use a SQL database. MaxQ provides a utility class, DBUtil to simplify adding SQL to your tests.

To use this class:

  • Place a copy of the JAR file for your JDBC database driver is in MaxQ's lib directory and call it db-connector.jar. If using a UNIX you can use a symbolic link instead. e.g.
    $ ln -s $HOME/lib/mysql-connector-java-3.0.12-production-bin.jar maxq/lib/db-connector.jar
  • Add: from com.bitmechanic.maxq import DBUtil to the top of your test script
  • Call: db = DBUtil("driver name", "url", "username", "password") to open a connection to the database
  • Call any of the methods in the class (e.g. execute)
  • Call: db.close() at the end of your script to close the database connection.
Consider this example:
    # imports
    from com.bitmechanic.maxq import HttpTestCase
    from com.bitmechanic.maxq import DBUtil
    from junit.textui import TestRunner
    from java.util import HashMap
    
    # definition of test class
    class MaxQTest(HttpTestCase):
        def __init__(self):
            HttpTestCase.__init__(self, "")
    
        def runTest(self):
            # open connection to database
            db = DBUtil("org.gjt.mm.mysql.Driver", 
                 "jdbc:mysql://crankshaft.bitmechanic.com:3306/mydb", "myuser", "mypw")
            self.get("http://www.bitmechanic.com/")
            self.assertEquals(200, self.getResponse().getStatusCode())
            val = db.loadVal("select max(id) from contact")
            self.assertTrue(val > 0)
    
            # insert a row
            rows = db.execute("insert into sku (name) values ('test333')")
            self.assertEquals(rows, 1)
    
            # delete a row
            rows = db.execute("delete from sku where name = 'test333'")
            self.assertEquals(rows, 1)
    
            # close connection
            db.close()
    
    ##########################################
    
    # Code to load and run the test
    test = MaxQTest()
    test.runTest()
    


Writing Library Scripts

If your site has repetitive functionality, or functionality that requires the user to be in a certain state (e.g. logged into the site), then your recorded scripts will likely contain duplicate requests. In order to improve the maintainability of the tests, you probably want to refactor those routines into test libraries.

Consider this script that logs into a bug database:

     1: # imports
     2: from com.bitmechanic.maxq import HttpTestCase
     3: from junit.textui import TestRunner
     4: from java.util import HashMap
     5: import mantis
     6: 
     7: # defintition of test class
     8: class GetTest(HttpTestCase):
     9:     def __init__(self):
    10:         HttpTestCase.__init__(self, "")
    11: 
    12:     def testHome(self):
    13:         print "Testing mantis lib"
    14:         mantis.login(self)
    15:         self.get("http://www.bitmechanic.com/mantis/main_page.php")
    16:         self.assertTrue(self.responseContains("Logged in"))
    17:         mantis.logout(self)
    18: 
    19: ###########################################################################
    20: 
    21: # Code to load and run the test
    22: test = GetTest()
    23: test.testHome()
    

Note line 5: import mantis. That tells Jython to look for a file named mantis.py. Use the --path flag to MaxQ to specify a search path for this file.

On line 14 we invoke a method in the mantis library file. The syntax is [package name].[method name]. We pass in self so the library file can make HTTP requests within our same HTTP session (and consequently have access to any cookies we've stored).

Here's the source to mantis.py

     1: from java.util import HashMap
     2: 
     3: #
     4: # Login to mantis
     5: #
     6: def login(test):
     7:     print "Logging in user"
     8:     map = HashMap()
     9:     map.put("f_username", "scott")
    10:     map.put("f_password", "notmypw")
    11:     test.post("http://www.bitmechanic.com/mantis/login.php", map)
    12:     test.assertTrue(test.responseContains("Click here to proceed"))
    13: 
    14: #
    15: # Logout of mantis
    16: #
    17: def logout(test):
    18:     print "Logging out user"
    19:     test.get("http://www.bitmechanic.com/mantis/logout_page.php")
    20:     test.assertTrue(test.responseContains("Logged Out.."))
    

Using the Full Jython Libraries

MaxQ distributions include jython.jar, but miss many of the standard Jython libraries (such as string and re). To get access to these libraries you must install a Jython distribution and tell MaxQ to include its Lib directory in its python search path. There are two ways to do this:

  • Run MaxQ with the --path argument. e.g.
    maxq --path $HOME/jython-2.1/Lib
  • Put the path to the Jython libraries in conf/maxq.properties:
    python.path=/Users/oliver/snake/test/jython-2.1/Lib