Skip to content

Commit

Permalink
Merge pull request #29 from fijijavis/add_merge_util
Browse files Browse the repository at this point in the history
Add merge script
  • Loading branch information
fijijavis authored May 29, 2019
2 parents 715bbac + 89984f5 commit 293021d
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 1 deletion.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,37 @@ reporters: [
],
```

## Result Files
With WDIO v5, reporting has moved from a centralized process to one that is handled by each of the "sessions" spun up for parallel test execution.
This change helped reduce the amount of chatter during WDIO test execution and thus improved performance. The downside is we are no longer able
to get a single report for ALL test execution. Consider the following:

2 suites of tests configured to run in 2 browsers:

* WDIO v4: 1 json file with execution results
* WDIO v5: 4 json files with execution results


`wdio-json-reporter` provides a utility function to merge the multiple json files into a single file. Follow the steps below to take advantage of the utility.

1) Create a small node script
```javascript
const mergeResults = require('wdio-json-reporter/mergeResults')
mergeResults()
```

2) Call node script from command line and pass 2 arguments

* <RESULTS_DIR>: Directory where results files are written
* <FILE_REGEX>: Regex pattern for finding `wdio-json-reporter` result files in <RESULTS_DIR>. This is necessary because multiple reporters produce `json` result files

Example:
```bash
node mergeResults.json ./Results "wdio-json-*"
```

Upon completion, the merge script will output a single json file named `wdio-merged.json` in the provided <RESULTS_DIR>


# WDIO v4 Compatibility

Expand Down
2 changes: 2 additions & 0 deletions mergeResults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// make mergeResults available at the root
module.exports = require('./src/mergeResults')
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
{
"name": "wdio-json-reporter",
"version": "1.1.0",
"version": "1.2.0",
"description": "A WebdriverIO plugin. Report results in json format.",
"main": "./src/index.js",
"files": [
"src",
"mergeResults.js"
],
"scripts": {
"test": "jest",
"lint": "eslint ./src test/"
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class JsonReporter extends WDIOReporter {
testSuite.duration = suite._duration
testSuite.start = suite.start
testSuite.end = suite.end
testSuite.sessionId = runner.sessionId
testSuite.tests = MapTests(suite.tests)
testSuite.hooks = MapHooks(suite.hooks)

Expand Down
52 changes: 52 additions & 0 deletions src/mergeResults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const fs = require('fs')
const path = require('path')

const mergeResults = (...args) => {
const dir = process.argv[2]
const filePattern = process.argv[3]

const rawData = getDataFromFiles(dir, filePattern)
const mergedResults = mergeData(rawData)
writeFile(dir, mergedResults)
}

function getDataFromFiles (dir, filePattern) {
const fileNames = fs.readdirSync(dir).filter(file => file.match(filePattern))
const data = []

fileNames.forEach(fileName => {
data.push(JSON.parse(fs.readFileSync(`${dir}/${fileName}`)))
})

return data
}

function mergeData (rawData) {
let mergedResults

rawData.forEach(data => {
if (mergedResults === undefined) {
// use the first result so that we have the right shape
mergedResults = {}
Object.assign(mergedResults, data)
mergedResults.capabilities = [mergedResults.capabilities] // make this an array so we can capture all caps
} else {
mergedResults.suites.push(...data.suites)
mergedResults.specs.push(...data.specs)
mergedResults.state.passed += data.state.passed
mergedResults.state.failed += data.state.failed
mergedResults.state.skipped += data.state.skipped
mergedResults.capabilities.push(data.capabilities)
}
})

return mergedResults
}

function writeFile (dir, mergedResults) {
const fileName = 'wdio-merged.json'
const filePath = path.join(dir, fileName)
fs.writeFileSync(filePath, JSON.stringify(mergedResults))
}

module.exports = mergeResults

0 comments on commit 293021d

Please sign in to comment.