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