Package pysys :: Package xml :: Module manual
[hide private]
[frames] | no frames]

Source Code for Module pysys.xml.manual

  1  #!/usr/bin/env python 
  2  # PySys System Test Framework, Copyright (C) 2006-2016  M.B.Grieve 
  3   
  4  # This library is free software; you can redistribute it and/or 
  5  # modify it under the terms of the GNU Lesser General Public 
  6  # License as published by the Free Software Foundation; either 
  7  # version 2.1 of the License, or (at your option) any later version. 
  8   
  9  # This library is distributed in the hope that it will be useful, 
 10  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 12  # Lesser General Public License for more details. 
 13   
 14  # You should have received a copy of the GNU Lesser General Public 
 15  # License along with this library; if not, write to the Free Software 
 16  # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 
 17   
 18  # Contact: moraygrieve@users.sourceforge.net 
 19   
 20  import os, os.path, sys, logging, xml.dom.minidom 
 21   
 22  from pysys.constants import * 
 23   
 24  log = logging.getLogger('pysys.xml.manual') 
 25   
 26  DTD=''' 
 27  <!ELEMENT pysysmanualtest (step)+ > 
 28  <!ELEMENT step (description, expectedresult?) > 
 29  <!ELEMENT description (#PCDATA) > 
 30  <!ELEMENT expectedresult (#PCDATA) > 
 31  <!ATTLIST step title CDATA #REQUIRED 
 32                 validate (true | false) "true"> 
 33  ''' 
 34   
 35   
36 -class XMLManualTestStep:
37 - def __init__(self, number, title, validate, wrap, description, expectedResult):
38 self.number = number 39 self.title = title 40 self.validate = validate 41 self.wrap = wrap 42 self.description = description 43 self.expectedResult = expectedResult
44 45
46 - def toString(self):
47 print "Step number: %d" % self.number 48 print "Step title: %s" % self.title 49 print "Step validate: %s" % self.validate 50 print "Step wrap: %s" % self.wrap 51 print "Step description: ", 52 desc = self.description.split('\n') 53 for index in range(0, len(desc)): 54 if index == 0: print desc[index] 55 if index != 0: print " %s" % desc[index] 56 print "Expected result: %s" % self.expectedResult
57
58 -class XMLManualTestParser:
59 - def __init__(self, xmlfile):
60 self.dirname = os.path.dirname(xmlfile) 61 self.xmlfile = xmlfile 62 63 if not os.path.exists(xmlfile): 64 raise Exception, "Unable to find supplied manual test input file \"%s\"" % xmlfile 65 66 try: 67 self.doc = xml.dom.minidom.parse(xmlfile) 68 except: 69 raise Exception, "%s " % (sys.exc_info()[1]) 70 else: 71 if self.doc.getElementsByTagName('pysysmanualtest') == []: 72 raise Exception, "No <pysysmanualtest> element supplied in XML descriptor" 73 else: 74 self.root = self.doc.getElementsByTagName('pysysmanualtest')[0]
75 76 79 80
81 - def getSteps(self):
82 stepsNodeList = self.root.getElementsByTagName('step') 83 if stepsNodeList == []: 84 raise Exception, "No <step> element supplied in XML manual test input file" 85 86 steps = [] 87 stepnumber = 0 88 for stepsNode in stepsNodeList: 89 title = stepsNode.getAttribute("title") 90 validate = stepsNode.getAttribute("validate") 91 wrap = stepsNode.getAttribute("wrap") 92 93 if stepsNode.getElementsByTagName('description') == []: 94 raise Exception, "No <description> child element of <step> supplied in XML manual test input file" 95 else: 96 try: 97 description = stepsNode.getElementsByTagName('description')[0].childNodes[0].data 98 except: 99 description = "" 100 try: 101 expectedResult = stepsNode.getElementsByTagName('expectedresult')[0].childNodes[0].data 102 except: 103 expectedResult = "" 104 stepnumber = stepnumber + 1 105 steps.append(XMLManualTestStep(stepnumber, title, validate, wrap, description, expectedResult)) 106 return steps
107
108 - def putSteps(self, steps):
109 stepsNodeList = self.root.getElementsByTagName('step') 110 for step in stepsNodeList: 111 self.root.removeChild(step) 112 step.unlink() 113 114 stepsNodeList = [] 115 116 for step in range(len(steps)): 117 newStep = self.doc.createElement('step') 118 newDesc = self.doc.createElement('description') 119 newDesc.appendChild(self.doc.createCDATASection("")) 120 newStep.appendChild(newDesc) 121 newExp = self.doc.createElement('expectedresult') 122 newExp.appendChild(self.doc.createCDATASection("")) 123 newStep.appendChild(newExp) 124 self.root.appendChild(newStep) 125 stepsNodeList.append(newStep) 126 127 if steps[step].expectedResult == "\n": steps[step].expectedResult = "" 128 129 stepsNode = stepsNodeList[step] 130 stepsNode.setAttribute("title", steps[step].title) 131 stepsNode.setAttribute("validate", steps[step].validate) 132 stepsNode.setAttribute("wrap", steps[step].validate) 133 stepsNode.getElementsByTagName('description')[0].childNodes[0].data = steps[step].description 134 stepsNode.getElementsByTagName('expectedresult')[0].childNodes[0].data = steps[step].expectedResult
135
136 - def writeXml(self):
137 f = open(self.xmlfile, 'w') 138 f.write(self.doc.toxml()) 139 f.close()
140