1717import org .openqa .selenium .TakesScreenshot ;
1818import org .openqa .selenium .WebDriver ;
1919
20+ import javax .imageio .ImageIO ;
2021import java .awt .*;
22+ import java .awt .image .BufferedImage ;
2123import java .io .File ;
2224import java .io .IOException ;
2325import java .text .SimpleDateFormat ;
@@ -39,8 +41,7 @@ public class CaptureHelpers extends ScreenRecorder {
3941 private static SimpleDateFormat dateFormat = new SimpleDateFormat ("dd-MM-yyyy HH-mm-ss" );
4042
4143 //Hàm xây dựng
42- public CaptureHelpers (GraphicsConfiguration cfg , Rectangle captureArea , Format fileFormat , Format screenFormat ,
43- Format mouseFormat , Format audioFormat , File movieFolder , String name ) throws IOException , AWTException {
44+ public CaptureHelpers (GraphicsConfiguration cfg , Rectangle captureArea , Format fileFormat , Format screenFormat , Format mouseFormat , Format audioFormat , File movieFolder , String name ) throws IOException , AWTException {
4445 super (cfg , captureArea , fileFormat , screenFormat , mouseFormat , audioFormat , movieFolder );
4546 this .name = name ;
4647 }
@@ -54,8 +55,7 @@ protected File createMovieFile(Format fileFormat) throws IOException {
5455 } else if (!movieFolder .isDirectory ()) {
5556 throw new IOException (movieFolder + " is not a directory." );
5657 }
57- return new File (movieFolder ,
58- name + "_" + dateFormat .format (new Date ()) + "." + Registry .getInstance ().getExtension (fileFormat ));
58+ return new File (movieFolder , name + "_" + dateFormat .format (new Date ()) + "." + Registry .getInstance ().getExtension (fileFormat ));
5959 }
6060
6161 // Hàm Start record video
@@ -71,18 +71,9 @@ public static void startRecord(String methodName) {
7171
7272 Rectangle captureSize = new Rectangle (0 , 0 , width , height );
7373
74- GraphicsConfiguration gc = GraphicsEnvironment
75- .getLocalGraphicsEnvironment ()
76- .getDefaultScreenDevice ()
77- .getDefaultConfiguration ();
74+ GraphicsConfiguration gc = GraphicsEnvironment .getLocalGraphicsEnvironment ().getDefaultScreenDevice ().getDefaultConfiguration ();
7875 try {
79- screenRecorder = new CaptureHelpers (gc , null ,
80- new Format (MediaTypeKey , MediaType .FILE , MimeTypeKey , MIME_AVI ),
81- new Format (MediaTypeKey , MediaType .VIDEO , EncodingKey , ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE ,
82- CompressorNameKey , ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE , DepthKey , 24 , FrameRateKey ,
83- Rational .valueOf (15 ), QualityKey , 1.0f , KeyFrameIntervalKey , 15 * 60 ),
84- new Format (MediaTypeKey , MediaType .VIDEO , EncodingKey , "black" , FrameRateKey , Rational .valueOf (30 )),
85- null , file , methodName );
76+ screenRecorder = new CaptureHelpers (gc , null , 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 );
8677
8778 screenRecorder .start ();
8879 } catch (IOException | AWTException e ) {
@@ -123,4 +114,32 @@ public static void captureScreenshot(WebDriver driver, String screenName) {
123114 }
124115 }
125116
117+ public static File getScreenshot (String screenshotName ) {
118+ Rectangle allScreenBounds = new Rectangle (Toolkit .getDefaultToolkit ().getScreenSize ());
119+ String dateName = new SimpleDateFormat ("yyyy-MM-dd-hh-mm-ss.SSS" ).format (new Date ());
120+ BufferedImage image = null ;
121+ try {
122+ image = new Robot ().createScreenCapture (allScreenBounds );
123+ } catch (AWTException e ) {
124+ throw new RuntimeException (e );
125+ }
126+
127+ String path = Helpers .getCurrentDir () + FrameworkConstants .EXTENT_REPORT_FOLDER + File .separator + "images" ;
128+ File folder = new File (path );
129+ if (!folder .exists ()) {
130+ folder .mkdir ();
131+ Log .info ("Folder created: " + folder );
132+ }
133+
134+ String filePath = path + File .separator + screenshotName + dateName + ".png" ;
135+ File file = new File (filePath );
136+ try {
137+ ImageIO .write (image , "PNG" , file );
138+ } catch (IOException e ) {
139+ throw new RuntimeException (e );
140+ }
141+ return file ;
142+ }
143+
144+
126145}
0 commit comments