如何使用webDriver(java)截取屏幕截图并一一粘贴到word文件中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35877185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:41:43  来源:igfitidea点击:

How to take screenshots and paste it in a word file one by one using webDriver (java)

javaseleniumwebdriver

提问by Ayaz Hasan

I am able to take screenshot by ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

我可以通过 ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 截取屏幕截图。

In my application I have to take screenshot for every page so I want to save the multiple screenshot into a single .doc file one by one. Is there any API?

在我的应用程序中,我必须为每个页面截取屏幕截图,因此我想将多个屏幕截图一一保存到单个 .doc 文件中。有没有API?

Any Idea? Please Help...

任何的想法?请帮忙...

回答by Kapil

Easiest way - take screenshot, put it in PNG/JPEG file, read it, add it in MS-Word, delete the file, simple. here's a ready to use code for you.... BINGO...!!

最简单的方法 - 截取屏幕截图,将其放入 PNG/JPEG 文件中,读取它,将其添加到 MS-Word 中,删除文件,简单。这是一个可供您使用的代码......宾果游戏......!!

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 {

            XWPFDocument docx = new XWPFDocument();
            XWPFRun run = docx.createParagraph().createRun();
            FileOutputStream out = new FileOutputStream("d:/xyz/doc1.docx");

            for (int counter = 1; counter <= 5; counter++) {
                captureScreenShot(docx, run, out);
                TimeUnit.SECONDS.sleep(1);
            }

            docx.write(out);
            out.flush();
            out.close();
            docx.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void captureScreenShot(XWPFDocument docx, XWPFRun run, FileOutputStream out) throws Exception {

        String screenshot_name = System.currentTimeMillis() + ".png";
        BufferedImage image = new Robot()
                .createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
        File file = new File("d:/xyz/" + screenshot_name);
        ImageIO.write(image, "png", file);
        InputStream pic = new FileInputStream("d:/xyz/" + screenshot_name);
        run.addBreak();
        run.addPicture(pic, XWPFDocument.PICTURE_TYPE_PNG, screenshot_name, Units.toEMU(350), Units.toEMU(350));
        pic.close();
        file.delete();
    }

}

回答by Shubham Jain

Selenium Webdriver do not provide any feature to add snapshot in word file. For this you need to use third party libraries.

Selenium Webdriver 不提供任何在 word 文件中添加快照的功能。为此,您需要使用第三方库。

Refer below:-

参考以下:-

how to insert image into word document using java

如何使用java将图像插入到word文档中

How can I add an Image to MSWord document using Java

如何使用 Java 将图像添加到 MSWord 文档中

You can also add your image file in TestNG output file using reporter

您还可以使用记者在 TestNG 输出文件中添加您的图像文件

Refer below :-

参考以下:-

http://www.automationtesting.co.in/2010/07/testng-take-screenshot-of-failed-test.html

http://www.automationtesting.co.in/2010/07/testng-take-screenshot-of-failed-test.html

Hope it will help you :)

希望它会帮助你:)