• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 */
15
16class ReportExtend {
17    constructor(fileModule) {
18        this.id = 'extend';
19        this.fileModule = fileModule;
20    }
21
22    init(coreContext) {
23        this.coreContext = coreContext;
24        this.suiteService = this.coreContext.getDefaultService('suite');
25    }
26
27    taskStart() {
28
29    }
30
31    taskDone() {
32        const report = {
33            tag: 'testsuites',
34            name: 'summary_report',
35            timestamp: new Date().toDateString(),
36            time: '1',
37            errors: 0,
38            failures: 0,
39            tests: 0,
40            children: []
41        };
42        const rootSuite = this.suiteService.rootSuite;
43        if (rootSuite && rootSuite.childSuites) {
44            for (let testsuite of rootSuite.childSuites) {
45                let suiteReport = {
46                    tag: 'testsuite',
47                    name: testsuite['description'],
48                    errors: 0,
49                    tests: 0,
50                    failures: 0,
51                    time: '0.1',
52                    children: []
53                };
54                let specs = testsuite['specs'];
55                for (let testcase of specs) {
56                    report.tests++;
57                    suiteReport.tests++;
58                    let caseReport = {
59                        tag: 'testcase',
60                        name: testcase['description'],
61                        status: 'run',
62                        time: '0.0',
63                        classname: testsuite['description']
64                    };
65                    if (testcase.error) {
66                        caseReport['result'] = false;
67                        caseReport['children'] = [{
68                            tag: 'error',
69                            type: '',
70                            message: testcase.error.message
71                        }];
72                        report.errors++;
73                        suiteReport.errors++;
74                    } else if (testcase.result.failExpects.length > 0) {
75                        caseReport['result'] = false;
76                        let message = '';
77                        testcase.result.failExpects.forEach(failExpect => {
78                            message += failExpect.message || ('expect ' + failExpect.actualValue + ' ' + failExpect.checkFunc + ' ' + (failExpect.expectValue || '')) + ';';
79                        });
80                        caseReport['children'] = [{
81                            tag: 'failure',
82                            type: '',
83                            message: message
84                        }];
85                        report.failures++;
86                        suiteReport.failures++;
87                    } else {
88                        caseReport['result'] = true;
89                    }
90                    suiteReport.children.push(caseReport);
91                }
92                report.children.push(suiteReport);
93            }
94        }
95
96        let reportXml = '<?xml version="1.0" encoding="UTF-8"?>\n' + json2xml(report);
97        this.fileModule.writeText({
98            uri: 'internal://app/report.xml',
99            text: reportXml,
100            success: function () {
101                console.info('call success callback success');
102            },
103            fail: function (data, code) {
104                console.info('call fail callback success:');
105            },
106            complete: function () {
107                console.info('call complete callback success');
108            }
109        });
110    }
111}
112
113function json2xml(json) {
114    let tagName;
115    let hasChildren = false;
116    let childrenStr = '';
117    let attrStr = '';
118    for (let key in json) {
119        if (key === 'tag') {
120            tagName = json[key];
121        } else if (key === 'children') {
122            if (json[key].length > 0) {
123                hasChildren = true;
124                for (let child of json[key]) {
125                    childrenStr += json2xml(child);
126                }
127            }
128        } else {
129            attrStr += ` ${key}="${json[key]}"`;
130        }
131    }
132    let xml = `<${tagName}${attrStr}`;
133    xml += hasChildren ? `>${childrenStr}</${tagName}>` : '/>';
134    return xml;
135}
136
137export default ReportExtend;
138