Skip to content

Commit 8e90ccc

Browse files
committed
Fixed the issue with taking screenshots when the test fails.
1 parent fbe3d47 commit 8e90ccc

File tree

2 files changed

+11
-22
lines changed

2 files changed

+11
-22
lines changed

src/main/java/listeners/TestListener.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import constants.GlobalConstants;
2323
import decorators.Driver;
24+
import drivermanager.DriverManager;
2425
import logsetup.Log;
2526
import reports.ExtentReport;
2627
import utilities.ScreenshotUtility;
@@ -68,22 +69,17 @@ public void onTestSuccess(ITestResult result) {
6869
@Override
6970
public void onTestFailure(ITestResult result) {
7071
String testDesc=result.getMethod().getDescription();
71-
72-
//Getting the driver from result parameter for taking the screenshot on failure of test
73-
try {
74-
Driver driver = (Driver)result.getTestClass().getRealClass().getField("driver").get(result.getInstance());
75-
72+
try {
7673
//Take screenshot and copying to Screenshot folder in the project
7774
String screenshotsFolderPath =GlobalConstants.SCREENSHOT_FOLDER + testDesc + ".png";
7875

79-
//Call takescreenshot() method from DriverLogger class and copying the screenshot from source path to Screenshot folder
80-
//FileHandler.copy(((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE),
81-
// new File(screenshotsFolderPath));
82-
76+
//Call takescreenshot() method from DriverLogger class and copying the screenshot from source path to Screenshot folder
77+
FileHandler.copy(ScreenshotUtility.getscreenshot(),new File(screenshotsFolderPath));
78+
8379
//Add screenshot from Screenshot folder to extent report
84-
//extentTest.addScreenCaptureFromPath(screenshotsFolderPath);
80+
extentTest.addScreenCaptureFromPath(screenshotsFolderPath);
8581

86-
} catch (IllegalArgumentException | SecurityException | NoSuchFieldException | IllegalAccessException | WebDriverException e) {
82+
} catch (IllegalArgumentException | SecurityException | WebDriverException | IOException e) {
8783
e.printStackTrace();
8884
}
8985

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
1-
/**
2-
*
3-
*/
41
package utilities;
52

63
import java.io.File;
74

8-
import org.openqa.selenium.OutputType;
9-
import org.openqa.selenium.TakesScreenshot;
10-
import decorators.Driver;
115
import drivermanager.DriverManager;
126

137
/**
148
* @author Alfred Sunny
159
*
1610
*/
17-
public class ScreenshotUtility {
11+
public final class ScreenshotUtility {
1812

19-
//Method to get the screenshots
20-
public static File takescreenshot(Driver driver) {
21-
22-
return ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
13+
//Method to get the screenshots. Instance of the threadlocal is used to prevent the thread related issue
14+
public static File getscreenshot() {
15+
return DriverManager.getDriver().takescreenshot();
2316
}
2417
}

0 commit comments

Comments
 (0)