/
ExtentReports.java
458 lines (434 loc) · 14.8 KB
/
ExtentReports.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
package com.aventstack.extentreports;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import com.aventstack.extentreports.gherkin.GherkinDialectManager;
import com.aventstack.extentreports.gherkin.model.IGherkinFormatterModel;
import com.aventstack.extentreports.model.Media;
import com.aventstack.extentreports.model.ReportStats;
import com.aventstack.extentreports.model.SystemEnvInfo;
import com.aventstack.extentreports.observer.ExtentObserver;
/**
* <p>
* The ExtentReports report client for starting reporters and building reports.
* For most applications, you should have one ExtentReports instance for the
* entire JVM.
* </p>
*
* <p>
* ExtentReports itself does not build any reports, but allows reporters to
* access information, which in turn create the reports. An example of
* building an HTML report and adding information to ExtentX:
* </p>
*
* <pre>
* ExtentSparkReporter spark = new ExtentHtmlReporter("ExtentSpark.html");
* ExtentReports extent = new ExtentReports();
* extent.attachReporter(spark);
*
* extent.createTest("TestName").pass("Test Passed");
*
* extent.flush();
* </pre>
*
* <p>
* A few notes:
* </p>
*
* <ul>
* <li>It is mandatory to call the <code>flush</code> method to ensure
* information is written to the started reporters.</li>
* <li>You can create standard and BDD-style tests using the
* <code>createTest</code> method</li>
* </ul>
*
* @see ExtentTest
* @see GherkinKeyword
* @see IGherkinFormatterModel
* @see Status
*/
public class ExtentReports extends AbstractProcessor implements Writable, AnalysisTypeConfigurable {
/**
* Attach a {@link ExtentObserver} reporter, allowing it to access all
* started tests, nodes and logs
*
* @param observer
* {@link ExtentObserver} reporter
*/
@SuppressWarnings("rawtypes")
public void attachReporter(ExtentObserver... observer) {
super.attachReporter(observer);
}
/**
* Creates a BDD-style test with description representing one of the
* {@link IGherkinFormatterModel} classes such as:
*
* <ul>
* <li>{@link com.aventstack.extentreports.gherkin.model.Feature}</li>
* <li>{@link com.aventstack.extentreports.gherkin.model.Background}</li>
* <li>{@link com.aventstack.extentreports.gherkin.model.Scenario}</li>
* <li>{@link com.aventstack.extentreports.gherkin.model.Given}</li>
* <li>{@link com.aventstack.extentreports.gherkin.model.When}</li>
* <li>{@link com.aventstack.extentreports.gherkin.model.Then}</li>
* <li>{@link com.aventstack.extentreports.gherkin.model.And}</li>
* <li>{@link com.aventstack.extentreports.gherkin.model.But}</li>
* </ul>
*
* <p>
* Example:
* </p>
*
* <pre>
* extent.createTest(Feature.class, "feature", "description");
* extent.createTest(Scenario.class, "scenario", "description");
* extent.createTest(Given.class, "given", "description");
* </pre>
*
* @param type
* A {@link IGherkinFormatterModel} type
* @param name
* Name of test
* @param description
* A short description of the test
*
* @return {@link ExtentTest} object
*/
public ExtentTest createTest(Class<? extends IGherkinFormatterModel> type, String name,
String description) {
ExtentTest t = new ExtentTest(this, type, name, description);
onTestCreated(t.getModel());
return t;
}
/**
* Creates a BDD-style test representing one of the
* {@link IGherkinFormatterModel} classes such as:
*
* <ul>
* <li>{@link com.aventstack.extentreports.gherkin.model.Feature}</li>
* <li>{@link com.aventstack.extentreports.gherkin.model.Background}</li>
* <li>{@link com.aventstack.extentreports.gherkin.model.Scenario}</li>
* <li>{@link com.aventstack.extentreports.gherkin.model.Given}</li>
* <li>{@link com.aventstack.extentreports.gherkin.model.When}</li>
* <li>{@link com.aventstack.extentreports.gherkin.model.Then}</li>
* <li>{@link com.aventstack.extentreports.gherkin.model.And}</li>
* <li>{@link com.aventstack.extentreports.gherkin.model.But}</li>
* </ul>
*
* <p>
* Example:
* </p>
*
* <pre>
* extent.createTest(Feature.class, "feature");
* extent.createTest(Scenario.class, "scenario");
* extent.createTest(Given.class, "given");
* </pre>
*
* @param type
* A {@link IGherkinFormatterModel} type
* @param name
* Name of test
*
* @return {@link ExtentTest} object
*/
public ExtentTest createTest(Class<? extends IGherkinFormatterModel> type, String name) {
return createTest(type, name, null);
}
/**
* Creates a BDD-style test with description using name of the Gherkin model
* such as:
*
* <ul>
* <li>{@link com.aventstack.extentreports.gherkin.model.Feature}</li>
* <li>{@link com.aventstack.extentreports.gherkin.model.Background}</li>
* <li>{@link com.aventstack.extentreports.gherkin.model.Scenario}</li>
* <li>{@link com.aventstack.extentreports.gherkin.model.Given}</li>
* <li>{@link com.aventstack.extentreports.gherkin.model.When}</li>
* <li>{@link com.aventstack.extentreports.gherkin.model.Then}</li>
* <li>{@link com.aventstack.extentreports.gherkin.model.And}</li>
* <li>{@link com.aventstack.extentreports.gherkin.model.But}</li>
* </ul>
*
* <p>
* Example:
* </p>
*
* <pre>
* extent.createTest(new GherkinKeyword("Feature"), "feature", "description");
* extent.createTest(new GherkinKeyword("Scenario"), "scenario", "description");
* extent.createTest(new GherkinKeyword("Given"), "given", "description");
* </pre>
*
* @param gherkinKeyword
* Name of the {@link GherkinKeyword}
* @param name
* Name of test
* @param description
* A short description of the test
*
* @return {@link ExtentTest} object
*/
public ExtentTest createTest(GherkinKeyword gherkinKeyword, String name, String description) {
Class<? extends IGherkinFormatterModel> clazz = gherkinKeyword.getKeyword().getClass();
return createTest(clazz, name, description);
}
/**
* Creates a BDD-style test using name of the Gherkin model such as:
*
* <ul>
* <li>{@link com.aventstack.extentreports.gherkin.model.Feature}</li>
* <li>{@link com.aventstack.extentreports.gherkin.model.Background}</li>
* <li>{@link com.aventstack.extentreports.gherkin.model.Scenario}</li>
* <li>{@link com.aventstack.extentreports.gherkin.model.Given}</li>
* <li>{@link com.aventstack.extentreports.gherkin.model.When}</li>
* <li>{@link com.aventstack.extentreports.gherkin.model.Then}</li>
* <li>{@link com.aventstack.extentreports.gherkin.model.And}</li>
* <li>{@link com.aventstack.extentreports.gherkin.model.But}</li>
* </ul>
*
* <p>
* Example:
* </p>
*
* <pre>
* extent.createTest(new GherkinKeyword("Feature"), "feature");
* extent.createTest(new GherkinKeyword("Scenario"), "scenario");
* extent.createTest(new GherkinKeyword("Given"), "given");
* </pre>
*
* @param gherkinKeyword
* Name of the {@link GherkinKeyword}
* @param testName
* Name of test
*
* @return {@link ExtentTest} object
*/
public ExtentTest createTest(GherkinKeyword gherkinKeyword, String testName) {
return createTest(gherkinKeyword, testName, null);
}
/**
* Creates a test with description
*
* @param name
* Name of test
* @param description
* A short test description
*
* @return {@link ExtentTest} object
*/
public ExtentTest createTest(String name, String description) {
ExtentTest t = new ExtentTest(this, name, description);
onTestCreated(t.getModel());
return t;
}
/**
* Creates a test
*
* @param name
* Name of test
*
* @return {@link ExtentTest} object
*/
public ExtentTest createTest(String name) {
return createTest(name, null);
}
/**
* Removes a test
*
* @param test
* {@link ExtentTest} object
*/
public void removeTest(ExtentTest test) {
onTestRemoved(test.getModel());
}
/**
* Removes a test by name
*
* @param name
* The test name
*/
public void removeTest(String name) {
getReport().findTest(getReport().getTestList(), name)
.ifPresent(this::onTestRemoved);
}
/**
* Writes test information from the started reporters to their output view
*
* <ul>
* <li>ExtentSparkReporter: flush output to HTML file</li>
* <li>ExtentKlovReporter: inserts or updates info in MongoDB</li>
* </ul>
*/
@Override
public void flush() {
onFlush();
}
/**
* Adds any applicable system information to all started reporters
*
* <p>
* Example:
* </p>
*
* <pre>
* extent.setSystemInfo("HostName", "AventStack");
* </pre>
*
* @param k
* Name of system variable
* @param v
* Value of system variable
*/
public void setSystemInfo(String k, String v) {
onSystemInfoAdded(new SystemEnvInfo(k, v));
}
/**
* Adds logs from test framework tools to the test-runner logs view (if
* available in the reporter)
*
* <p>
* TestNG usage example:
* </p>
*
* <pre>
* extent.setTestRunnerOutput(Reporter.getOutput());
* </pre>
*
* @param log
* Log string from the test runner frameworks such as TestNG or
* JUnit
*/
public void addTestRunnerOutput(List<String> log) {
log.forEach(this::addTestRunnerOutput);
}
/**
* Adds logs from test framework tools to the test-runner logs view (if
* available in the reporter)
*
* <p>
* TestNG usage example:
* </p>
*
* <pre>
* for (String s : Reporter.getOutput()) {
* extent.setTestRunnerOutput(s);
* }
* </pre>
*
* @param log
* Log string from the test runner frameworks such as TestNG or
* JUnit
*/
public void addTestRunnerOutput(String log) {
onReportLogAdded(log);
}
/**
* Tries to resolve a {@link Media} location if the supplied path is not
* found using supplied locations. This can resolve cases where the default
* path was supplied to be relative for a FileReporter. If the absolute path
* is not determined, the supplied will be used.
*
* @param path
* Dirs used to create absolute path of the {@link Media} object
*
* @return {@link ExtentReports}
*/
public ExtentReports tryResolveMediaPath(String[] path) {
setMediaResolverPath(path);
return this;
}
/**
* Creates the internal models by consuming a JSON archive from a previous
* run session. This provides the same functionality as available in earlier
* versions via <code>appendExisting</code>, with the exception of being
* accessible by all reporters instead of just one.
*
* @param jsonFile
* The JSON archive file
* @throws IOException
* Exception thrown if the jsonFile is not found
*/
public void createDomainFromJsonArchive(File jsonFile) throws IOException {
convertRawEntities(this, jsonFile);
}
/**
* Creates the internal models by consuming a JSON archive from a previous
* run session. This provides the same functionality as available in earlier
* versions via <code>appendExisting</code>, with the exception of being
* accessible by all reporters instead of just one.
*
* @param jsonFilePath
* The JSON archive file
* @throws IOException
* Exception thrown if the jsonFilePath is not found
*/
public void createDomainFromJsonArchive(String jsonFilePath) throws IOException {
createDomainFromJsonArchive(new File(jsonFilePath));
}
/**
* Use this setting when building post-execution reports, such as from
* TestNG IReporter. This setting allows setting test with your own
* time-stamps. With this enabled, Extent does not use time-stamps for tests
* at the time they were created.
*
* <p>
* If this setting is enabled and time-stamps are not specified explicitly,
* the time-stamps of test creation are used.
*
* @param useManualConfig
* Set to true if building reports at the end of execution with
* manual configuration
*/
public void setReportUsesManualConfiguration(boolean useManualConfig) {
setUsingNaturalConf(!useManualConfig);
}
/**
* Type of AnalysisStrategy for the reporter. Not all reporters support this
* setting.
*
* <p>
* There are 2 types of strategies available:
*
* <ul>
* <li>TEST: Shows analysis at the test and step level</li>
* <li>SUITE: Shows analysis at the suite, test and step level</li>
* </ul>
*
* @param strategy
* {@link AnalysisStrategy} determines the type of analysis
* (dashboard) created for the reporter. Not all reporters will
* support this setting.
*/
@Override
public void setAnalysisStrategy(AnalysisStrategy strategy) {
getReport().getStats().setAnalysisStrategy(strategy);
}
/**
* Allows setting the target language for Gherkin keywords.
*
* <p>
* Default setting is "en"
*
* @param language
* A valid dialect from <a href=
* "https://github.com/cucumber/cucumber/blob/master/gherkin/gherkin-languages.json">gherkin-languages.json</a>
*
* @throws UnsupportedEncodingException
* Thrown if the language is one of the supported language from
* <a href=
* "https://github.com/cucumber/cucumber/blob/master/gherkin/gherkin-languages.json">gherkin-languages.json</a>
*/
public void setGherkinDialect(String language) throws UnsupportedEncodingException {
GherkinDialectManager.setLanguage(language);
}
/**
* Returns an instance of {@link ReportStats} with counts of tests executed
* by their status (pass, fail, skip etc)
*
* @return an instance of {@link ReportStats}
*/
public ReportStats getStats() {
return getReport().getStats();
}
}