03
Dec

Capture screenshot to ClipBoard

import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.image.*;
import java.io.*;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class CopyImagetoClipboard implements ClipboardOwner {
	public CopyImagetoClipboard() {
		try {
			Robot robot = new Robot();
			Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
			Rectangle screen = new Rectangle(screenSize);
			BufferedImage i = robot.createScreenCapture(screen);
			TransferableImage trans = new TransferableImage(i);
			Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
			c.setContents(trans, this);
		} catch (AWTException x) {
			x.printStackTrace();
			System.exit(1);
		}
	}

	public static void main(String[] arg) throws InterruptedException {
		System.setProperty("webdriver.chrome.driver", "C:\\Browserdrivers_Selenium\\chromedriver.exe");

		WebDriver driver = new ChromeDriver();
		driver.manage().window().maximize();
		driver.get("http://defectracker.com/");

		CopyImagetoClipboard ci = new CopyImagetoClipboard();

		System.out.println("Screenshot in clipboard now.....");
		Thread.sleep(15000);
		driver.quit();
	}

	public void lostOwnership(Clipboard clip, Transferable trans) {
		System.out.println("Lost Clipboard Ownership");
	}

	private class TransferableImage implements Transferable {

		Image i;

		public TransferableImage(Image i) {
			this.i = i;
		}

		public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
			if (flavor.equals(DataFlavor.imageFlavor) && i != null) {
				return i;
			} else {
				throw new UnsupportedFlavorException(flavor);
			}
		}

		public DataFlavor[] getTransferDataFlavors() {
			DataFlavor[] flavors = new DataFlavor[1];
			flavors[0] = DataFlavor.imageFlavor;
			return flavors;
		}

		public boolean isDataFlavorSupported(DataFlavor flavor) {
			DataFlavor[] flavors = getTransferDataFlavors();
			for (int i = 0; i < flavors.length; i++) {
				if (flavor.equals(flavors[i])) {
					return true;
				}
			}

			return false;
		}
	}
}