1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
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
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
78 if self.doc: self.doc.unlink()
79
80
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
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
137 f = open(self.xmlfile, 'w')
138 f.write(self.doc.toxml())
139 f.close()
140