==========================================================================================================

     写在前面:

     好久以前写的截屏和录屏的程序了,最近测广告曝光想录屏,就又拎起这部分内容了。

==========================================================================================================

 

首先,在以前的自动化框架程序里,定义了共通类,来实现截屏和录屏的功能,代码如下:

截屏代码:

    public static void TakeScreenShot(String strFileName) throws IOException {
        if (CommonConstants.SCREEN_SHORT_FLAG) {
            WebDriver driver = TestBase.getWebDriver();
            File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
            // get local system time
            Date now = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            String strTimeStampFileName = dateFormat.format(now) + "_" + strFileName;
            // Use timestamp file name
            String strFilePath = CommonConstants.SCREENSHOT_PATH + strTimeStampFileName + ".jpg";
            FileUtils.copyFile(screenshot, new File(strFilePath));
        }
    }

 

录屏初始化代码:

public static void InitScreenRecorder(String strFileName) throws IOException, AWTException {
        if (CommonConstants.SCREEN_RECORDER_FLAG) {
            // video Save Path:C:users<<UserName>>Videos
            GraphicsConfiguration gconfig = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
                    .getDefaultConfiguration();
            org.monte.media.Format fileFormat = new org.monte.media.Format(MediaTypeKey, MediaType.FILE, MimeTypeKey,
                    MIME_AVI);
            org.monte.media.Format screenFormat = new org.monte.media.Format(MediaTypeKey, MediaType.VIDEO, EncodingKey,
                    ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE, CompressorNameKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE,
                    DepthKey, (int) 24, FrameRateKey, Rational.valueOf(15), QualityKey, 1.0f, KeyFrameIntervalKey,
                    (int) (15 * 60));
            org.monte.media.Format mouseFormat = new org.monte.media.Format(MediaTypeKey, MediaType.VIDEO, EncodingKey,
                    "black", FrameRateKey, Rational.valueOf(30));
             screenRecorder = new ScreenRecorder(gconfig, fileFormat, screenFormat, mouseFormat, null);
}
}

开始录屏:

    public static void startScreenRecorder() throws IOException {
        if (CommonConstants.SCREEN_RECORDER_FLAG) {
            // Start Capturing the Video
            screenRecorder.start();
        }
    }

结束录屏:

    public static void stopScreenRecorder() throws IOException {
        if (CommonConstants.SCREEN_RECORDER_FLAG) {
            // Stop the ScreenRecorder
            screenRecorder.stop();
        }
    }

由于默认的录屏工具,录完屏存储的地址是默认:

C:users<<UserName>>Videos

不满足我现在想存储在指定目录下的需求,所以就重新调用screenRecorder构造函数:

            // screenRecorder = new ScreenRecorder(gconfig, fileFormat,
            // screenFormat, mouseFormat, null);

            // get local system time
            Date now = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            String strTimeStampFileName = dateFormat.format(now) + "_" + strFileName;
            String strFilePath = CommonConstants.SCREENRECORD_PATH + strTimeStampFileName;
            File movieFolder = new File(strFilePath);
            screenRecorder = new ScreenRecorder(gconfig, null, fileFormat, screenFormat, mouseFormat, null,
                    movieFolder);

将构造函数增加了movieFolder的参数,然后就可以想怎么存就怎么存取了。

 

posted on 2019-11-13 14:36  伊凡Ivan  阅读(713)  评论(0编辑  收藏  举报