2017-06-22 17 views
2

我写了一个java方法,其返回类型是file。此方法使用Ashot抓取屏幕截图并将其存储在屏幕截图对象上。我需要将截图对象转换为文件对象,以便我可以返回文件对象。如何将屏幕截图对象转换为Ashot硒中的文件对象

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; 
} 
+0

你能告诉我们你的代码? –

回答

1

这应该做的伎俩

// 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()); 
+0

谢谢Sighil ....它的工作.. – dgsanket

+0

@dgsanket ..如果它有帮助,请接受答案。 – Sighil