Search code examples
javaselenium-webdrivervideoscreen-recording

Selenium + Java + Screen Recorder - Not able to record screen


I have created the next class for Recording video in the Selenium Webriver Automation.

First of all, for this I'm using the Monte Media jar (Dependency in POM):

 <dependency>
        <groupId>com.github.stephenc.monte</groupId>
        <artifactId>monte-screen-recorder</artifactId>
        <version>0.7.7.0</version>
    </dependency>

The class that I have created is the next:

public class Video {

private static final Logger logger = LogManager.getLogger(Video.class);
private Properties prop = null;
private String path = "";
private String prefix = "";
private String extension = "";
private String time = "";
private static ScreenRecorder screenRecorder;


public void setUp() throws Exception {

    //Crea una instancia de GraphicsConfiguration para obener la configuración gráfica.
    //de la pantalla.
    GraphicsConfiguration gc = GraphicsEnvironment//
            .getLocalGraphicsEnvironment()//
            .getDefaultScreenDevice()//
            .getDefaultConfiguration();

    //Crea la instancia del grabador de video con la configuración necesaria.
    ScreenRecorder screenRecorder = new ScreenRecorder(gc,
            new Format(MediaTypeKey, FormatKeys.MediaType.FILE, MimeTypeKey, MIME_AVI),
            new Format(MediaTypeKey, FormatKeys.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)),
            new Format(MediaTypeKey, MediaType.VIDEO, EncodingKey, "black",
                    FrameRateKey, Rational.valueOf(30)),
            null);

}

public static void iniciaGrabacion(InternetExplorerDriver driver)throws Exception{

    //Método para comenzar de la grabación.
    screenRecorder.start();

}

public static void finalizaGrabacion(InternetExplorerDriver driver)throws Exception{

    //Método para finalizar de la grabación.
    screenRecorder.stop();
}


private void prepareVariables() {
    try {
        prop = AppConfigUtil.getInstance().getProperties();
    } catch (AppConfigException e) {
        logger.error(e);
    }
    path = (String) prop.get("video.path");
    prefix = (String) prop.get("video.prefix");
    extension = (String) prop.get("video.extension");
    LocalDateTime localDateTime = LocalDateTime.now();
    time = "" + localDateTime.getYear() + localDateTime.getMonthValue() + localDateTime.getDayOfMonth() + localDateTime.getHour() + localDateTime.getMinute() + localDateTime.getSecond() + localDateTime.getNano();
}

What I want to do is calling the static void methods for begin recording (iniciaGrabacion) and stop recodring (finalizaGrabacion) from any other class, not in the same class that I've posted.

public static void iniciaGrabacion(InternetExplorerDriver driver)throws Exception{

    //Método para comenzar de la grabación.
    screenRecorder.start();

}

public static void finalizaGrabacion(InternetExplorerDriver driver)throws Exception{

    //Método para finalizar de la grabación.
    screenRecorder.stop();

I run any other class, calling these methods with these lines:

Video.iniciaGrabacion((InternetExplorerDriver)driver);

For close:

Video.finalizaGrabacion((InternetExplorerDriver)driver);

But i'm facing the next issues:

java.lang.NullPointerException
at cl.chilito.util.report.Video.iniciaGrabacion(Video.java:63)
at cl.chile.automatizacion.casos.prestamos.PrestamoNuevoK.PrestamoNuevoK(PrestamoNuevoK.java:97)
at cl.chile.automatizacion.casos.prestamos.PrestamoNuevoK.testPrestamoNuevoK(PrestamoNuevoK.java:78)

Could anybody help me with this?


Solution

  • public static void startRecording() throws Exception
        {
    
        File file = new File("C:\\videos");
    
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int width = screenSize.width;
        int height = screenSize.height;
    
        Rectangle captureSize = new Rectangle(0,0, width, height);
    
    
        GraphicsConfiguration gc = GraphicsEnvironment
                .getLocalGraphicsEnvironment()
                .getDefaultScreenDevice()
                .getDefaultConfiguration();
    
    
        screenRecorder = new VideoRecorder(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, "Video"); //Video puede ser cambiado al nombre que deseen
        screenRecorder.start();
    }
    

    And the stop method:

    public static void stopRecording() throws Exception
        {
            screenRecorder.stop();
        }
    

    Calling from an external class:

    Classname.startRecording(); Classname.stopRecording();

    Works fine.