This post explains how to capture or insert screenshots into a Microsoft word document easily with help of java code. Also you can integrate below line of java code in selenium framework to capture screen of various pages during test execution.
Screen capturing in word document :
This is all about java screenshot capturing solution. Thank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section.
Screen capturing in word document :
package demoPackage; import java.awt.Rectangle; import java.awt.Robot; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.util.concurrent.TimeUnit; import javax.imageio.ImageIO; import org.apache.poi.util.Units; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFRun; public class TakeScreenshots { public static void main(String[] args) { try { String dirPath = "d:/file/" ; //Create folder/directory if not exist. File file = new File(dirPath); if (!file.exists()) { if (file.mkdir()) { System.out.println("Directory is created!"); } else { System.out.println("Failed to create directory!"); } } XWPFDocument docx = new XWPFDocument(); XWPFRun run = docx.createParagraph().createRun(); FileOutputStream out = new FileOutputStream(dirPath+"doc1.docx"); // Add for loop for example, because here we are capturing 5 screenhots for (int counter = 1; counter <= 5; counter++) { captureScreenShot( run, out, dirPath); TimeUnit.SECONDS.sleep(1); } System.out.println("Write to doc file sucessfully..."); docx.write(out); out.flush(); out.close(); docx.close(); } catch (Exception e) { e.printStackTrace(); } } public static void captureScreenShot( XWPFRun run, FileOutputStream out, String dirPath) throws Exception { String screenshot_name = System.currentTimeMillis() + ".png"; BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize())); File file = new File(dirPath + screenshot_name); ImageIO.write(image, "png", file); InputStream pic = new FileInputStream(dirPath + screenshot_name); run.addBreak(); run.addPicture(pic, XWPFDocument.PICTURE_TYPE_PNG, screenshot_name, Units.toEMU(500), Units.toEMU(350)); pic.close(); file.delete(); } }
This is all about java screenshot capturing solution. Thank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section.
No comments:
Post a Comment