|
1 | 1 | var path = require('path'), |
2 | 2 | fs = require('fs'), |
3 | 3 | mkdirp = require('mkdirp'); |
4 | | -var BrowserStackReporter = { |
5 | | - name: "browserstack-protractor-plugin", |
6 | | - |
7 | | - teardown: function(){ |
8 | 4 |
|
9 | | - function onSingleFileOutput (sessionID) { |
10 | | - const xml = prepareXml(sessionID) |
11 | | - let filename = `WDIO.xunit.all.xml` // default |
12 | | - write(filename, xml) |
13 | | - } |
| 5 | +var suites = {}; |
| 6 | +var specs = {}; |
| 7 | +var myReporter = { |
14 | 8 |
|
15 | | - function prepareName(name = 'Skipped test') { |
16 | | - return name.split(/[^a-zA-Z0-9]+/).filter( |
17 | | - (item) => item && item.length |
18 | | - ).join('_') |
19 | | - } |
| 9 | + suiteStarted: function(suite){ |
| 10 | + currentSuite = suite; |
| 11 | + currentSuite.specs = [] |
| 12 | + }, |
20 | 13 |
|
21 | | - function prepareXml(sessionID){ |
22 | | - var xmlbuilder = require('xmlbuilder'); |
23 | | - const builder = xmlbuilder.create('testsuites', {encoding: 'UTF-8', allowSurrogateChars: true}) |
24 | | - const packageName = "packageName" |
25 | | - // TODO suitname |
26 | | - const suiteName = prepareName("suitetitle") |
27 | | - const testSuite = builder.ele("testsuite", {name: suiteName}) |
28 | | - // TODO testname |
29 | | - const testName = prepareName("testname") |
30 | | - const testCase = testSuite.ele("testcase",{className: `${packageName}.${suiteName}`, name: testName }); |
31 | | - testCase.ele("session", {}, sessionID); |
32 | | - return builder.end({ pretty: true}); |
33 | | - } |
| 14 | + suiteDone: function(suite) { |
| 15 | + suites[suite.id] = suite |
| 16 | + }, |
| 17 | + |
| 18 | + jasmineDone: function(){ |
| 19 | + this.onSingleFileOutput(suites) |
| 20 | + }, |
| 21 | + |
| 22 | + specDone: function(spec) { |
| 23 | + browser.getSession().then(function(session) { |
| 24 | + spec.sessionID = session.getId(); |
| 25 | + currentSuite.specs.push(spec) |
| 26 | + specs[spec.id] = spec |
| 27 | + }); |
| 28 | + }, |
| 29 | + |
| 30 | + onSingleFileOutput: function (suites) { |
| 31 | + const xml = this.prepareXml(suites) |
| 32 | + let filename = `REPORT-browserstack.all.xml` |
| 33 | + this.write(filename, xml) |
| 34 | + }, |
34 | 35 |
|
35 | | - function write(filename, xml) { |
36 | | - var outputDir = "target/browserstack-reports" |
37 | | - try { |
38 | | - const dir = path.resolve(outputDir) |
39 | | - const filepath = path.join(dir, filename) |
40 | | - mkdirp.sync(dir) |
41 | | - fs.writeFileSync(filepath, xml) |
42 | | - console.log(`Wrote xunit report "${filename}" to [${outputDir}].`) |
43 | | - } catch (e) { |
44 | | - console.log(`Failed to write xunit report "${filename}" |
45 | | - to [${outputDir}]. Error: ${e}`) |
| 36 | + prepareName: function(name = 'Skipped test') { |
| 37 | + return name.split(/[^a-zA-Z0-9]+/).filter( |
| 38 | + (item) => item && item.length |
| 39 | + ).join('_') |
| 40 | + }, |
| 41 | + |
| 42 | + prepareXml: function(suites){ |
| 43 | + var xmlbuilder = require('xmlbuilder'); |
| 44 | + const builder = xmlbuilder.create('testsuites', {encoding: 'UTF-8', allowSurrogateChars: true}) |
| 45 | + for (let suiteIndex in suites) { |
| 46 | + const suite = suites[suiteIndex] |
| 47 | + const suiteName = this.prepareName(suite.description) |
| 48 | + const testSuite = builder.ele("testsuite", {name: suiteName}) |
| 49 | + for (let specIndex in suite.specs) { |
| 50 | + const test = suite.specs[specIndex] |
| 51 | + const testName = this.prepareName(test.description) |
| 52 | + const testCase = testSuite.ele("testcase",{name: testName, id: `${suiteName}.${testName}{0}`, index: 0 }); |
| 53 | + testCase.ele("session", {}, test.sessionID); |
46 | 54 | } |
47 | 55 | } |
| 56 | + return builder.end({ pretty: true}); |
| 57 | + }, |
48 | 58 |
|
49 | | - function format (val) { |
50 | | - return JSON.stringify(baseReporter.limit(val)) |
| 59 | + write: function(filename, xml) { |
| 60 | + var outputDir = "./browserstack-reports" |
| 61 | + try { |
| 62 | + const dir = path.resolve(outputDir) |
| 63 | + const filepath = path.join(dir, filename) |
| 64 | + mkdirp.sync(dir) |
| 65 | + fs.writeFileSync(filepath, xml) |
| 66 | + console.log(`Wrote xunit report "${filename}" to [${outputDir}].`) |
| 67 | + } catch (e) { |
| 68 | + console.log(`Failed to write xunit report "${filename}" |
| 69 | + to [${outputDir}]. Error: ${e}`) |
51 | 70 | } |
| 71 | + }, |
52 | 72 |
|
53 | | - console.log("wowowow teardown") |
54 | | - browser.getSession().then(function(session) { |
55 | | - onSingleFileOutput(session.getId()) |
56 | | - }); |
| 73 | + format: function(val) { |
| 74 | + return JSON.stringify(baseReporter.limit(val)) |
57 | 75 | } |
| 76 | +} |
| 77 | + |
| 78 | +var BrowserStackReporter = { |
| 79 | + name: "browserstack-protractor-plugin", |
58 | 80 |
|
| 81 | + onPrepare: function() { |
| 82 | + jasmine.getEnv().addReporter(myReporter); |
| 83 | + }, |
59 | 84 | } |
60 | 85 |
|
61 | 86 | module.exports = BrowserStackReporter |
0 commit comments