Index: clients/java/src/main/java/com/thoughtworks/selenium/HttpCommandProcessor.java =================================================================== --- clients/java/src/main/java/com/thoughtworks/selenium/HttpCommandProcessor.java (revision 2103) +++ clients/java/src/main/java/com/thoughtworks/selenium/HttpCommandProcessor.java (working copy) @@ -17,6 +17,7 @@ package com.thoughtworks.selenium; +import java.awt.image.BufferedImage; import java.io.*; import java.net.HttpURLConnection; import java.net.MalformedURLException; @@ -25,6 +26,8 @@ import java.text.ParseException; import java.util.ArrayList; +import javax.imageio.ImageIO; + /** * Sends commands and retrieves results via HTTP. * @author Ben Griffiths, Jez Humble @@ -150,6 +153,33 @@ sessionId = null; } + /** Send capture command to the bridge servlet + * + * @param fileType - e.g. "PNG", "JPEG", or "BMP" + * @return - a screen shot as buffered image + */ + public BufferedImage getScreenShot(String fileType) { + String[] args = new String[] {fileType}; + DefaultRemoteCommand command = new DefaultRemoteCommand("getScreenshot", args); + InputStream inputStream = null; + try { + inputStream = getCommandResponse(command.getCommandURLString(), inputStream); + return ImageIO.read(inputStream); + } catch (IOException e) { + System.err.println("WARNING: caught IOExcetion processing doScreenShot(" + fileType + ")"); + return null; + } finally { + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException e) { + System.err.println("WARNING: failed to close InputStream processing doScreenShot(" + fileType + ")"); + e.printStackTrace(); + } + } + } + } + public String getString(String commandName, String[] args) { String result = doCommand(commandName, args); if (result.length() >= "OK,".length()) { Index: server-coreless/src/main/java/org/openqa/selenium/server/SeleniumDriverResourceHandler.java =================================================================== --- server-coreless/src/main/java/org/openqa/selenium/server/SeleniumDriverResourceHandler.java (revision 2103) +++ server-coreless/src/main/java/org/openqa/selenium/server/SeleniumDriverResourceHandler.java (working copy) @@ -437,6 +437,15 @@ log.error("Problem capturing screenshot", e); results = "ERROR: Problem capturing screenshot: " + e.getMessage(); } + } else if ("getScreenshot".equals(cmd)) { + try { + res.setContentType("image/" + values.get(0)); + getScreenshot(values.get(0), res); + return null; + } catch (Exception e) { + log.error("Problem get screenshot", e); + results = "ERROR: Problem get screenshot: " + e.getMessage(); + } } else if ("keyDownNative".equals(cmd)) { try { RobotRetriever.getRobot().keyPress(Integer.parseInt(values.get(0))); @@ -590,15 +599,25 @@ } } - private void captureScreenshot(String fileName) throws AWTException, IOException, InterruptedException, ExecutionException, TimeoutException { + private BufferedImage doScreenshot() throws AWTException, IOException, InterruptedException, ExecutionException, TimeoutException { Robot robot = RobotRetriever.getRobot(); Rectangle captureSize = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); - BufferedImage bufferedImage = robot.createScreenCapture(captureSize); + return robot.createScreenCapture(captureSize); + } + + private void captureScreenshot(String fileName) throws AWTException, IOException, InterruptedException, ExecutionException, TimeoutException { + BufferedImage bufferedImage = doScreenshot(); File outFile = new File(fileName); ImageIO.write(bufferedImage, "png", outFile); } + private void getScreenshot(String formatName, HttpResponse res) throws AWTException, IOException, InterruptedException, ExecutionException, TimeoutException { + // TODO maybe implement null- and empty string check to formatName + BufferedImage bufferedImage = doScreenshot(); + ImageIO.write(bufferedImage, formatName, res.getOutputStream()); + } + private void shutDown(HttpResponse res) { log.info("Shutdown command received");