1

I am automating tests of a GUI application similar to calculator. I am using Java and AutoIt for implementing the test cases. I need to take screenshot on failure.

I have tried taking screenshot using BufferedImage, but it takes the screenshot of the complete page. I need to take the screenshot of the application excluding the background. For Ex:- like taking snapshot using WebDriver. Is is possible to achieve this?

  • I removed the Selenium tags, because your question states you are not using it and Selenium cannot take screenshots of the full application. Are you also using Selenium and are you testing a web-application? Its not very clear what you are doing and why you want to take a screenshot of the windows. Adding this information might help get you better answers. – Niels van Reijmersdal Mar 25 '16 at 8:53
0

Since you are using AutoIt you can use the _ScreenCapture_CaptureWnd function to take a screenshot of a single window, example code can be found here: https://www.autoitscript.com/autoit3/docs/libfunctions/_ScreenCapture_CaptureWnd.htm

Since you are using Java you can also use Robot to take a screenshot of a part of the screen, but you need to specify the area manual. For some code example look here: https://stackoverflow.com/questions/58305/is-there-a-way-to-take-a-screenshot-using-java-and-save-it-to-some-sort-of-image

| improve this answer | |
2

Java code to take screenshot with below flexibility

  1. Take screenshot of only active window (not whole desktop, however can be easily modified to do so.)
  2. Can be used to save as popular image extensions .jpg , .png etc.
  3. Finally exports image as byte[] to be used to attach it in HTML reports etc.

import java.awt.AWTException;
import java.awt.Image;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Snapshot
{

    private Robot bot;
    private byte[] image;
    private Image img   = null;


    public byte[] getScreenshot() {
        try {
            return takeActiveWindowScreenshot();
        } catch (AWTException e) {
            e.printStackTrace();
        } catch (UnsupportedFlavorException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }


    public void obtainScreenshot()  {



    }

    public synchronized byte[] takeActiveWindowScreenshot() throws AWTException, UnsupportedFlavorException, IOException, InterruptedException {

        bot = new Robot();
        bot.keyPress(KeyEvent.VK_ALT);
        bot.keyPress(KeyEvent.VK_PRINTSCREEN);
        bot.keyRelease(KeyEvent.VK_ALT);
        bot.keyRelease(KeyEvent.VK_PRINTSCREEN);

        saveImageFromClipboard();
        convertImageToByteArray();
        return this.image;
    }


    public void saveImageFromClipboard () throws UnsupportedFlavorException, IOException, InterruptedException {

        int counter = 0;
        while (true)
        {

            if (counter == 5) break; 
            Thread.sleep(500);

            Transferable trans = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null); 
                   if (trans != null && trans.isDataFlavorSupported(DataFlavor.imageFlavor))
                   {
                       try  
                       {
                           img =  (Image) trans.getTransferData(DataFlavor.imageFlavor);    
                           trans = new StringSelection("");
                           Toolkit.getDefaultToolkit().getSystemClipboard().setContents(trans, null);
                           return;
                       }
                       catch (UnsupportedFlavorException e) {   e.printStackTrace();    }
                       catch (IOException e)    {   e.printStackTrace();    }

                   }
                   counter++;
        }   
        System.out.println("Snapshot couldn't be taken..");
    }



    public void convertImageToByteArray() throws IOException {

        BufferedImage bImage;
        if(img != null) {
        if (img instanceof BufferedImage)
        {
            bImage = (BufferedImage) img;
        }
        else
        {
        // Create a buffered image with transparency
            bImage  = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(bImage, "jpg", baos);
        image = baos.toByteArray();
        baos.close();
        return;
        }
        System.out.println("Screenshot can't be captured.");
    }



}
| improve this answer | |
-1

Yes, this is simple:

public void getscreenshot() throws Exception 
{
    File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    //The below method will save the screen shot in d drive with name "screenshot.png"
    FileUtils.copyFile(scrFile, new File("D:\\screenshot.png"));
} 

Use this method for taking a screenshot.

| improve this answer | |
  • This will screenshot the entire page. Not a good answer. – FDM Feb 24 '16 at 7:33
-1

Another option would be to augment the RemoteWebDriver to give it all of the interfaces its proxied driver implements:

WebDriver driver = new RemoteWebDriver(
    new URL("http://localhost:4444/wd/hub"), 
    DesiredCapabilities.firefox());

WebDriver augmentedDriver = new Augmenter().augment(driver); 
((TakesScreenshot)augmentedDriver).getScreenshotAs(...);
| improve this answer | |
  • This will screenshot the entire page. Not a good answer. – FDM Feb 24 '16 at 7:33
  • it will take the screenshot for entire page and also its applicable only for web based applications. The scenario here is the GUI application – Ajmal Feb 24 '16 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.