Following code will work like charm in Selenium
public void takeScreenshot(String Name)
{
try
{
RemoteWebDriver augmentedDriver =(RemoteWebDriver) new Augmenter().augment(driver);
File screenshot = ((TakesScreenshot)augmentedDriver).getScreenshotAs(OutputType.FILE);
new File(WD_Components.outputDirectory+"//ScreenShot").mkdir();
FileUtils.copyFile(screenshot, new File(WD_Components.outputDirectory+"//ScreenShot//"+Name+".jpg"));
setLog("INFO", "Successfully taken the screenshot");
}
catch (Exception e)
{
// TODO: handle exception
setLog("ERROR", "Not taken the screenshot"+e);
}
}
Augment()
//Enhance the interfaces implemented by an instance of the
RemoteWebDriver based on the returned Capabilities of the driver. Note: this class is still experimental. Use at your own risk.Below code will help to capture the screenshot of specific element in Selenium(Partial)
public class Shooter{
private WebDriver driver;
public void shootWebElement(WebElement element) throws IOException {
File screen = ((TakesScreenshot) this.driver).getScreenshotAs(OutputType.FILE);
Point p = element.getLocation();
int width = element.getSize().getWidth();
int height = element.getSize().getHeight();
BufferedImage img = null;
img = ImageIO.read(screen);
BufferedImage dest = img.getSubimage(p.getX(), p.getY(), width,
height);
ImageIO.write(dest, "png", screen);
File f = null;
f = new File("S:\\ome\\where\\over\\the\\rainbow");
FileUtils.copyFile(screen, f);
}
}
It will Maximize the window and take the screenshot
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));
Below code will help to reduce the image file size using Java
In addition to scaling the image per Hovercraft Full of Eels answer, you can experiment with setting the jpeg quality to something less than the default:
Iterator iter = ImageIO.getImageWritersByFormatName("jpeg");
ImageWriter writer = (ImageWriter)iter.next();
ImageWriteParam iwp = writer.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(0.5); // integer between 0 and 1
Also, depending on the type of screenshots you are taking, you might find some file size reduction by using a different image file format based on palettes, such as PNG(8-bit) or GIF. These formats can use less file size as compared to jpeg when the image contains limited set of colors that occur in frequent blocks of the same color. ...like many traditional GUI application screenshots.
Nice article..Really useful this post..Thanks for shared..Keep update.. software testing training with job guarantee
ReplyDeleteselenium training in chennai
selenium training in velachery
This comment has been removed by the author.
ReplyDelete