r2 - 31 Oct 2006 - 11:21:34 - AdamChristianYou are here: OSAF >  Projects Web  >  QualityAssuranceTeam > CosmoUIQualityAssurace > WritingCosmoUIAutomatedTests

Writing Cosmo UI Automated Tests

There are many pieces involved in writing automated tests for Cosmo UI, the following will outline one piece at a time what is required and how you can tailor them for your specific test.

To access and run these tests you need to get the tools repository:
svn co http://svn.osafoundation.org/svn/tools tools

The tests are located in the trunk/scripts directory

Part 1: Importing required dependencies

JSONTest allows us to use JSON-RPC specific functionality, and operator is used to access the mod function for randomly generating days and times for the events.

 from JSONTest import JSONTest
 import operator
Part 2: Setting up the class and startRun function

The class name is specific to your test, but if its for scooby it needs to inherit from the JSONTest class as it requires some of its functionality.
startRun is the function that is automatically called in fullRun() and is used for all of the logging, debug and masking.

class scoobyMultiAddRemEventsTest(JSONTest):
    
    def startRun(self):
Part 3: Setting up the Cosmo user

This is the user we will be using for this test, it needs to be added to Cosmo before we can use it in Scooby. The username should be set to be specific to this test, as well as the path in pathBuilder.

 # ------- Test Create Account ------- # 
        self.testStart('Setup Accounts')
        
        try:
            self.appendUser = self.appendDict['username']
        except KeyError:
            self.appendUser = ''
        
        self.username = "scoobyMultiAddRemEvent"
        self.password = "scooby"
        
        cmpheaders = self.headerAdd({'Content-Type' : "text/xml; charset=UTF-8"})
        cmpheaders = self.headerAddAuth("root", "cosmo", headers=cmpheaders)
           
        #CMP path
        #cmppath = self.pathBuilder('/cmp/user/%s' % (self.username))
        cmppath = '/cosmo/cmp/user/%s' % (self.username)
        
        #Create testing account        
        bodycreateaccount = '<?xml version="1.0" encoding="utf-8" ?>                                   
        <user xmlns="http://osafoundation.org/cosmo/CMP">                                   
        <username>%s</username>                                   
        <password>%s</password>                                   
        <firstName>%s</firstName>                                   
        <lastName>Test</lastName>                                   
        <email>%s@osafoundation.org</email>                                   
        </user>' % (self.username, self.password, self.username, self.username)
                                 
        #Create account and check status
        self.request('PUT', cmppath, body=bodycreateaccount, headers=cmpheaders)
        self.checkStatus(201)
Part 4: Setup the Scooby path and headers

        self.path = '/scooby'
        #Here we start interacting with scooby
        self.my_headers = {'Accept': '*/*', 'Referrer' : 'http://%s:%s/' % (self.connection['host'], self.connection['port'])}
        #We need to acquire the Jsession ID that is provided in the SET-COOKIE header of the response
        #elf.request(method='GET',url=self.pathBuilder('/'), headers = {'Accept': '*/*', 'Referrer' : 'http://%s:%s/' % (host, port)})
        self.request(method='GET',url=self.pathBuilder('/'), headers = self.my_headers)  
        self.parseJsid() #Parse and store JSID
        self.my_headers = self.headerAdd({'Cookie' : 'JSESSIONID=%s; username=%s' % (self.parseJsid(), self.username)})
        self.verifyListInResponse(negative=['error'])
Part 5: Authenticate to Scooby with the user we have setup

        #Authenticate
        self.my_headers.update({'Accept': '*/*', 'Content-type' : 'application/x-www-form-urlencoded'})
        self.request(method='POST', url=self.pathBuilder('/j_acegi_security_check'), body='j_username=%s&j_password=%s' % (self.username, self.password), headers = self.my_headers)
        self.verifyListInResponse(negative=['error'])
Part 6: Create a calendar to be used for this test, this test uses a calendar named Scooby but this can be customized if you wish.

       #JSON-RPC: Create a calendar (for scooby it will be scooby, and will fail if scooby already exists)
        self.my_headers.update({'Accept': '*/*', 'Content-type' : 'text/plain'})
        self.request(method='POST', url=self.pathBuilder('/JSON-RPC'), body='{"id": 5, "method": "scoobyService.createCalendar", "params": ["Scooby", "Scooby"]}', headers = self.my_headers)
        self.verifyListInResponse(negative=['error'])
Part 7: Writing your scooby test

If you only need your test to run its actions once put them at the bottom of the startRun function, however most tests require that they are run many times and can use recurrence via the command line so we put these tests in the recurringRun() function. This is automatically called as many times as recurrence is specified and uses built in functionality inherited from testObject to automatically keep track of failures etc.

 def recurringRun(self):
Part 8: Your actual test code

The following is an example of a test that creates and event and then removes it by directly making JSON-RPC calls, as explained above this is called as many times as you tell it via recurrence so this acts more as a performance and stress test. There are many more methods that can be used to manipulate scooby and they are documented below.

        #JSON-RPC: Create an event
        self.request(method='POST', url=self.pathBuilder('/JSON-RPC'), body='{"id": %s, "method": "scoobyService.saveEvent", "params": ["Scooby", {"id": null, "title": "%s", "description": "%s", "start": {"year": 2006, "month": %s, "date": 31, "hours": "9", "minutes": "00", "seconds": 0, "timezone": null, "utc": false}, "end": {"year": 2006, "month": %s, "date": 31, "hours": 10, "minutes": 0, "seconds": 0, "timezone": null, "utc": false}, "allDay": false, "pointInTime": false, "anyTime": false, "recurrenceRule": null, "status": null, "masterEvent": false, "instance": false, "javaClass": "org.osaf.scooby.model.Event"}]}' % (self.currentRecurrence, operator.mod(int(self.currentRecurrence),12), operator.mod(int(self.currentRecurrence),12), operator.mod(int(self.currentRecurrence),12), operator.mod(int(self.currentRecurrence),12)), headers = self.my_headers)
        self.verifyListInResponse(negative=['error'])
       
        #JSON-RPC: Remove the last event created
        self.request(method='POST', url=self.pathBuilder('/JSON-RPC'), body='{"id": %s, "method": "scoobyService.removeEvent", "params": ["Scooby", "%s"]}' % (self.currentRecurrence, self.getJsonAttrib("result")), headers = self.my_headers)
        self.verifyListInResponse(negative=['error'])
Part 9: scoobService methods, each of these methods have a specific set of parameters than call also be tailored to your needs.

Each of the calls to self.verifyListInResponse(negative=['error']) checks the response for the word error and if it exists this call is documented as a failed, which in turn keeps the tests from passing.

        #JSON-RPC: Get the available system methods
        self.my_headers.update({'Accept': '*/*', 'Content-type' : 'text/plain'})
        self.request(method='POST', url=self.pathBuilder('/JSON-RPC'), body='{"id": 1, "method": "system.listMethods", "params": []}', headers = self.my_headers)
        self.verifyListInResponse(negative=['error'])
         
        #JSON-RPC: Call to getVersion
        self.request(method='POST', url=self.pathBuilder('/JSON-RPC'), body='{"id": 2, "method": "scoobyService.getVersion", "params": []}', headers = self.my_headers)
        self.verifyListInResponse(negative=['error'])
        
        #JSON-RPC: Call to getEvents
        self.request(method='POST', url=self.pathBuilder('/JSON-RPC'), body='{"id": 3, "method": "scoobyService.getEvents", "params": ["Scooby", 1148713200000, 1149490799000]}', headers = self.my_headers)
        self.verifyListInResponse(negative=['error'])        
        
        #JSON-RPC: Get the calendars
        self.request(method='POST', url=self.pathBuilder('/JSON-RPC'), body='{"id": 4, "method": "scoobyService.getCalendars", "params": []}', headers = self.my_headers)
        self.verifyListInResponse(negative=['error'])
        
        #JSON-RPC: Create a calendar (for scooby it will be scooby, and will fail if scooby already exists)
        self.request(method='POST', url=self.pathBuilder('/JSON-RPC'), body='{"id": 5, "method": "scoobyService.createCalendar", "params": ["Scooby", "Scooby"]}', headers = self.my_headers)
        self.verifyListInResponse(negative=['error'])
        
        #JSON-RPC: Create an event
        self.request(method='POST', url=self.pathBuilder('/JSON-RPC'), body='{"id": 6, "method": "scoobyService.saveEvent", "params": ["Scooby", {"id": null, "title": "Welcome to Scooby!", "description": "Welcome to Scooby!", "start": {"year": 2006, "month": 4, "date": 31, "hours": "9", "minutes": "00", "seconds": 0, "timezone": null, "utc": false}, "end": {"year": 2006, "month": 4, "date": 31, "hours": 10, "minutes": 0, "seconds": 0, "timezone": null, "utc": false}, "allDay": false, "pointInTime": false, "anyTime": false, "recurrenceRule": null, "status": null, "masterEvent": false, "instance": false, "javaClass": "org.osaf.scooby.model.Event"}]}', headers = my_headers)
        self.verifyListInResponse(negative=['error'])

        #JSON-RPC: Remove the last event created
        self.request(method='POST', url=self.pathBuilder('/JSON-RPC'), body='{"id": %s, "method": "scoobyService.removeEvent", "params": ["Scooby", "%s"]}' % (self.currentRecurrence, self.getJsonAttrib("result")), headers = self.my_headers)
        self.verifyListInResponse(negative=['error'])
Edit | WYSIWYG | Attach | Printable | Raw View | Backlinks: Web, All Webs | History: r2 < r1 | More topic actions
 
Open Source Applications Foundation
Except where otherwise noted, this site and its content are licensed by OSAF under an Creative Commons License, Attribution Only 3.0.
See list of page contributors for attributions.