Skip to content

Commit cf073e2

Browse files
author
Dave Haeffner
authored
Merge pull request #660 from serpi90/runner-junit-v3
feat: add --output-format=[jest | junit] to selenium-side-runner to export JUnit XML (V3)
2 parents 5694b32 + 66e11f9 commit cf073e2

File tree

3 files changed

+83
-14
lines changed

3 files changed

+83
-14
lines changed

docs/introduction/command-line-runner.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ You can now run all of your Selenium IDE tests on any browser, in parallel, and
88

99
There's just the small matter of installing the Selenium IDE command line runner, getting the necessary browser drivers (if running your tests locally), and launching the runner from a command prompt with the options you want.
1010

11-
![command-line-runner-sample](/selenium-ide/img/docs/runner.png)
11+
![command-line-runner-sample](/website/static/img/docs/runner.png)
1212

1313
## Prerequisites
1414

@@ -144,6 +144,34 @@ With Chrome specific capabilities you can also run the tests headlessly.
144144
selenium-side-runner -c "chromeOptions.args=[disable-infobars, headless]"
145145
```
146146

147+
### Output tests results to a file
148+
149+
If you need to export test results to a file (when running as part of a CI process for example), you can use a combination `--output-directory` and `--output-format`.
150+
151+
`--output-directory` defines if and where to put the test result files.
152+
153+
`--output-format` defines which format use for the test result file, it can be `jest` or `junit`. The default if format is not specified is `jest`
154+
155+
#### Examples
156+
157+
```sh
158+
selenium-side-runner --output-directory=results
159+
```
160+
161+
Will output results in `jest` frormat in `./results/projectName.json'
162+
163+
```sh
164+
selenium-side-runner --output-directory=results --output-format=jest
165+
```
166+
167+
Will output results in `jest` frormat in `./results/projectName.json'
168+
169+
```sh
170+
selenium-side-runner --output-directory=results --output-format=junit
171+
```
172+
173+
Will output results in `junit` frormat in `./results/projectName.xml'
174+
147175
## A framework at your fingertips
148176

149177
There are also other niceties that come out of the box with the runner. Things you would expect to be available in a traditional test automation framework.

packages/selenium-side-runner/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"global-npm": "^0.3.0",
3535
"jest-cli": "24.7.1",
3636
"jest-environment-selenium": "2.1.0",
37+
"jest-junit": "^6.3.0",
3738
"js-beautify": "^1.7.5",
3839
"js-yaml": "^3.13.1",
3940
"rimraf": "^2.6.2",

packages/selenium-side-runner/src/index.js

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ program
7070
)
7171
.option(
7272
'--output-directory [directory]',
73-
'Write test results to files, results written in JSON'
73+
'Write test results to files, format is defined by --output-format'
74+
)
75+
.option(
76+
'--output-format [jest | junit]',
77+
'Format for the output. (default: jest)'
7478
)
7579
.option(
7680
'--force',
@@ -180,6 +184,51 @@ configuration.baseUrl = program.baseUrl
180184
? program.baseUrl
181185
: configuration.baseUrl
182186

187+
configuration.outputFormat = () => ({
188+
jestArguments: [],
189+
jestConfiguration: {},
190+
packageJsonConfiguration: {},
191+
})
192+
if (program.outputDirectory) {
193+
const outputDirectory = path.isAbsolute(program.outputDirectory)
194+
? program.outputDirectory
195+
: path.join('..', program.outputDirectory)
196+
const outputFormatConfigurations = {
197+
jest(project) {
198+
return {
199+
jestArguments: [
200+
'--json',
201+
'--outputFile',
202+
path.join(outputDirectory, `${project.name}.json`),
203+
],
204+
jestConfiguration: {},
205+
packageJsonConfiguration: {},
206+
}
207+
},
208+
junit(project) {
209+
return {
210+
jestArguments: [],
211+
jestConfiguration: { reporters: ['default', 'jest-junit'] },
212+
packageJsonConfiguration: {
213+
'jest-junit': {
214+
outputDirectory: outputDirectory,
215+
outputName: `${project.name}.xml`,
216+
},
217+
},
218+
}
219+
},
220+
}
221+
const format = program.outputFormat ? program.outputFormat : 'jest'
222+
configuration.outputFormat = outputFormatConfigurations[format]
223+
if (!configuration.outputFormat) {
224+
const allowedFormats = Object.keys(outputFormatConfigurations).join(', ')
225+
winston.error(
226+
`'${format}'is not an output format, allowed values: ${allowedFormats}`
227+
)
228+
process.exit(1)
229+
}
230+
}
231+
183232
winston.debug(util.inspect(configuration))
184233

185234
let projectPath
@@ -228,7 +277,9 @@ function runProject(project) {
228277
],
229278
testEnvironment: 'jest-environment-selenium',
230279
testEnvironmentOptions: configuration,
280+
...configuration.outputFormat(project).jestConfiguration,
231281
},
282+
...configuration.outputFormat(project).packageJsonConfiguration,
232283
dependencies: project.dependencies || {},
233284
},
234285
null,
@@ -318,18 +369,7 @@ function runJest(project) {
318369
`{**/*${program.filter}*/*.test.js,**/*${program.filter}*.test.js}`,
319370
]
320371
.concat(program.maxWorkers ? ['-w', program.maxWorkers] : [])
321-
.concat(
322-
program.outputDirectory
323-
? [
324-
'--json',
325-
'--outputFile',
326-
path.isAbsolute(program.outputDirectory)
327-
? path.join(program.outputDirectory, `${project.name}.json`)
328-
: '../' +
329-
path.join(program.outputDirectory, `${project.name}.json`),
330-
]
331-
: []
332-
)
372+
.concat(configuration.outputFormat(project).jestArguments)
333373
const opts = {
334374
cwd: path.join(process.cwd(), projectPath),
335375
stdio: 'inherit',

0 commit comments

Comments
 (0)