|
| 1 | +package com.anhtester.helpers; |
| 2 | + |
| 3 | +import com.anhtester.drivers.DriverManager; |
| 4 | +import org.monte.media.Format; |
| 5 | +import org.monte.media.Registry; |
| 6 | +import org.monte.media.math.Rational; |
| 7 | +import org.monte.screenrecorder.ScreenRecorder; |
| 8 | +import org.openqa.selenium.OutputType; |
| 9 | +import org.openqa.selenium.TakesScreenshot; |
| 10 | +import org.openqa.selenium.io.FileHandler; |
| 11 | + |
| 12 | +import java.awt.*; |
| 13 | +import java.io.File; |
| 14 | +import java.io.IOException; |
| 15 | +import java.text.SimpleDateFormat; |
| 16 | +import java.util.Date; |
| 17 | + |
| 18 | +import static org.monte.media.FormatKeys.*; |
| 19 | +import static org.monte.media.VideoFormatKeys.*; |
| 20 | + |
| 21 | +public class CaptureHelper extends ScreenRecorder { |
| 22 | + |
| 23 | + // Record with Monte Media library |
| 24 | + public static ScreenRecorder screenRecorder; |
| 25 | + public String name; |
| 26 | + |
| 27 | + //Hàm xây dựng |
| 28 | + public CaptureHelper(GraphicsConfiguration cfg, Rectangle captureArea, Format fileFormat, Format screenFormat, Format mouseFormat, Format audioFormat, File movieFolder, String name) throws IOException, AWTException { |
| 29 | + super(cfg, captureArea, fileFormat, screenFormat, mouseFormat, audioFormat, movieFolder); |
| 30 | + this.name = name; |
| 31 | + } |
| 32 | + |
| 33 | + //Hàm này bắt buộc để ghi đè custom lại hàm trong thư viên viết sẵn |
| 34 | + @Override |
| 35 | + protected File createMovieFile(Format fileFormat) throws IOException { |
| 36 | + |
| 37 | + if (!movieFolder.exists()) { |
| 38 | + movieFolder.mkdirs(); |
| 39 | + } else if (!movieFolder.isDirectory()) { |
| 40 | + throw new IOException("\"" + movieFolder + "\" is not a directory."); |
| 41 | + } |
| 42 | + SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH-mm-ss"); |
| 43 | + return new File(movieFolder, name + "-" + dateFormat.format(new Date()) + "." + Registry.getInstance().getExtension(fileFormat)); |
| 44 | + } |
| 45 | + |
| 46 | + // Start record video |
| 47 | + public static void startRecord(String methodName) { |
| 48 | + //Tạo thư mục để lưu file video vào |
| 49 | + File file = new File(SystemHelper.getCurrentDir() + PropertiesHelper.getValue("VIDEO_RECORD_PATH")); |
| 50 | + Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); |
| 51 | + int width = screenSize.width; |
| 52 | + int height = screenSize.height; |
| 53 | + |
| 54 | + Rectangle captureSize = new Rectangle(0, 0, width, height); |
| 55 | + |
| 56 | + GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration(); |
| 57 | + try { |
| 58 | + screenRecorder = new CaptureHelper(gc, captureSize, new Format(MediaTypeKey, MediaType.FILE, MimeTypeKey, MIME_AVI), new Format(MediaTypeKey, MediaType.VIDEO, EncodingKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE, CompressorNameKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE, DepthKey, 24, FrameRateKey, Rational.valueOf(15), QualityKey, 1.0f, KeyFrameIntervalKey, 15 * 60), new Format(MediaTypeKey, MediaType.VIDEO, EncodingKey, "black", FrameRateKey, Rational.valueOf(30)), null, file, methodName); |
| 59 | + screenRecorder.start(); |
| 60 | + } catch (IOException e) { |
| 61 | + throw new RuntimeException(e); |
| 62 | + } catch (AWTException e) { |
| 63 | + throw new RuntimeException(e); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // Stop record video |
| 68 | + public static void stopRecord() { |
| 69 | + try { |
| 70 | + screenRecorder.stop(); |
| 71 | + } catch (IOException e) { |
| 72 | + throw new RuntimeException(e); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + //Tạo format ngày giờ để xíu gắn dô cái name của screenshot hoặc record video không bị trùng tên (không bị ghi đè file) |
| 78 | + private static SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH-mm-ss"); |
| 79 | + |
| 80 | + public static void captureScreenshot(String screenshotName) { |
| 81 | + try { |
| 82 | + // Tạo tham chiếu đối tượng của TakesScreenshot với dirver hiện tại |
| 83 | + TakesScreenshot ts = (TakesScreenshot) DriverManager.getDriver(); |
| 84 | + // Gọi hàm getScreenshotAs để chuyển hóa hình ảnh về dạng FILE |
| 85 | + File source = ts.getScreenshotAs(OutputType.FILE); |
| 86 | + //Kiểm tra folder nếu không tồn tại thì tạo folder |
| 87 | + File theDir = new File(SystemHelper.getCurrentDir() + PropertiesHelper.getValue("SCREENSHOT_PATH")); |
| 88 | + if (!theDir.exists()) { |
| 89 | + theDir.mkdirs(); |
| 90 | + } |
| 91 | + // Chổ này đặt tên thì truyền biến "screenName" gán cho tên File chụp màn hình |
| 92 | + FileHandler.copy(source, new File(SystemHelper.getCurrentDir() + PropertiesHelper.getValue("SCREENSHOT_PATH") + File.separator + screenshotName + "_" + dateFormat.format(new Date()) + ".png")); |
| 93 | + System.out.println("Screenshot taken: " + screenshotName); |
| 94 | + System.out.println("Screenshot taken current URL: " + DriverManager.getDriver().getCurrentUrl()); |
| 95 | + } catch (Exception e) { |
| 96 | + System.out.println("Exception while taking screenshot: " + e.getMessage()); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + public static void takeScreenshot(String screenshotName) { |
| 101 | + // Tạo tham chiếu của TakesScreenshot |
| 102 | + TakesScreenshot ts = (TakesScreenshot) DriverManager.getDriver(); |
| 103 | + // Gọi hàm để chụp ảnh màn hình - getScreenshotAs |
| 104 | + File source = ts.getScreenshotAs(OutputType.FILE); |
| 105 | + // Kiểm tra folder tồn tại. Nếu không thì tạo mới folder theo đường dẫn |
| 106 | + File theDir = new File(SystemHelper.getCurrentDir() + PropertiesHelper.getValue("SCREENSHOT_PATH")); |
| 107 | + if (!theDir.exists()) { |
| 108 | + theDir.mkdirs(); //Tạo mới thư mục |
| 109 | + } |
| 110 | + // Lưu file ảnh với tên cụ thể vào đường dẫn |
| 111 | + try { |
| 112 | + FileHandler.copy(source, new File(SystemHelper.getCurrentDir() + PropertiesHelper.getValue("SCREENSHOT_PATH") + screenshotName + ".png")); |
| 113 | + System.out.println("Take screenshot " + screenshotName + " successfully."); |
| 114 | + } catch (IOException e) { |
| 115 | + System.out.println("ERROR. Can not Take screenshot " + screenshotName + "."); |
| 116 | + e.printStackTrace(); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | +} |
0 commit comments