|
14 | 14 | import com.aventstack.extentreports.service.ExtentTestManager;
|
15 | 15 | import com.aventstack.extentreports.testng.listener.ExtentITestListenerClassAdapter;
|
16 | 16 |
|
17 |
| -@Listeners({ExtentITestListenerClassAdapter.class}) |
| 17 | +@Listeners({ ExtentITestListenerClassAdapter.class }) |
18 | 18 | public class ScreenshotTests {
|
19 |
| - |
| 19 | + |
| 20 | + private static final String IMG_NAME = "screenshot.png"; |
| 21 | + |
20 | 22 | // A static image stored under classpath
|
21 |
| - private static final String IMG_PATH = "src/test/resources/screenshot.png"; |
22 |
| - |
23 |
| - // Using the same OUTPUT_PATH as set in extent.properties [extent.reporter.html.out] |
| 23 | + private static final String IMG_PATH = "src/test/resources/" + IMG_NAME; |
| 24 | + |
| 25 | + // Using the same OUTPUT_PATH as set in extent.properties |
| 26 | + // [extent.reporter.html.out] |
24 | 27 | private static final String OUTPUT_PATH = "test-output/HtmlReport/";
|
25 |
| - |
26 |
| - |
| 28 | + |
27 | 29 | @Test
|
28 | 30 | public void passTest() {
|
29 | 31 | Assert.assertTrue(true);
|
30 | 32 | }
|
31 |
| - |
| 33 | + |
32 | 34 | /**
|
33 |
| - * A screenshot will be attached for failTest |
| 35 | + * A screenshot will be attached for failTest |
34 | 36 | */
|
35 | 37 | @Test
|
36 | 38 | public void failTest() {
|
37 | 39 | Assert.assertTrue(false);
|
38 | 40 | }
|
39 |
| - |
| 41 | + |
40 | 42 | @AfterMethod
|
41 | 43 | public synchronized void afterMethod(ITestResult result) throws IOException {
|
42 | 44 | switch (result.getStatus()) {
|
43 |
| - case ITestResult.FAILURE: |
44 |
| - ExtentTestManager.getTest(result).fail( |
45 |
| - "ITestResult.FAILURE, event afterMethod", |
46 |
| - MediaEntityBuilder.createScreenCaptureFromPath(getImage()).build() |
47 |
| - ); |
48 |
| - break; |
49 |
| - case ITestResult.SKIP: |
50 |
| - ExtentTestManager.getTest(result).skip("ITestResult.SKIP, event afterMethod"); |
51 |
| - break; |
52 |
| - default: |
53 |
| - break; |
| 45 | + case ITestResult.FAILURE: |
| 46 | + ExtentTestManager.getTest(result).fail("ITestResult.FAILURE, event afterMethod", |
| 47 | + MediaEntityBuilder.createScreenCaptureFromPath(getImage()).build()); |
| 48 | + break; |
| 49 | + case ITestResult.SKIP: |
| 50 | + ExtentTestManager.getTest(result).skip("ITestResult.SKIP, event afterMethod"); |
| 51 | + break; |
| 52 | + default: |
| 53 | + break; |
54 | 54 | }
|
55 | 55 | }
|
56 |
| - |
| 56 | + |
57 | 57 | /**
|
58 |
| - * !!This code block is just an example only!! |
59 |
| - * !!Real-world implemention would require capturing a screenshot!! |
| 58 | + * !!This code block is just an example only!! !!Real-world implemention would |
| 59 | + * require capturing a screenshot!! |
60 | 60 | *
|
61 | 61 | * @return Image path
|
62 |
| - * @throws IOException |
| 62 | + * @throws IOException |
63 | 63 | */
|
64 | 64 | private String getImage() throws IOException {
|
65 |
| - String sc = "screenshot.png"; |
66 |
| - Files.copy(new File(IMG_PATH).toPath(), new File(OUTPUT_PATH + sc).toPath()); |
67 |
| - return sc; |
| 65 | + Files.copy(new File(IMG_PATH).toPath(), new File(OUTPUT_PATH + IMG_NAME).toPath()); |
| 66 | + return IMG_NAME; |
68 | 67 | }
|
69 |
| - |
| 68 | + |
70 | 69 | }
|
0 commit comments