1/* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15class ReportExtend { 16 constructor (fileModule) { 17 this.id = 'extend' 18 this.fileModule = fileModule 19 } 20 21 init (coreContext) { 22 this.coreContext = coreContext 23 this.suiteService = this.coreContext.getDefaultService('suite') 24 } 25 26 taskStart () { 27 28 } 29 30 taskDone () { 31 const report = { 32 tag: 'testsuites', 33 name: 'summary_report', 34 timestamp: new Date().toDateString(), 35 time: '1', 36 errors: 0, 37 failures: 0, 38 tests: 0, 39 children: [] 40 } 41 const rootSuite = this.suiteService.rootSuite 42 if (rootSuite && rootSuite.childSuites) { 43 for (let testsuite of rootSuite.childSuites) { 44 let suiteReport = { 45 tag: 'testsuite', 46 name: testsuite['description'], 47 errors: 0, 48 tests: 0, 49 failures: 0, 50 time: '0.1', 51 children: [] 52 } 53 let specs = testsuite['specs'] 54 for (let testcase of specs) { 55 report.tests++ 56 suiteReport.tests++ 57 let caseReport = { 58 tag: 'testcase', 59 name: testcase['description'], 60 status: 'run', 61 time: '0.0', 62 classname: testsuite['description'] 63 } 64 if (testcase.error) { 65 caseReport['result'] = false 66 caseReport['children'] = [{ 67 tag: 'error', 68 type: '', 69 message: testcase.error.message 70 }] 71 report.errors++ 72 suiteReport.errors++ 73 } else if (testcase.result.failExpects.length > 0) { 74 caseReport['result'] = false 75 let message = '' 76 testcase.result.failExpects.forEach(failExpect => { 77 message += failExpect.message || ('expect ' + failExpect.actualValue + ' ' + failExpect.checkFunc + ' ' + (failExpect.expectValue || '')) + ';' 78 }) 79 caseReport['children'] = [{ 80 tag: 'failure', 81 type: '', 82 message: message 83 }] 84 report.failures++ 85 suiteReport.failures++ 86 } else { 87 caseReport['result'] = true 88 } 89 suiteReport.children.push(caseReport) 90 } 91 report.children.push(suiteReport) 92 } 93 } 94 95 let reportXml = '<?xml version="1.0" encoding="UTF-8"?>\n' + json2xml(report) 96 this.fileModule.writeText({ 97 uri: 'internal://app/report.xml', 98 text: reportXml, 99 success: function () { 100 console.info('call success callback success') 101 }, 102 fail: function (data, code) { 103 console.info('call fail callback success:') 104 }, 105 complete: function () { 106 console.info('call complete callback success') 107 } 108 }) 109 } 110} 111 112function json2xml (json) { 113 let tagName 114 let hasChildren = false 115 let childrenStr = '' 116 let attrStr = '' 117 for (let key in json) { 118 if (key === 'tag') { 119 tagName = json[key] 120 } else if (key === 'children') { 121 if (json[key].length > 0) { 122 hasChildren = true 123 for (let child of json[key]) { 124 childrenStr += json2xml(child) 125 } 126 } 127 } else { 128 attrStr += ` ${key}="${json[key]}"` 129 } 130 } 131 let xml = `<${tagName}${attrStr}` 132 xml += hasChildren ? `>${childrenStr}</${tagName}>` : '/>' 133 return xml 134} 135 136export default ReportExtend 137