簡體   English   中英

如何將截圖對象轉換為Ashot selenium中的文件對象

[英]How to convert screenshot object into file object in Ashot selenium

我寫了一個java方法,其返回類型是file。 此方法使用Ashot抓取屏幕截圖並將其存儲在Screenshot對象上。 我需要將該屏幕截圖對象轉換為文件對象,以便我可以返回文件對象。

   public static File grabScreenshot() {

    try {       

 Thread.sleep(Integer.parseInt(Property.getProperty("screenshotDelay")));

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

    } catch (NumberFormatException e) {
        e.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    }       

    File screenshot=null; //creating null file object to return

    Screenshot screenshot1 = new AShot().shootingStrategy(new ViewportPastingStrategy(1000)).takeScreenshot(driver());

    //Here I have to typecast the screenshot1 to file type so that I can return
    return screenshot;
}

這應該可以解決問題

// getImage() will give buffered image which can be used to write to file
BufferedImage bi = new AShot()
            .shootingStrategy(ShootingStrategies.viewportPasting(100))
            .takeScreenshot(driver).getImage();

File outputfile = new File("image.jpg");
try {
    ImageIO.write(bi, "jpg", outputfile);
} catch (IOException e) {
    e.printStackTrace();
}

// Print the absolute path to see where the file is created
System.out.println(outputfile.getAbsolutePath());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM