|
| 1 | +# API |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +Html-reporter adds an `htmlReporter` object to the `hermione` object with its own API. |
| 6 | + |
| 7 | +| **Name** | **Type** | **Descriptiion** | |
| 8 | +| -------- | -------- | ---------------- | |
| 9 | +| [events](#events) | Object | A list of events to subscribe to. | |
| 10 | +| [extraItems](#extraitems) | Object | Additional elements to be added to the burger menu of the report. | |
| 11 | +| [imagesSaver](#imagessaver) | Object | Interface for saving images to the user's storage. | |
| 12 | +| [reportsSaver](#reportssaver) | Object | Interface for saving sqlite databases to the user's storage. | |
| 13 | +| [addExtraItem](#addextraitem) | Method | Adds an additional item to the burger menu of the report. | |
| 14 | +| [downloadDatabases](#downloaddatabases) | Method | Downloads all databases from the given files of the type _databaseUrls.json_. | |
| 15 | +| [mergeDatabases](#mergedatabases) | Method | Merges all given databases and saves the final report on the specified path. | |
| 16 | +| [getTestsTreeFromDatabase](#getteststreefromdatabase) | Method | Returns the test tree from the passed database. | |
| 17 | + |
| 18 | +## events |
| 19 | + |
| 20 | +A list of events to subscribe to. |
| 21 | + |
| 22 | +For more information, see the section "[Events](./html-reporter-events.md)". |
| 23 | + |
| 24 | +## extraItems |
| 25 | + |
| 26 | +Additional elements to be added to the burger menu of the report. |
| 27 | + |
| 28 | +To add elements, use the [addExtraItem](#addextraitem) method. |
| 29 | + |
| 30 | +## imagesSaver |
| 31 | + |
| 32 | +Interface for saving images to the user's storage. |
| 33 | + |
| 34 | +### Usage example |
| 35 | + |
| 36 | +```javascript |
| 37 | +const MyStorage = require('my-storage'); |
| 38 | +const myStorage = new MyStorage(); |
| 39 | + |
| 40 | +module.exports = (hermione, opts) => { |
| 41 | + hermione.on(hermione.events.INIT, async () => { |
| 42 | + hermione.htmlReporter.imagesSaver = { |
| 43 | + /** |
| 44 | + * Save the image to a custom storage. |
| 45 | + * The function can be either asynchronous or synchronous. |
| 46 | + * The function should return the path or URL to the saved image. |
| 47 | + * @property {String} localFilePath – the path to the image on the file system |
| 48 | + * @param {Object} options |
| 49 | + * @param {String} options.destPath – the path to the image in the html-report |
| 50 | + * @param {String} options.reportDir - path to the html-report folder |
| 51 | + * @returns {String} the path or URL to the image |
| 52 | + */ |
| 53 | + saveImg: async (localFilePath, options) => { |
| 54 | + const { destPath, reportDir } = options; |
| 55 | + const imageUrl = await myStorage.save(localFilePath, destPath, reportDir); |
| 56 | + |
| 57 | + // ... |
| 58 | + |
| 59 | + return imageUrl; |
| 60 | + } |
| 61 | + } |
| 62 | + }); |
| 63 | +}; |
| 64 | +``` |
| 65 | + |
| 66 | +## reportsSaver |
| 67 | + |
| 68 | +Interface for saving sqlite databases to the user's storage. |
| 69 | + |
| 70 | +### Usage example |
| 71 | + |
| 72 | +```javascript |
| 73 | +const MyStorage = require('my-storage'); |
| 74 | +const myStorage = new MyStorage(); |
| 75 | + |
| 76 | +module.exports = (hermione, opts) => { |
| 77 | + hermione.on(hermione.events.INIT, async () => { |
| 78 | + hermione.htmlReporter.reportsSaver = { |
| 79 | + /** |
| 80 | + * Save sqlite database to user storage. |
| 81 | + * The function can be either asynchronous or synchronous. |
| 82 | + * The function should return the path or URL to the saved sqlite database. |
| 83 | + * @property {String} localFilePath – the path to the sqlite database on the file system |
| 84 | + * @param {Object} options |
| 85 | + * @param {String} options.destPath – the path to the sqlite database in the html-report |
| 86 | + * @param {String} options.reportDir - path to the html-report folder |
| 87 | + * @returns {String} the path or URL to the sqlite database |
| 88 | + */ |
| 89 | + saveReportData: async (localFilePath, options) => { |
| 90 | + const { destPath, reportDir } = options; |
| 91 | + const dbUrl = await myStorage.save(localFilePath, destPath, reportDir); |
| 92 | + |
| 93 | + // ... |
| 94 | + |
| 95 | + return dbUrl; |
| 96 | + } |
| 97 | + } |
| 98 | + }); |
| 99 | +}; |
| 100 | +``` |
| 101 | + |
| 102 | +## addExtraItem |
| 103 | + |
| 104 | +Adds an additional item to the burger menu of the report. |
| 105 | + |
| 106 | +### Example of a call |
| 107 | + |
| 108 | +```javascript |
| 109 | +hermione.htmlReporter.addExtraItem(caption, url); |
| 110 | +``` |
| 111 | + |
| 112 | +### Call parameters |
| 113 | + |
| 114 | +All parameters are required. |
| 115 | + |
| 116 | +| **Parameter name** | **Type** | **Description** | |
| 117 | +| ----------------------- | -------- | --------------- | |
| 118 | +| caption | String | The name of the item to add to the burger menu. | |
| 119 | +| url | String | The URL to which the menu item to be added will link. | |
| 120 | + |
| 121 | +## downloadDatabases |
| 122 | + |
| 123 | +Downloads all databases from the given files of the type `databaseUrls.json`. |
| 124 | + |
| 125 | +### Example of a call |
| 126 | + |
| 127 | +```javascript |
| 128 | +const dbPaths = await hermione.htmlReporter.downloadDatabases( |
| 129 | + ['.\databaseUrls.json'], { pluginConfig } |
| 130 | +); |
| 131 | +``` |
| 132 | + |
| 133 | +### Call parameters |
| 134 | + |
| 135 | +The function takes 2 arguments—a list of paths to the files `databaseUrls.json` in the form of an array of strings and an object with the key `pluginConfig`, in the value of which the plugin config is stored. |
| 136 | + |
| 137 | +The function returns a list of paths to saved databases. |
| 138 | + |
| 139 | +## mergeDatabases |
| 140 | + |
| 141 | +Merges all given databases and saves the final report on the specified path. |
| 142 | + |
| 143 | +### Example of a call |
| 144 | + |
| 145 | +```javascript |
| 146 | +await hermione.htmlReporter.mergeDatabases(srcDbPaths, path); |
| 147 | +``` |
| 148 | + |
| 149 | +### Call parameters |
| 150 | + |
| 151 | +| **Parameter name** | **Type** | **Description** | |
| 152 | +| ----------------------- | -------- | --------------- | |
| 153 | +| srcDbPaths | String[] | Paths to databases. | |
| 154 | +| path | String | The path where the resulting database will be saved. | |
| 155 | + |
| 156 | +## getTestsTreeFromDatabase |
| 157 | + |
| 158 | +Returns the test tree from the passed database. |
| 159 | + |
| 160 | +### Example of a call |
| 161 | + |
| 162 | +```javascript |
| 163 | +const dbTree = hermione.htmlReporter.getTestsTreeFromDatabase(mergedDbPath); |
| 164 | +``` |
| 165 | + |
| 166 | +### Call parameters |
| 167 | + |
| 168 | +The function takes one argument—the path to the database with the result of the tests run. |
| 169 | + |
| 170 | +### Usage example |
| 171 | + |
| 172 | +```javascript |
| 173 | +function getSuccessTestRunIds({ hermione, mergedDbPath }) { |
| 174 | + const dbTree = hermione.htmlReporter.getTestsTreeFromDatabase(mergedDbPath); |
| 175 | + |
| 176 | + const successTestRunIds = []; |
| 177 | + |
| 178 | + for (const browserId of dbTree.browsers.allIds) { |
| 179 | + const browser = dbTree.browsers.byId[browserId]; |
| 180 | + const lastResultId = _.last(browser.resultIds); |
| 181 | + const lastResult = lastResultId && dbTree.results.byId[lastResultId]; |
| 182 | + |
| 183 | + if (!lastResult || lastResult.status !== SUCCESS) { |
| 184 | + continue; |
| 185 | + } |
| 186 | + |
| 187 | + const testRunId = new URL(lastResult.suiteUrl).searchParams.get("testRunId"); |
| 188 | + |
| 189 | + if (!testRunId) { |
| 190 | + continue; |
| 191 | + } |
| 192 | + |
| 193 | + successTestRunIds.push(testRunId); |
| 194 | + } |
| 195 | + |
| 196 | + return successTestRunIds; |
| 197 | +} |
| 198 | +``` |
0 commit comments