Skip to content

Commit

Permalink
feat: ability to add meta info extenders
Browse files Browse the repository at this point in the history
  • Loading branch information
DudaGod committed Apr 4, 2019
1 parent 843a273 commit 5536494
Show file tree
Hide file tree
Showing 25 changed files with 225 additions and 60 deletions.
29 changes: 25 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ directory.
* **baseHost** (optional) - `String` - it changes original host for view in the browser; by default original host does not change
* **scaleImages** (optional) – `Boolean` – fit images into page width; `false` by default
* **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.
* **errorPatterns** (optional) - `Array` - error message patterns for 'Group by error' mode.
* **errorPatterns** (optional) - `Array` - error message patterns for 'Group by error' mode.
Array element must be `Object` ({'*name*': `String`, '*pattern*': `String`}) or `String` (interpret as *name* and *pattern*).
Test will be associated with group if test error matches on group error pattern.
Test will be associated with group if test error matches on group error pattern.
New group will be created if test cannot be associated with existing groups.

Also there is ability to override plugin parameters by CLI options or environment variables
Expand Down Expand Up @@ -74,7 +74,6 @@ Add plugin to your `hermione` config file:
```js
module.exports = {
// ...

plugins: {
'html-reporter/hermione': {
enabled: true,
Expand Down Expand Up @@ -141,5 +140,27 @@ Adds item to html report as link:
@param {String} text of link
@param {String} url of link

tool.htmlReporter.addExtraItem(linkText, linkUrl)
tool.htmlReporter.addExtraItem('some-text', 'some-url')
```

In this case url with link 'some-url' and text 'some-text' will be added to the menu bar.

### addMetaInfoExtender

Extend meta-info of each test using passed data:

```js
tool.htmlReporter.addMetaInfoExtender(name, value);
```

* **name** (required) `String` - name of meta info
* **value** (required) `Function` - handler to which `suite` and `extraItems` are passed

Example:
```js
tool.htmlReporter.addMetaInfoExtender('foo', (suite, extraItems) => {
return suite.suitePath.join(' ') + extraItems.platform;
});
```

In this case a line `suite full name: some-platform` will be added to the meta info of each test.
2 changes: 1 addition & 1 deletion gemini.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function prepareData(gemini, reportBuilder) {
gemini.on(gemini.events.END, (stats) => resolve(
reportBuilder
.setStats(stats)
.setExtraItems(gemini.htmlReporter.extraItems)
.setApiValues(gemini.htmlReporter.values)
));
});
}
Expand Down
2 changes: 1 addition & 1 deletion hermione.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function prepareData(hermione, reportBuilder) {
hermione.on(hermione.events.RUNNER_END, (stats) => resolve(
reportBuilder
.setStats(stats)
.setExtraItems(hermione.htmlReporter.extraItems)
.setApiValues(hermione.htmlReporter.values)
));
});

Expand Down
4 changes: 4 additions & 0 deletions lib/gui/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ exports.start = ({paths, tool, guiApi, configs}) => {
app.addClient(res);
});

server.set('json replacer', (key, val) => {
return typeof val === 'function' ? val.toString() : val;
});

server.get('/init', (req, res) => {
res.json(app.data);
});
Expand Down
2 changes: 1 addition & 1 deletion lib/gui/tool-runner-factory/base-tool-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module.exports = class ToolRunner {

this._eventSource = new EventSource();
this._reportBuilder = ReportBuilderFactory.create(this._toolName, tool, pluginConfig);
this._reportBuilder.setExtraItems(tool.htmlReporter.extraItems);
this._reportBuilder.setApiValues(tool.htmlReporter.values);
}

get config() {
Expand Down
21 changes: 18 additions & 3 deletions lib/plugin-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,29 @@

module.exports = class HtmlReporter {
constructor() {
this._extraItems = {};
this._values = {
extraItems: {},
metaInfoExtenders: {}
};
}

addExtraItem(key, value) {
this._extraItems[key] = value;
this._values.extraItems[key] = value;
}

get extraItems() {
return this._extraItems;
return this._values.extraItems;
}

addMetaInfoExtender(key, value) {
this._values.metaInfoExtenders[key] = value;
}

get metaInfoExtenders() {
return this._values.metaInfoExtenders;
}

get values() {
return this._values;
}
};
8 changes: 4 additions & 4 deletions lib/report-builder-factory/report-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = class ReportBuilder {
this._tree = {name: 'root'};
this._skips = [];
this._tool = tool;
this._extraItems = {};
this._apiValues = {};
this._pluginConfig = pluginConfig;
this._TestAdapter = TestAdapter;
}
Expand Down Expand Up @@ -104,8 +104,8 @@ module.exports = class ReportBuilder {
return this;
}

setExtraItems(extraItems) {
this._extraItems = extraItems;
setApiValues(values) {
this._apiValues = values;

return this;
}
Expand Down Expand Up @@ -213,7 +213,7 @@ module.exports = class ReportBuilder {
skips: _.uniq(this._skips, JSON.stringify),
suites: this._tree.children,
config: {defaultView, baseHost, scaleImages, lazyLoadOffset, errorPatterns},
extraItems: this._extraItems,
apiValues: this._apiValues,
date: new Date().toString()
}, this._stats);
}
Expand Down
6 changes: 5 additions & 1 deletion lib/server-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,12 @@ function hasImage(formattedResult) {
}

function prepareCommonJSData(data) {
const stringifiedData = JSON.stringify(data, (key, val) => {
return typeof val === 'function' ? val.toString() : val;
});

return [
`var data = ${JSON.stringify(data)};`,
`var data = ${stringifiedData};`,
'try { module.exports = data; } catch(e) {}'
].join('\n');
}
Expand Down
6 changes: 3 additions & 3 deletions lib/static/components/controls/menu-bar.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

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

if (isEmpty(extraItems)) {
return (<Fragment/>);
return null;
}

return (
Expand All @@ -35,4 +35,4 @@ class MenuBar extends Component {
}
}

export default connect(({extraItems}) => ({extraItems}))(MenuBar);
export default connect(({apiValues: {extraItems}}) => ({extraItems}))(MenuBar);
19 changes: 15 additions & 4 deletions lib/static/components/section/body/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

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

getExtraMetaInfo = () => {
const {suite, apiValues: {extraItems, metaInfoExtenders}} = this.props;

return omitBy(mapValues(metaInfoExtenders, (extender) => {
const stringifiedFn = extender.startsWith('return') ? extender : `return ${extender}`;

return new Function(stringifiedFn)()(suite, extraItems);
}), isEmpty);
}

_toggleTestResult({opened}) {
const {result: {name: browserId}, suite: {suitePath}} = this.props;

Expand Down Expand Up @@ -169,7 +179,7 @@ class Body extends Component {
</div>
{this._addRetryButton()}
</div>
<MetaInfo metaInfo={metaInfo} suiteUrl={suiteUrl}/>
<MetaInfo metaInfo={metaInfo} suiteUrl={suiteUrl} getExtraMetaInfo={this.getExtraMetaInfo}/>
{description && <Description content={description}/>}
{this._getTabs()}
</div>
Expand All @@ -179,10 +189,11 @@ class Body extends Component {
}

export default connect(
({gui, running, suites, suiteIds}) => ({
({gui, running, suites, suiteIds, apiValues}) => ({
failed: values(pick(suites, suiteIds.failed)),
gui,
running
running,
apiValues
}),
(dispatch) => ({actions: bindActionCreators(actions, dispatch)})
)(Body);
8 changes: 5 additions & 3 deletions lib/static/components/section/body/meta-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ const metaToElements = (metaInfo) => {
export default class MetaInfo extends Component {
static propTypes = {
metaInfo: PropTypes.object.isRequired,
suiteUrl: PropTypes.string.isRequired
suiteUrl: PropTypes.string.isRequired,
getExtraMetaInfo: PropTypes.func.isRequired
}

render() {
const {metaInfo, suiteUrl} = this.props;
const formattedMetaInfo = Object.assign({}, metaInfo, {url: mkLinkToUrl(suiteUrl, metaInfo.url)});
const {metaInfo, suiteUrl, getExtraMetaInfo} = this.props;
const extraMetaInfo = getExtraMetaInfo();
const formattedMetaInfo = Object.assign({}, metaInfo, extraMetaInfo, {url: mkLinkToUrl(suiteUrl, metaInfo.url)});
const metaElements = metaToElements(formattedMetaInfo);

return (
Expand Down
5 changes: 4 additions & 1 deletion lib/static/modules/default-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ export default Object.assign(defaults, {
failed: []
},
closeIds: [],
extraItems: {},
apiValues: {
extraItems: {},
metaInfoExtenders: {}
},
loading: {},
modal: {},
stats: {
Expand Down
9 changes: 4 additions & 5 deletions lib/static/modules/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const localStorage = window.localStorage;

function getInitialState(data) {
const {skips, suites, config, total, updated, passed,
failed, skipped, warned, retries, perBrowser, extraItems, gui = false, date} = data;
failed, skipped, warned, retries, perBrowser, apiValues, gui = false, date} = data;
const {errorPatterns, scaleImages, lazyLoadOffset, defaultView} = config;
const parsedURL = new URL(window.location.href);
const filteredBrowsers = parsedURL.searchParams.getAll('browser');
Expand All @@ -25,7 +25,7 @@ function getInitialState(data) {
skips,
groupedErrors,
config,
extraItems,
apiValues,
date: dateToLocaleString(date),
stats: {
all: {total, updated, passed, failed, skipped, retries, warned},
Expand All @@ -49,7 +49,7 @@ export default function reducer(state = getInitialState(compiledData), action) {
autoRun,
suites,
skips,
extraItems,
apiValues,
config: {scaleImages, lazyLoadOffset}
} = action.payload;
const {errorPatterns} = state.config;
Expand All @@ -71,7 +71,7 @@ export default function reducer(state = getInitialState(compiledData), action) {
autoRun,
skips,
groupedErrors,
extraItems,
apiValues,
view: {scaleImages, lazyLoadOffset}
},
formattedSuites
Expand Down Expand Up @@ -355,4 +355,3 @@ function forceUpdateSuiteData(suites, test) {
const id = getSuiteId(test);
suites[id] = cloneDeep(suites[id]);
}

4 changes: 4 additions & 0 deletions lib/static/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,10 @@ a:active {
text-decoration: none;
}

.menu-item__link::first-letter {
text-transform: uppercase;
}

.image-box__placeholder {
position: relative;
display: table-cell;
Expand Down
20 changes: 12 additions & 8 deletions test/gemini.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe('lib/gemini', () => {

sandbox.spy(ReportBuilder.prototype, 'setStats');
sandbox.stub(ReportBuilder.prototype, 'addSuccess');
sandbox.stub(ReportBuilder.prototype, 'setExtraItems');
sandbox.stub(ReportBuilder.prototype, 'setApiValues');
sandbox.stub(ReportBuilder.prototype, 'save');
});

Expand All @@ -110,14 +110,18 @@ describe('lib/gemini', () => {
return initReporter_().then(() => assert.calledOnce(PluginAdapter.prototype.init));
});

it('should set extra items', () => {
return initReporter_()
.then(() => {
gemini.htmlReporter.addExtraItem('some', 'item');
gemini.emit(gemini.events.END);
it('should set values added through api', async () => {
await initReporter_();

assert.calledOnceWith(ReportBuilder.prototype.setExtraItems, {some: 'item'});
});
gemini.htmlReporter.addExtraItem('key1', 'value1');
gemini.htmlReporter.addMetaInfoExtender('key2', 'value2');

gemini.emit(gemini.events.END);

assert.calledOnceWith(ReportBuilder.prototype.setApiValues, {
extraItems: {key1: 'value1'},
metaInfoExtenders: {key2: 'value2'}
});
});

it('should save statistic', () => {
Expand Down
20 changes: 12 additions & 8 deletions test/hermione.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe('lib/hermione', () => {
sandbox.stub(ReportBuilder.prototype, 'addError');
sandbox.stub(ReportBuilder.prototype, 'addFail');
sandbox.stub(ReportBuilder.prototype, 'addRetry');
sandbox.stub(ReportBuilder.prototype, 'setExtraItems');
sandbox.stub(ReportBuilder.prototype, 'setApiValues');
sandbox.stub(ReportBuilder.prototype, 'save');
});

Expand Down Expand Up @@ -192,14 +192,18 @@ describe('lib/hermione', () => {
});
});

it('should set extra items', () => {
return initReporter_()
.then(() => {
hermione.htmlReporter.addExtraItem('some', 'item');
it('should set values added through api', async () => {
await initReporter_();

return hermione.emitAndWait(hermione.events.RUNNER_END);
})
.then(() => assert.calledOnceWith(ReportBuilder.prototype.setExtraItems, {some: 'item'}));
hermione.htmlReporter.addExtraItem('key1', 'value1');
hermione.htmlReporter.addMetaInfoExtender('key2', 'value2');

await hermione.emitAndWait(hermione.events.RUNNER_END);

assert.calledOnceWith(ReportBuilder.prototype.setApiValues, {
extraItems: {key1: 'value1'},
metaInfoExtenders: {key2: 'value2'}
});
});

it('should save statistic', () => {
Expand Down
Loading

0 comments on commit 5536494

Please sign in to comment.