How can I take a screenshot with Selenium WebDriver?

后端 未结 30 2591
不知归路
不知归路 2020-11-21 07:48

Is it possible to take a screenshot using Selenium WebDriver?

(Note: Not Selenium Remote Control)

30条回答
  •  北恋
    北恋 (楼主)
    2020-11-21 08:31

    Java

    Using RemoteWebDriver, after augmenting the Node with screenshot capability, I would store the screenshot like so:

    void takeScreenShotMethod(){
        try{
            Thread.sleep(10000);
            long id = Thread.currentThread().getId();
            BufferedImage image = new Robot().createScreenCapture(new Rectangle(
                Toolkit.getDefaultToolkit().getScreenSize()));
            ImageIO.write(image, "jpg", new File("./target/surefire-reports/"
                + id + "/screenshot.jpg"));
        }
        catch( Exception e ) {
            e.printStackTrace();
        }
    }
    

    You may use this method wherever required. Then, I assume you can customize the style sheet of maven-surefire-report-plugin at surefire-reports/html/custom.css so that your reports include the link to the correct screenshot for each test?

提交回复
热议问题