Take Screenshot of particular webelement without Using AShot - Bug Reaper

                  Bug Reaper

Lean about Automation Testing,Selenium WebDriver,RestAssured,Appium,Jenkins,JAVA,API Automation,TestNG,Maven, Rest API, SOAP API,Linux,Maven,Security Testing,Interview Questions

Tuesday 6 February 2018

Take Screenshot of particular webelement without Using AShot

Below is the Snippet That Can Help in taking Screenshot of the WebElement on the WebPage    

  System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");

        driver.get("https://www.facebook.com");
        driver.manage().window().maximize();
        Thread.sleep(6000);

        WebElement fbLogo = driver.findElement(By.xpath("//*[contains(@class,'fb_logo')]"));

        File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        BufferedImage  fullImg = ImageIO.read(screenshot);

        // Get the location of element on the page
        Point point = fbLogo.getLocation();

        // Get width and height of the element
        int eleWidth = fbLogo.getSize().getWidth();
        int eleHeight = fbLogo.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
        File screenshotLocation = new File("C:\\screenshots\\GoogleLogo_screenshot.png");
        FileUtils.copyFile(screenshot, screenshotLocation);

2 comments: