Index: clients/java/iedoc2java.xml =================================================================== --- clients/java/iedoc2java.xml (revision 2108) +++ clients/java/iedoc2java.xml (working copy) @@ -66,10 +66,18 @@ package com.thoughtworks.selenium;&nl; + import java.awt.image.BufferedImage;&nl; /**&nl; */&nl;public interface Selenium { + /** + * Captures a screenshot and sends it back to the client. + * + * @param fileType whatever type of image is supported by the current jvm, e.g. "png", "jpeg" or "bmp" + */ + public BufferedImage getScreenshot(String fileType); + /** Launches the browser with a new Selenium session */ void start(); @@ -80,6 +88,7 @@ package com.thoughtworks.selenium;&nl; + import java.awt.image.BufferedImage;&nl; end users will primarily interact with this object. */ public class DefaultSelenium implements Selenium { @@ -124,6 +133,15 @@ this.commandProcessor = processor; } + /** + * Captures a screenshot and sends it back to the client. + * + * @param fileType whatever type of image is supported by the current jvm, e.g. "png", "jpeg" or "bmp" + */ + public BufferedImage getScreenshot(String fileType) { + return this.commandProcessor.getScreenshot(fileType); + } + public void start() { try { commandProcessor.start(); Index: clients/java/src/main/java/com/thoughtworks/selenium/CommandProcessor.java =================================================================== --- clients/java/src/main/java/com/thoughtworks/selenium/CommandProcessor.java (revision 2108) +++ clients/java/src/main/java/com/thoughtworks/selenium/CommandProcessor.java (working copy) @@ -17,6 +17,8 @@ package com.thoughtworks.selenium; +import java.awt.image.BufferedImage; + /** *

Provides a doCommand method, which sends the command to the browser * to be performed.

@@ -37,6 +39,13 @@ */ String doCommand(String command, String[] args); + /** Send capture command to the remote + * + * @param fileType - e.g. "PNG", "JPEG", or "BMP" + * @return - a screen shot as buffered image + */ + public BufferedImage getScreenshot(String fileType); + /** Starts a new Selenium testing session */ public void start(); Index: clients/java/src/main/java/com/thoughtworks/selenium/HttpCommandProcessor.java =================================================================== --- clients/java/src/main/java/com/thoughtworks/selenium/HttpCommandProcessor.java (revision 2108) +++ clients/java/src/main/java/com/thoughtworks/selenium/HttpCommandProcessor.java (working copy) @@ -17,6 +17,8 @@ package com.thoughtworks.selenium; +import java.awt.image.BufferedImage; +import javax.imageio.ImageIO; import java.io.*; import java.net.HttpURLConnection; import java.net.MalformedURLException; @@ -104,6 +106,32 @@ return sb.toString(); } + /** 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 ioExc) { + // TODO: maybe throwing an exception here is better + return null; + } finally { + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException ioExc) { + ioExc.printStackTrace(); + } + } + } + } + private InputStream getCommandResponse(String command, InputStream is) throws IOException { int responsecode = HttpURLConnection.HTTP_MOVED_PERM; while (responsecode == HttpURLConnection.HTTP_MOVED_PERM) { Index: server-coreless/src/main/java/org/openqa/selenium/server/SeleniumDriverResourceHandler.java =================================================================== --- server-coreless/src/main/java/org/openqa/selenium/server/SeleniumDriverResourceHandler.java (revision 2108) +++ 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); + results = "OK"; + } 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");