Skip to content

Commit cf42685

Browse files
committed
Update ScreenshotTests.java
1 parent e55fa11 commit cf42685

File tree

1 file changed

+28
-29
lines changed
  • extentreports-testng-adapter-example/src/test/java/com/aventstack/extentreports/adapter/testng/tests

1 file changed

+28
-29
lines changed

extentreports-testng-adapter-example/src/test/java/com/aventstack/extentreports/adapter/testng/tests/ScreenshotTests.java

+28-29
Original file line numberDiff line numberDiff line change
@@ -14,57 +14,56 @@
1414
import com.aventstack.extentreports.service.ExtentTestManager;
1515
import com.aventstack.extentreports.testng.listener.ExtentITestListenerClassAdapter;
1616

17-
@Listeners({ExtentITestListenerClassAdapter.class})
17+
@Listeners({ ExtentITestListenerClassAdapter.class })
1818
public class ScreenshotTests {
19-
19+
20+
private static final String IMG_NAME = "screenshot.png";
21+
2022
// 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]
2427
private static final String OUTPUT_PATH = "test-output/HtmlReport/";
25-
26-
28+
2729
@Test
2830
public void passTest() {
2931
Assert.assertTrue(true);
3032
}
31-
33+
3234
/**
33-
* A screenshot will be attached for failTest
35+
* A screenshot will be attached for failTest
3436
*/
3537
@Test
3638
public void failTest() {
3739
Assert.assertTrue(false);
3840
}
39-
41+
4042
@AfterMethod
4143
public synchronized void afterMethod(ITestResult result) throws IOException {
4244
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;
5454
}
5555
}
56-
56+
5757
/**
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!!
6060
*
6161
* @return Image path
62-
* @throws IOException
62+
* @throws IOException
6363
*/
6464
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;
6867
}
69-
68+
7069
}

0 commit comments

Comments
 (0)