Partial screenshot / Highlight element before screenshot - Selenium WebDriver

If you are using selenium web-driver and you want to either highlight element while taking screenshots or take the screenshot of a particular element, here you go :

We can get the element screenshot by cropping entire page screenshot as below:

File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage  fullImg = ImageIO.read(screenshot);
//Get the location of element on the page
Point point = ele.getLocation();
//Get width and height of the element
int eleWidth = ele.getSize().getWidth();
int eleHeight = ele.getSize().getHeight();
//Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(), eleWidth,
    eleHeight);
ImageIO.write(eleScreenshot, "png", screenshot);
//Copy the element screenshot to disk
FileUtils.copyFile(screenshot, new File("c:\\partial.png"))
Highlight before screenshot:

JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("arguments[0].setAttribute('style', arguments[1]);",f, "color: yellow; border: 5px solid red;"); File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

Just Highlight:

for (int i = 0; i < 30; i++) { 
JavascriptExecutor javascript = (JavascriptExecutor) driver;
javascript.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, "color: orange; border: 5px solid orange;");
javascript.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, "color: pink; border: 5px solid pink;");
javascript.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, "color: yellow; border: 5px solid yellow;");
javascript.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, ""); 

Comments

Popular posts from this blog

Software Testing @ Microsoft

Trim / Remove spaces in Xpath?