2016-05-10 1 views
0

Пытается разобрать xml, сгенерированный squish, и создать реляционные данные в формате tsv для загрузки в базу данных. Xml выглядитXml синтаксический разбор с функциями python и создание реляционных данных с разделителями табуляции

<SquishReport version="2.2" xmlns="http://www.froglogic.com/XML2"> 
<test name="ATF_Sandbox"> 
    <prolog time="2016-05-05T06:43:43+05:30"/> 
    <test name="tst_MapView"> 
     <prolog time="2016-05-05T06:43:43+05:30"/> 
     <message line="30" type="LOG" file="D:\TestSlave5\workspace\TestSlave5 SyncCode\SquishTests\ATF_Sandbox\tst_MapView\test.py" time="2016-05-05T06:44:41+05:30"> 
      <description><![CDATA[CS.VPR.MAP.VSL.MPV.001]]></description> 
     </message> 
     <verification line="759" type="" name="" file="D:\TestSlave5\workspace\TestSlave5 SyncCode\SquishTests\suite_common\squishFunctions.py"> 
      <result type="PASS" time="2016-05-05T06:44:51+05:30"> 
       <description>Check: Current map mode is 'Overview' mode</description> 
       <description type="DETAILED"></description> 
      </result> 
     </verification> 
     <verification line="2264" type="" name="" file="D:\TestSlave5\workspace\TestSlave5 SyncCode\SquishTests\suite_common\squishFunctions.py"> 
      <result type="PASS" time="2016-05-05T06:44:52+05:30"> 
       <description>Teststep: CS.VPR.MAP.VSL.MPV.001 (Screenshot in "D:/TestSlave5/workspace/TestSlave5 SyncCode/SquishTests/ATF_Sandbox/tst_MapView\failedImages\failed_46.png")</description> 
       <description type="DETAILED">Screen shot captured: #REMARK MANUAL VERIFICATION REQUIRED STEP: &lt;ccp is pointing north up></description> 
       <description type="file">D:/TestSlave5/workspace/TestSlave5 SyncCode/SquishTests/ATF_Sandbox/tst_MapView\failedImages\failed_46.png</description> 
      </result> 
     </verification> 
     <message line="48" type="LOG" file="D:\TestSlave5\workspace\TestSlave5 SyncCode\SquishTests\ATF_Sandbox\tst_MapView\test.py" time="2016-05-05T06:44:52+05:30"> 
      <description><![CDATA[CS.VPR.MAP.VSL.MPV.002]]></description> 
     </message> 
     <verification line="2264" type="" name="" file="D:\TestSlave5\workspace\TestSlave5 SyncCode\SquishTests\suite_common\squishFunctions.py"> 
      <result type="PASS" time="2016-05-05T06:44:52+05:30"> 
       <description>Teststep: CS.VPR.MAP.VSL.MPV.002 (Screenshot in "D:/TestSlave5/workspace/TestSlave5 SyncCode/SquishTests/ATF_Sandbox/tst_MapView\failedImages\failed_47.png")</description> 
       <description type="DETAILED">Screen shot captured: #REMARK WORKAROUND FOR : &lt;resolution of the screen is 400x240> &lt;Verifying width and height of the HomeScreen></description> 
       <description type="file">D:/TestSlave5/workspace/TestSlave5 SyncCode/SquishTests/ATF_Sandbox/tst_MapView\failedImages\failed_47.png</description> 
      </result> 
     </verification> 
     <verification line="61" type="" name="" file="D:\TestSlave5\workspace\TestSlave5 SyncCode\SquishTests\ATF_Sandbox\tst_MapView\test.py"> 
      <result type="PASS" time="2016-05-05T06:44:52+05:30"> 
       <description>Comparison</description> 
       <description type="DETAILED">'400' and '400' are equal</description> 
      </result> 
     </verification> 
     <message line="71" type="LOG" file="D:\TestSlave5\workspace\TestSlave5 SyncCode\SquishTests\ATF_Sandbox\tst_MapView\test.py" time="2016-05-05T06:44:52+05:30"> 
      <description><![CDATA[CS.VPR.MAP.VSL.MPV.003]]></description> 
     </message> 
     <verification line="2264" type="" name="" file="D:\TestSlave5\workspace\TestSlave5 SyncCode\SquishTests\suite_common\squishFunctions.py"> 
      <result type="PASS" time="2016-05-05T06:44:53+05:30"> 
       <description>Teststep: CS.VPR.MAP.VSL.MPV.003 (Screenshot in "D:/TestSlave5/workspace/TestSlave5 SyncCode/SquishTests/ATF_Sandbox/tst_MapView\failedImages\failed_48.png")</description> 
       <description type="DETAILED">Screen shot captured: #REMARK MANUAL VERIFICATION REQUIRED STEP: &lt;Map is centered at center of map.></description> 
       <description type="file">D:/TestSlave5/workspace/TestSlave5 SyncCode/SquishTests/ATF_Sandbox/tst_MapView\failedImages\failed_48.png</description> 
      </result> 
     </verification> 
     <message line="356" type="LOG" file="D:\TestSlave5\workspace\TestSlave5 SyncCode\SquishTests\ATF_Sandbox\tst_MapView\test.py" time="2016-05-05T06:48:22+05:30"> 
      <description><![CDATA[CS.VPR.MAP.VSL.RDM.003]]></description> 
     </message> 
     <message line="365" type="LOG" file="D:\TestSlave5\workspace\TestSlave5 SyncCode\SquishTests\ATF_Sandbox\tst_MapView\test.py" time="2016-05-05T06:48:22+05:30"> 
      <description><![CDATA[#REMARK MANUAL VERIFICATION REQUIRED STEP: <Route preview is played at 300% of normal road speed>]]></description> 
     </message> 
     <message line="370" type="LOG" file="D:\TestSlave5\workspace\TestSlave5 SyncCode\SquishTests\ATF_Sandbox\tst_MapView\test.py" time="2016-05-05T06:48:22+05:30"> 
      <description><![CDATA[CS.VPR.MAP.VSL.RDM.004]]></description> 
     </message> 
     <message line="379" type="LOG" file="D:\TestSlave5\workspace\TestSlave5 SyncCode\SquishTests\ATF_Sandbox\tst_MapView\test.py" time="2016-05-05T06:48:22+05:30"> 
      <description><![CDATA[#REMARK MANUAL VERIFICATION REQUIRED STEP: <Route Preview starts playing from the beginning and keeps looping until the Route Preview is stopped>]]></description> 
     </message> 
     <message line="384" type="LOG" file="D:\TestSlave5\workspace\TestSlave5 SyncCode\SquishTests\ATF_Sandbox\tst_MapView\test.py" time="2016-05-05T06:48:22+05:30"> 
      <description><![CDATA[CS.VPR.MAP.VSL.RDM.005]]></description> 
     </message> 
     <verification line="102" type="" name="" file="D:\TestSlave5\workspace\TestSlave5 SyncCode\SquishTests\suite_common\squishFunctions.py"> 
      <result type="PASS" time="2016-05-05T06:48:22+05:30"> 
       <description>Comparison</description> 
       <description type="DETAILED">'True' and 'True' are equal</description> 
       <description type="DETAILED">Check if existence of widget ':HomeView.btnToggleMap_CMapTogglePushButton' equals True</description> 
      </result> 
     </verification> 
     <verification line="102" type="" name="" file="D:\TestSlave5\workspace\TestSlave5 SyncCode\SquishTests\suite_common\squishFunctions.py"> 
      <result type="PASS" time="2016-05-05T06:48:22+05:30"> 
       <description>Comparison</description> 
       <description type="DETAILED">'True' and 'True' are equal</description> 
       <description type="DETAILED">Check if existence of widget ':HomeView.btnZoomIn_QPushButton' equals True</description> 
      </result> 
     </verification> 

Я пытаюсь достигнуть данных в реляционный формат, который выглядит как -

CS.VPR.MAP.VSL.MPV.001 PASS Check: Current map mode is 'Overview' mode None None 
CS.VPR.MAP.VSL.MPV.001 PASS Teststep: CS.VPR.MAP.VSL.MPV.001 (Screenshot in "D:/TestSlave5/workspace/TestSlave5 SyncCode/SquishTests/ATF_Sandbox/tst_MapView\failedImages\failed_46.png") Screen shot captured: #REMARK MANUAL VERIFICATION REQUIRED STEP: <ccp is pointing north up>  MANUAL 
CS.VPR.MAP.VSL.MPV.002 PASS Teststep: CS.VPR.MAP.VSL.MPV.002 (Screenshot in "D:/TestSlave5/workspace/TestSlave5 SyncCode/SquishTests/ATF_Sandbox/tst_MapView\failedImages\failed_47.png") Screen shot captured: #REMARK WORKAROUND FOR : <resolution of the screen is 400x240> <Verifying width and height of the HomeScreen>  None 
CS.VPR.MAP.VSL.RDM.003 PASS #REMARK MANUAL VERIFICATION REQUIRED STEP: <Route preview is played at 300% of normal road speed> None 
CS.VPR.MAP.VSL.RDM.004 PASS #REMARK MANUAL VERIFICATION REQUIRED STEP: <Route Preview starts playing from the beginning and keeps looping until the Route Preview is stopped> None 

Я могу добиться того, что с ниже сценария -

for test in root.getchildren(): 
for test2 in test.getchildren(): 
    for message in test2.getchildren(): 
     if message.tag == "{http://www.froglogic.com/XML2}message": 
      messageText1 = message[0].text 
      if re.match(testStepPattern, messageText1): 
       testStepID = messageText1 
      elif messageText1.startswith('#REMARK MANUAL'): 
       remarkTag = message[0].text 
       print testStepID, '\t', 'PASS', '\t', remarkTag, '\t', "None", '\t', "NOT AUTOMATED" 
     elif message.tag == "{http://www.froglogic.com/XML2}verification": 
      resultAttrib = message[0].attrib.get('type') 
      resultDetails = message[0][0].text 
      resultDetailsMore = message[0][1].text 
      if "REMARK MANUAL" in str(resultDetailsMore): 
       print testStepID, '\t', resultAttrib, '\t', resultDetails, '\t', resultDetailsMore, '\t', "MANUAL" 
      else: 
       print testStepID, '\t', resultAttrib, '\t', resultDetails, '\t', resultDetailsMore, '\t', "None" 

Но при попытке разбить этот скрипт на функции, он бросает ошибку

Traceback (most recent call last): 
File "parser.py", line 85, in <module> 
    ParseXmlResults() 
File "parser.py", line 56, in ParseXmlResults 
testStepID, remarkTag = CheckAndGetMessageTagValues(message) 
TypeError: 'NoneType' object is not iterable 

Ниже приводится разбивка код -

import xml.etree.ElementTree as ET 
import re 

tree = ET.parse(r"C:\Users\M1032828\Desktop\result_MapView.xml") 
root = tree.getroot() 
testSetTree = root[0][1] 

testStepPattern = re.compile(r"\b([A-Z]{2,4}\.){4,5}[\d]{2,}\b") 
testStepID = "" 
tagDetails = "" 


def ParseXmlResults_ESR(): 
    global testStepID 
    for test in root.getchildren(): 
     print test 
     for test2 in test.getchildren(): 
      print "--------------------------------->", test2 
      for message in test2.getchildren(): 
       print '***********************>', message 
       if message.tag == "{http://www.froglogic.com/XML2}message": 
        messageText1 = message[0].text 
        print messageText1 
        testStepID, remarkTag = CheckAndGetMessageTagValues(message) 
        print testStepID, remarkTag 
       elif message.tag == "{http://www.froglogic.com/XML2}verification": 
        resultAttrib, resultDetails, resultDetailsMore = CheckAndGetVerificationTagDetails(message) 
        print resultAttrib, resultDetails, resultDetailsMore 

def CheckAndGetMessageTagValues(element): 
    global testStepID 
    global tagDetails 
    if re.match(testStepPattern, element[0].text): 
     testStepID = element[0].text 
     tagDetails = False 
     remarkTag = "" 
     return testStepID, remarkTag 
    elif element[0].text.startswith('#REMARK MANUAL'): 
     tagDetails = True 
     remarkTag = element[0].text 
     return testStepID, remarkTag 


def CheckAndGetVerificationTagDetails(element): 
    resultAttrib = element[0].attrib.get('type') 
    resultDetails = element[0][0].text 
    resultDetailsMore = element[0][1].text 
    return resultAttrib, resultDetails, resultDetailsMore 

if __name__ == "__main__": 
    ParseXmlResults() 

Может кто-то пожалуйста, помогите мне решить эту проблему. Заранее спасибо

+0

после полной трассировки стека – e4c5

+0

добавил полный трассировки стека в вопросе –

+0

что parser.py это из библиотеки, или это ваш код? – e4c5

ответ

0

Причина для вашего получаю эту ошибку в том, что был случай, когда CheckAndGetMessageTagValues не дал ничего, потому что ни один из ваших тестов: re.match(testStepPattern, element[0].text) ни element[0].text.startswith('#REMARK MANUAL') вернулся, правда, так что не запускать какой-либо из if положений.

Что нужно сделать, так это обеспечить, чтобы один из этих тестов всегда возвращал значение True или, что гораздо лучше, добавлял условие else, которое возвращало значения по умолчанию (или, возможно, даже улучшало бы исключение, если случай непредвиден), когда нет одно из других условий выполнено.

def CheckAndGetMessageTagValues(element): 
    global testStepID 
    global tagDetails 
    if re.match(testStepPattern, element[0].text): 
     testStepID = element[0].text 
     tagDetails = False 
     remarkTag = "" 
     return testStepID, remarkTag 
    elif element[0].text.startswith('#REMARK MANUAL'): 
     tagDetails = True 
     remarkTag = element[0].text 
     return testStepID, remarkTag 
    else: 
     return "defaultID", "noTagFound" 
     # or raise an Exception? 
+0

Спасибо за указание ... Я просто пропустил это! –

Смежные вопросы