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
| public class TakeScreenShot {
private static WebDriver driver;
public static void init(WebDriver d) {
driver = d;
}
public static void takeSnapShot(String methodName) {
String folderBaseDriver = null;
if (driver instanceof InternetExplorerDriver) {
folderBaseDriver = "Internet Explore";
} else if (driver instanceof ChromeDriver) {
folderBaseDriver = "Chrome";
} else if (driver instanceof FirefoxDriver) {
folderBaseDriver = "Firefox";
}
String screenshootDir = "path to your folder you want to save the screenshot"+File.separator + folderBaseDriver;
File f = new File(screenshootDir);
if (!f.exists() || !f.isDirectory()) {
f.mkdirs();
}
File output = null;
File file;
if (folderBaseDriver.equalsIgnoreCase("Internet Explore")) {
ScreenRegion s = new DesktopScreenRegion();
try {
ImageIO.write(s.capture(), "png", new File(screenshootDir
+ File.separator + methodName + ".png"));
} catch (IOException e) {
e.printStackTrace();
}
}else{
output = ((TakesScreenshot) driver)
.getScreenshotAs(OutputType.FILE);
file = new File(screenshootDir, methodName + ".png");
try {
FileUtils.copyFile(output, file);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
|
Post a Comment
Post a Comment