I would like to take a screenshot in desktop app a...
# compose-desktop
m
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
There must be some swing way of taking screenshots. google
m
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.
Copy code
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
If you need a screenshot only to test some Composable, you can use TestComposeWindow
🙏 1
m
Thank you @Igor Demin, this is exactly what I’ve needed, it works perfectly
265 Views