Monday, 25 May 2020

Screenshot Utility Function:

Screenshot Utility Function:
----------------------------

In order to take a screenshot we need the following classes & interfaces
1. TakesScreenshot(I)
2. WebDriver(I) | RemoteWebDriver(C)
3. OutputType(I)
4. FileUtils(C)

From java1.8 version onwards inside interface we can take implemented methods as well in the form of default methods and static methods.

public interface CaptureScreenshot {

public static String captureImage(String tcName){
String imgLoc = BaseTest.getProjctPath()+"\\Screenshots\\"+tcName+".jpeg";
TakesScreenshot ts = (TakesScreenshot) BaseTest.getDriver();
File image = ts.getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(image, new File(imgLoc));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return imgLoc;
}
public static void captureImage(String tcName,String description){
TakesScreenshot ts = (TakesScreenshot) BaseTest.getDriver();
File image = ts.getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(image, new File(BaseTest.getProjctPath()+"\\Screenshots\\"+tcName+".jpeg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

No comments:

Post a Comment