Road to capture clip of element locator in webdriver java

In this post I am going to show you how to capture clip of page element using webdriver.
Below I have written a “CaptureElementClip.java“java webdriver test script of a google application where I capture google menu clip and save into project.

package com.webdriver.test;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

public class CaptureElementClip {

        private WebDriver driver;
        private String baseUrl;

        @BeforeSuite
        public void setUp() throws Exception {
                    driver = new FirefoxDriver();
                    baseUrl = "http://google.com";
                    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
         }

        @Test
        public void testGoogle() throws IOException {

                    //open application url
                    driver.get(baseUrl);

                    //take screen shot
                    File screen = ((TakesScreenshot) driver)
                                        .getScreenshotAs(OutputType.FILE);
                                        
                    //get webelement object of google menu locator
                    WebElement googleMenu = driver.findElement(By.id("gbz"));
                    Point point = googleMenu.getLocation();

                    //get element dimension
                    int width = googleMenu.getSize().getWidth();
                    int height = googleMenu.getSize().getHeight();
                   
                    BufferedImage img = ImageIO.read(screen);
                    BufferedImage dest = img.getSubimage(point.getX(), point.getY(), width,
                                                                                 height);
                    ImageIO.write(dest, "png", screen);
                    File file = new File("Menu.png");
                    FileUtils.copyFile(screen, file);
          }

          @AfterSuite
          public void tearDown() throws Exception {
                    driver.quit();
           }
}

After executing above test a “Menu.png” file is generated in root folder of your project.

4 comments:

  1. I have a below questions could any one answer –
    1. I have three browsers in first browser that is in IE I have entered the data and submitted after submit popup window displayed in which some data is there so capture that data and save now in second Brower that is chrome enter data and submit after submit popup window displayed in which some data is there so capture that data. In in third browser the first two browsers data need to be entered.
    Could anyone write the script for this?

    2. How to handle windows using Java script for attaching a file in Gmail compose without using sikuli or AutoIt.

    ReplyDelete
  2. u should switch to pop up window then select the text..

    ReplyDelete
  3. You can use the Robot Class to handle the Attaching file in email.

    ReplyDelete

Leave your comments, queries, suggestion I will try to provide solution