|
1 | 1 | package com.anhtester.helpers; |
2 | 2 |
|
| 3 | +import com.anhtester.constants.ConfigData; |
3 | 4 | import com.anhtester.drivers.DriverManager; |
4 | 5 | import com.anhtester.utils.DateUtils; |
5 | 6 | import io.appium.java_client.android.AndroidDriver; |
|
18 | 19 | import java.util.Base64; |
19 | 20 |
|
20 | 21 | public class CaptureHelpers { |
21 | | - /** |
22 | | - * Hàm static để chụp ảnh màn hình và lưu vào đường dẫn file được chỉ định. |
23 | | - * |
24 | | - * @param fileName Đường dẫn file nơi muốn lưu ảnh chụp màn hình (ví dụ: "screenshots/image.png"). |
25 | | - */ |
26 | | - public static void captureScreenshot(String fileName) { |
27 | | - try { |
28 | | - // Ép kiểu driver thành TakesScreenshot để lấy ảnh màn hình |
29 | | - File srcFile = ((TakesScreenshot) DriverManager.getDriver()).getScreenshotAs(OutputType.FILE); |
| 22 | + /** |
| 23 | + * Hàm static để chụp ảnh màn hình và lưu vào đường dẫn file được chỉ định. |
| 24 | + * |
| 25 | + * @param fileName Đường dẫn file nơi muốn lưu ảnh chụp màn hình (ví dụ: "screenshots/image.png"). |
| 26 | + */ |
| 27 | + public static void captureScreenshot(String fileName) { |
| 28 | + try { |
| 29 | + // Ép kiểu driver thành TakesScreenshot để lấy ảnh màn hình |
| 30 | + File srcFile = ((TakesScreenshot) DriverManager.getDriver()).getScreenshotAs(OutputType.FILE); |
30 | 31 |
|
31 | | - SystemHelpers.createFolder(SystemHelpers.getCurrentDir() + "exports/screenshots"); |
32 | | - String filePath = SystemHelpers.getCurrentDir() + "exports/screenshots/" + fileName + "_" + Thread.currentThread().getId() + "_" + SystemHelpers.makeSlug(DateUtils.getCurrentDateTime()) + ".png"; |
| 32 | + SystemHelpers.createFolder(SystemHelpers.getCurrentDir() + ConfigData.SCREENSHOT_PATH); |
| 33 | + String filePath = SystemHelpers.getCurrentDir() + ConfigData.SCREENSHOT_PATH + fileName + "_" + Thread.currentThread().getId() + "_" + SystemHelpers.makeSlug(DateUtils.getCurrentDateTime()) + ".png"; |
33 | 34 |
|
34 | | - // Tạo đối tượng Path cho file đích |
35 | | - Path targetPath = new File(filePath).toPath(); |
| 35 | + // Tạo đối tượng Path cho file đích |
| 36 | + Path targetPath = new File(filePath).toPath(); |
36 | 37 |
|
37 | | - // Sao chép file từ nguồn sang đích, thay thế file nếu đã tồn tại |
38 | | - Files.copy(srcFile.toPath(), targetPath, StandardCopyOption.REPLACE_EXISTING); |
39 | | - System.out.println("Chụp ảnh màn hình thành công, lưu tại: " + targetPath.toAbsolutePath()); |
40 | | - } catch (IOException e) { |
41 | | - System.err.println("Lỗi trong quá trình lưu file ảnh: " + e.getMessage()); |
42 | | - e.printStackTrace(); |
43 | | - } catch (Exception e) { |
44 | | - System.err.println("Lỗi trong quá trình chụp ảnh màn hình: " + e.getMessage()); |
45 | | - e.printStackTrace(); |
46 | | - } |
47 | | - } |
| 38 | + // Sao chép file từ nguồn sang đích, thay thế file nếu đã tồn tại |
| 39 | + Files.copy(srcFile.toPath(), targetPath, StandardCopyOption.REPLACE_EXISTING); |
| 40 | + System.out.println("Chụp ảnh màn hình thành công, lưu tại: " + targetPath.toAbsolutePath()); |
| 41 | + } catch (IOException e) { |
| 42 | + System.err.println("Lỗi trong quá trình lưu file ảnh: " + e.getMessage()); |
| 43 | + e.printStackTrace(); |
| 44 | + } catch (Exception e) { |
| 45 | + System.err.println("Lỗi trong quá trình chụp ảnh màn hình: " + e.getMessage()); |
| 46 | + e.printStackTrace(); |
| 47 | + } |
| 48 | + } |
48 | 49 |
|
49 | | - // Bắt đầu ghi video |
50 | | - public static void startRecording() { |
51 | | - if (DriverManager.getDriver() != null) { |
52 | | - ((AndroidDriver) DriverManager.getDriver()).startRecordingScreen( |
53 | | - new AndroidStartScreenRecordingOptions() |
54 | | - .withBitRate(4000000) // default: 4000000 |
55 | | - .withVideoSize("1080x2400") // 720 x 1600, 1080 x 2400 pixels |
56 | | - .withTimeLimit(Duration.ofMinutes(10))); // 10 minutes max video length |
57 | | - System.out.println("Bắt đầu ghi video cho " + DriverManager.getDriver().getCapabilities().getCapability("deviceName")); |
58 | | - } |
59 | | - } |
| 50 | + // Bắt đầu ghi video |
| 51 | + public static void startRecording() { |
| 52 | + if (DriverManager.getDriver() != null) { |
| 53 | + ((AndroidDriver) DriverManager.getDriver()).startRecordingScreen( |
| 54 | + new AndroidStartScreenRecordingOptions() |
| 55 | + .withBitRate(4000000) // default: 4000000 |
| 56 | + .withVideoSize("1080x2400") // 720 x 1600, 1080 x 2400 pixels |
| 57 | + .withTimeLimit(Duration.ofMinutes(10))); // 10 minutes max video length |
| 58 | + System.out.println("Bắt đầu ghi video cho " + DriverManager.getDriver().getCapabilities().getCapability("deviceName")); |
| 59 | + } |
| 60 | + } |
60 | 61 |
|
61 | | - // Dừng ghi video và lưu file |
62 | | - public static void stopRecording(String videoFileName) { |
63 | | - if (DriverManager.getDriver() != null) { |
64 | | - try { |
65 | | - String base64Video = ((CanRecordScreen) ((AndroidDriver) DriverManager.getDriver())).stopRecordingScreen(); |
66 | | - System.out.println("Base64 video length: " + (base64Video != null ? base64Video.length() : "null")); |
67 | | - if (base64Video != null && !base64Video.isEmpty()) { |
68 | | - byte[] videoBytes = Base64.getDecoder().decode(base64Video); |
69 | | - System.out.println("Video bytes length: " + videoBytes.length); |
70 | | - File videoFile = new File(videoFileName); |
71 | | - try (FileOutputStream fos = new FileOutputStream(videoFile)) { |
72 | | - fos.write(videoBytes); |
73 | | - } |
74 | | - System.out.println("Video được lưu tại: " + videoFile.getAbsolutePath() + " (Size: " + videoFile.length() + " bytes)"); |
75 | | - } else { |
76 | | - System.out.println("Không có dữ liệu video để lưu."); |
77 | | - } |
78 | | - } catch (Exception e) { |
79 | | - System.err.println("Lỗi khi dừng ghi video: " + e.getMessage()); |
| 62 | + // Dừng ghi video và lưu file |
| 63 | + public static void stopRecording(String videoFileName) { |
| 64 | + if (DriverManager.getDriver() != null) { |
| 65 | + try { |
| 66 | + String base64Video = ((CanRecordScreen) ((AndroidDriver) DriverManager.getDriver())).stopRecordingScreen(); |
| 67 | + System.out.println("Base64 video length: " + (base64Video != null ? base64Video.length() : "null")); |
| 68 | + if (base64Video != null && !base64Video.isEmpty()) { |
| 69 | + byte[] videoBytes = Base64.getDecoder().decode(base64Video); |
| 70 | + System.out.println("Video bytes length: " + videoBytes.length); |
| 71 | + File videoFile = new File(videoFileName); |
| 72 | + try (FileOutputStream fos = new FileOutputStream(videoFile)) { |
| 73 | + fos.write(videoBytes); |
| 74 | + } |
| 75 | + System.out.println("Video được lưu tại: " + videoFile.getAbsolutePath() + " (Size: " + videoFile.length() + " bytes)"); |
| 76 | + } else { |
| 77 | + System.out.println("Không có dữ liệu video để lưu."); |
80 | 78 | } |
81 | | - } |
82 | | - } |
| 79 | + } catch (Exception e) { |
| 80 | + System.err.println("Lỗi khi dừng ghi video: " + e.getMessage()); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
83 | 84 | } |
0 commit comments