#!/usr/bin/env python ############################################################################# # # # The Author of this File is: # Prashanth Mohan # # The Modifiers of this File are: # Venkateswaran S # Nagappan # # For more information on the Linux Desktop Testing Project (LDTP) visit # http://ldtp.freedesktop.org # ############################################################################# import socket, os, types, sys, string, re from xml.dom.minidom import * class LdtpExecutionError (Exception): def __init__(self, value): self.value = value def __str__(self): return repr(self.value) class ConnectionLost (Exception): def __init__(self, value): self.value = value def __str__(self): return repr(self.value) try: client = socket.socket (socket.AF_UNIX, socket.SOCK_STREAM) except socket.error,msg: print "ERROR WHILE CREATING SOCKET",msg raise LdtpExecutionError socketpath = os.environ.get ("LDTP_AUTH_SOCK") if socketpath == '': print "Serever Not Running" raise ConnectionLost (0) try: client.connect (socketpath) except socket.error,msg: print "COULD NOT ESTABLISH CONNECTION",msg raise ConnectionLost (0) def socksend (sendmsg): """This Function will send a LDTP packet to the server INPUT: sendmsg --> LDTP Packet in string format OUTPUT: returns the Error Code/appropriate value as string""" try: client.send (sendmsg) xmlcontent = '' data = client.recv (4) print data while True: data = client.recv (512) print data xmlcontent += data # FXIME: Read based on size if string.find (data, '') != -1: break data = unparse (xmlcontent) except socket.error, msg: print "ERROR WHILE SENDING/RECEIVING PACKET " + str (msg) raise ConnectionLost (0) return data def unparse (xml): """Returns the value obtained from the server's return LDTP packet""" try: length=int (xml[:xml.find('\n')]) xml=xml[xml.find('\n')+1:] #removes the length if length != len (xml)+1: # +1 for the removed starting '\n' #enter code here for request for resending xml pass try: dom = parseString(xml) response = dom.getElementsByTagName ('response')[0] status = response.getElementsByTageName ('status')[0] data = response.getElementsByTagName ('data')[0] code = int(status.getAttribute ('code')) msg = status.getAttribute ('msg') length_of_data = int(data.getAttribute ('length')) ret_msg = data.getAttribute ('value') if length_of_data != len (ret_msg): #again code for msg rereuest pass except: log ('Internal Error during Server-Client communication','cause') raise LdtpExecutionError (0) if isError(code): errmsg = 'Error Code No: '+str(codeno) errmsg += '\n' + msg log (errmsg,'cause') raise LdtpExecutionError (0) except: #log ('Unable to get information from Server\'s code packet','cause') raise LdtpExecutionError (0) return ret_msg def isError(codeno): """Determines whether the obtained codeno is an error or not""" #code here for checking error code pass def parsexml (name,*args): """This Function will parse some information into an LDTP packet INPUT: name --> function_name args --> [,window_name[,object_name[,args...]]] OUTPUT: returns a string in the LDTP packet (XML lib2) format""" try: line='' ## should this header be there in the packets? line+='' line=line+'\n' if len(args)>=1: window=args[0] line=line+'\n' if len(args)>=2: object=args[1] line=line+'\n' if len(args)>=3: for index in range (2,len(args)): arg=args[index] line=line+'\n' line+='' line+='' except: print "ERROR WHILE PARSING XML STRING" return 'ERROR' #replace with exception line=str (len (line))+line return line # Calendar View Functions def selectevent (windowname, Calendar_view_name, calendar_event_name): """Selects the row from the table of calendar events based on the calendar event name specified INPUT: selectevent ('', '', '') OUTPUT: Returns 1 on success and 0 on error.""" if not (type (windowname) == type (Calendar_view_name) == type (calendar_event_name) == types.StringType): raise SyntaxError xml=parsexml('selectevent',windowname,Calendar_view_name,calendar_event_name) try: data=socksend(xml) except ConnectionLost: raise ConnectionLost (0) return data def selecteventindex (windowname, component_name, event_number): """Select an event from a calendar table using its index. Index for a calendar event starts from 1. INPUT: selecteventindex ('', '', ) OUTPUT: Returns 1 on success and 0 on error.""" if not ((type (windowname) == type (component_name) == types.StringType) and (type (event_number) == types.IntType)): raise SyntaxError xml=parsexml('selecteventindex',windowname, component_name, event_number) try: data=socksend(xml) except ConnectionLost: raise ConnectionLost (0) return data def verifyeventexist (windowname, component_name): """verifies whether any events are present in a calendar table INPUT: verifyeventexist ('', '') OUTPUT: Returns 1 on success and 0 on no events.""" if not (type (windowname) == type (component_name) == types.StringType): raise SyntaxError xml=parsexml('verifyeventexist',windowname, component_name) try: data=socksend(xml) except ConnectionLost: raise ConnectionLost (0) return data # Checkbox Functions def check (windowname, component_name): """Check (tick) the check box state. INPUT: check ('', '') OUTPUT: Returns 1 if state is checked, else 0.""" if not (type (windowname) == type (component_name) == types.StringType): raise SyntaxError xml=parsexml('check',windowname, component_name) try: data=socksend(xml) except ConnectionLost: raise ConnectionLost (0) return data def uncheck (windowname, component_name): """Uncheck (un-tick) the check state. INPUT: uncheck ('', '') OUTPUT: Returns 1 if state is unchecked, else 0.""" if not (type (windowname) == type (component_name) == types.StringType): raise SyntaxError xml=parsexml('uncheck',windowname, component_name) try: data=socksend(xml) except ConnectionLost: raise ConnectionLost (0) return data def click (windowname, component_name): """click on radio button / check box / push button/ combo box/ radio menu item/ toggle button/ radio button. INPUT: click ('', '') OUTPUT: Clicks the component_name.""" if not (type (windowname) == type (component_name) == types.StringType): raise SyntaxError xml=parsexml('click',windowname, component_name) try: data=socksend(xml) except ConnectionLost: raise ConnectionLost (0) return data def verifycheck (windowname, component_name): """Checks the state of check box. INPUT: verifycheck ('', '') OUTPUT: If check box state is checked, then returns 1, else 0.""" if not (type (windowname) == type (component_name) == types.StringType): raise SyntaxError xml=parsexml('verifycheck',windowname, component_name) try: data=socksend(xml) except ConnectionLost: raise ConnectionLost (0) return data def verifyuncheck (windowname, component_name): """Checks the state of check box. INPUT: verifyuncheck ('', '') OUTPUT: If check box state is un-checked, then returns 1, else 0.""" if not (type (windowname) == type (component_name) == types.StringType): raise SyntaxError xml=parsexml('verifyuncheck',windowname, component_name) try: data=socksend(xml) except ConnectionLost: raise ConnectionLost (0) return data # CheckMenuItem Functions def selectmenuitem (windowname, menu_hierarchy): """Selects the menu item specified. INPUT: selectmenuitem ('', '') OUTPUT: TODO.""" if not (type (windowname) == type (menu_hierarchy) == types.StringType): raise SyntaxError xml=parsexml('selectmenuitem',windowname, menu_hierarchy) try: data=socksend(xml) except ConnectionLost: raise ConnectionLost (0) return data #TODO #def verifymenucheck (windowname, menu_hierarchy): # """ # INPUT: # # OUTPUT: """ # if not (type (windowname) == type (menu_hierarchy) == types.StringType): # raise SyntaxError # # xml=parsexml('verifymenucheck',windowname, menu_hierarchy) # # try: # data=socksend(xml) # except ConnectionLost: # raise ConnectionLost (0) # # return data # Combobox Functions #TODO #def capturetofile (): # """ # INPUT: # # OUTPUT: """ # if not (type () == type () == types.StringType): # raise SyntaxError # # xml=parsexml('capturetofile',,) # # try: # data=socksend(xml) # except ConnectionLost: # raise ConnectionLost (0) # # return data def hidelist (dlgName, cmbName): """ Hides combo box drop down list in the current dialog. Suppose in previous operation one testcase has clicked on combo box, its drop down list will be displayed. If further no any operation has been done on that combo box then to close that drop down list 'HideList' action is required. INPUT: hidelist ('dlgName', 'cmbName') OUTPUT: TODO""" if not (type (dlgName) == type (cmbName) == types.StringType): raise SyntaxError xml=parsexml('hidelist',dlgName,cmbName) try: data=socksend(xml) except ConnectionLost: raise ConnectionLost (0) return data def select (dlgName, cmbName, Select_argument): """ Select action will select an value given in argument from the combo box. INPUT: select ('dlgName', 'cmbName', '