Skip to content

Commit 5536494

Browse files
committed
feat: ability to add meta info extenders
1 parent 843a273 commit 5536494

File tree

25 files changed

+225
-60
lines changed

25 files changed

+225
-60
lines changed

README.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ directory.
2424
* **baseHost** (optional) - `String` - it changes original host for view in the browser; by default original host does not change
2525
* **scaleImages** (optional) – `Boolean` – fit images into page width; `false` by default
2626
* **lazyLoadOffset** (optional) - `Number` - allows you to specify how far above and below the viewport you want to begin loading images. Lazy loading would be disabled if you specify 0. `800` by default.
27-
* **errorPatterns** (optional) - `Array` - error message patterns for 'Group by error' mode.
27+
* **errorPatterns** (optional) - `Array` - error message patterns for 'Group by error' mode.
2828
Array element must be `Object` ({'*name*': `String`, '*pattern*': `String`}) or `String` (interpret as *name* and *pattern*).
29-
Test will be associated with group if test error matches on group error pattern.
29+
Test will be associated with group if test error matches on group error pattern.
3030
New group will be created if test cannot be associated with existing groups.
3131

3232
Also there is ability to override plugin parameters by CLI options or environment variables
@@ -74,7 +74,6 @@ Add plugin to your `hermione` config file:
7474
```js
7575
module.exports = {
7676
// ...
77-
7877
plugins: {
7978
'html-reporter/hermione': {
8079
enabled: true,
@@ -141,5 +140,27 @@ Adds item to html report as link:
141140
@param {String} text of link
142141
@param {String} url of link
143142

144-
tool.htmlReporter.addExtraItem(linkText, linkUrl)
143+
tool.htmlReporter.addExtraItem('some-text', 'some-url')
145144
```
145+
146+
In this case url with link 'some-url' and text 'some-text' will be added to the menu bar.
147+
148+
### addMetaInfoExtender
149+
150+
Extend meta-info of each test using passed data:
151+
152+
```js
153+
tool.htmlReporter.addMetaInfoExtender(name, value);
154+
```
155+
156+
* **name** (required) `String` - name of meta info
157+
* **value** (required) `Function` - handler to which `suite` and `extraItems` are passed
158+
159+
Example:
160+
```js
161+
tool.htmlReporter.addMetaInfoExtender('foo', (suite, extraItems) => {
162+
return suite.suitePath.join(' ') + extraItems.platform;
163+
});
164+
```
165+
166+
In this case a line `suite full name: some-platform` will be added to the meta info of each test.

gemini.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function prepareData(gemini, reportBuilder) {
3535
gemini.on(gemini.events.END, (stats) => resolve(
3636
reportBuilder
3737
.setStats(stats)
38-
.setExtraItems(gemini.htmlReporter.extraItems)
38+
.setApiValues(gemini.htmlReporter.values)
3939
));
4040
});
4141
}

hermione.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function prepareData(hermione, reportBuilder) {
3030
hermione.on(hermione.events.RUNNER_END, (stats) => resolve(
3131
reportBuilder
3232
.setStats(stats)
33-
.setExtraItems(hermione.htmlReporter.extraItems)
33+
.setApiValues(hermione.htmlReporter.values)
3434
));
3535
});
3636

lib/gui/server.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ exports.start = ({paths, tool, guiApi, configs}) => {
3030
app.addClient(res);
3131
});
3232

33+
server.set('json replacer', (key, val) => {
34+
return typeof val === 'function' ? val.toString() : val;
35+
});
36+
3337
server.get('/init', (req, res) => {
3438
res.json(app.data);
3539
});

lib/gui/tool-runner-factory/base-tool-runner.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module.exports = class ToolRunner {
3030

3131
this._eventSource = new EventSource();
3232
this._reportBuilder = ReportBuilderFactory.create(this._toolName, tool, pluginConfig);
33-
this._reportBuilder.setExtraItems(tool.htmlReporter.extraItems);
33+
this._reportBuilder.setApiValues(tool.htmlReporter.values);
3434
}
3535

3636
get config() {

lib/plugin-api.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,29 @@
22

33
module.exports = class HtmlReporter {
44
constructor() {
5-
this._extraItems = {};
5+
this._values = {
6+
extraItems: {},
7+
metaInfoExtenders: {}
8+
};
69
}
710

811
addExtraItem(key, value) {
9-
this._extraItems[key] = value;
12+
this._values.extraItems[key] = value;
1013
}
1114

1215
get extraItems() {
13-
return this._extraItems;
16+
return this._values.extraItems;
17+
}
18+
19+
addMetaInfoExtender(key, value) {
20+
this._values.metaInfoExtenders[key] = value;
21+
}
22+
23+
get metaInfoExtenders() {
24+
return this._values.metaInfoExtenders;
25+
}
26+
27+
get values() {
28+
return this._values;
1429
}
1530
};

lib/report-builder-factory/report-builder.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = class ReportBuilder {
1919
this._tree = {name: 'root'};
2020
this._skips = [];
2121
this._tool = tool;
22-
this._extraItems = {};
22+
this._apiValues = {};
2323
this._pluginConfig = pluginConfig;
2424
this._TestAdapter = TestAdapter;
2525
}
@@ -104,8 +104,8 @@ module.exports = class ReportBuilder {
104104
return this;
105105
}
106106

107-
setExtraItems(extraItems) {
108-
this._extraItems = extraItems;
107+
setApiValues(values) {
108+
this._apiValues = values;
109109

110110
return this;
111111
}
@@ -213,7 +213,7 @@ module.exports = class ReportBuilder {
213213
skips: _.uniq(this._skips, JSON.stringify),
214214
suites: this._tree.children,
215215
config: {defaultView, baseHost, scaleImages, lazyLoadOffset, errorPatterns},
216-
extraItems: this._extraItems,
216+
apiValues: this._apiValues,
217217
date: new Date().toString()
218218
}, this._stats);
219219
}

lib/server-utils.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,12 @@ function hasImage(formattedResult) {
124124
}
125125

126126
function prepareCommonJSData(data) {
127+
const stringifiedData = JSON.stringify(data, (key, val) => {
128+
return typeof val === 'function' ? val.toString() : val;
129+
});
130+
127131
return [
128-
`var data = ${JSON.stringify(data)};`,
132+
`var data = ${stringifiedData};`,
129133
'try { module.exports = data; } catch(e) {}'
130134
].join('\n');
131135
}

lib/static/components/controls/menu-bar.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
import React, {Component, Fragment} from 'react';
3+
import React, {Component} from 'react';
44
import {connect} from 'react-redux';
55
import PropTypes from 'prop-types';
66
import {Dropdown} from 'semantic-ui-react';
@@ -22,7 +22,7 @@ class MenuBar extends Component {
2222
const {extraItems} = this.props;
2323

2424
if (isEmpty(extraItems)) {
25-
return (<Fragment/>);
25+
return null;
2626
}
2727

2828
return (
@@ -35,4 +35,4 @@ class MenuBar extends Component {
3535
}
3636
}
3737

38-
export default connect(({extraItems}) => ({extraItems}))(MenuBar);
38+
export default connect(({apiValues: {extraItems}}) => ({extraItems}))(MenuBar);

lib/static/components/section/body/index.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
import {isEmpty, defaults, pick, values} from 'lodash';
3+
import {isEmpty, defaults, pick, values, mapValues, omitBy} from 'lodash';
44
import React, {Component} from 'react';
55
import {connect} from 'react-redux';
66
import {bindActionCreators} from 'redux';
@@ -78,6 +78,16 @@ class Body extends Component {
7878
this.props.actions.toggleStateResult({browserId, suitePath, stateName, retryIndex, opened});
7979
}
8080

81+
getExtraMetaInfo = () => {
82+
const {suite, apiValues: {extraItems, metaInfoExtenders}} = this.props;
83+
84+
return omitBy(mapValues(metaInfoExtenders, (extender) => {
85+
const stringifiedFn = extender.startsWith('return') ? extender : `return ${extender}`;
86+
87+
return new Function(stringifiedFn)()(suite, extraItems);
88+
}), isEmpty);
89+
}
90+
8191
_toggleTestResult({opened}) {
8292
const {result: {name: browserId}, suite: {suitePath}} = this.props;
8393

@@ -169,7 +179,7 @@ class Body extends Component {
169179
</div>
170180
{this._addRetryButton()}
171181
</div>
172-
<MetaInfo metaInfo={metaInfo} suiteUrl={suiteUrl}/>
182+
<MetaInfo metaInfo={metaInfo} suiteUrl={suiteUrl} getExtraMetaInfo={this.getExtraMetaInfo}/>
173183
{description && <Description content={description}/>}
174184
{this._getTabs()}
175185
</div>
@@ -179,10 +189,11 @@ class Body extends Component {
179189
}
180190

181191
export default connect(
182-
({gui, running, suites, suiteIds}) => ({
192+
({gui, running, suites, suiteIds, apiValues}) => ({
183193
failed: values(pick(suites, suiteIds.failed)),
184194
gui,
185-
running
195+
running,
196+
apiValues
186197
}),
187198
(dispatch) => ({actions: bindActionCreators(actions, dispatch)})
188199
)(Body);

0 commit comments

Comments
 (0)