https://kotlinlang.org logo
Title
m

Michal Harakal

08/02/2021, 1:51 PM
I would like to take a screenshot in desktop app as a part of jvmTest. In androidTest I can do that with takeScreenshot method from uiautomator.UiDevice. Any hint how I can do that without spinning up an Android emulator (I have both targets …)?
b

Big Chungus

08/02/2021, 3:11 PM
There must be some swing way of taking screenshots. :google:
m

Michal Harakal

08/02/2021, 3:59 PM
Thanks, this seems to be viable direction, I was originally thinking and looking for something on lower level (skija buffer or so). Following code paints content of focused windows into image.
val panel = AppManager.focusedWindow!!.window
val bufImage =  BufferedImage(panel.getSize().width, panel.getSize().height,BufferedImage.TYPE_INT_RGB);
        panel.paint(bufImage.createGraphics());
        val imageFile = File("."+File.separator + "image.jpeg");
        try{
            imageFile.createNewFile();
            ImageIO.write(bufImage, "jpeg", imageFile);
        }catch(ex:Exception){
        }
Problem is a time point when to take the screenshot. I have tested with WindowEvents:onFocusGet but apparently windows is not composed yet, the image is empty. I have to figure out how to trigger screenshot after composition. I will go with cheap delay first ….
👍 1
i

Igor Demin

08/02/2021, 5:24 PM
If you need a screenshot only to test some Composable, you can use TestComposeWindow
:thank-you: 1
m

Michal Harakal

08/02/2021, 6:19 PM
Thank you @Igor Demin, this is exactly what I’ve needed, it works perfectly