Tuesday, 12 December 2017

Adding utility yo your selenium framework part-2

Screenshot [Full page]


package test.java.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebElement;

public class Screenshot extends DriverFactory {

	public static File captureElementBitmap(WebElement element) throws IOException {

		// Get the entire Screenshot from the driver of passed WebElement
		File screen = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

		// Create an instance of Buffered Image from captured screenshot
		// BufferedImage img = ImageIO.read(screen);

		// Get the Width and Height of the WebElement using
		int ImageWidth = element.getSize().getWidth();
		int ImageHeight = element.getSize().getHeight();

		// Used selenium Point class to get x y coordinates of Image element.
		// get location(x y coordinates) of the element.
		Point point = element.getLocation();
		int xcord = point.getX();
		int ycord = point.getY();

		// Reading full image screenshot.
		BufferedImage img = ImageIO.read(screen);

		// cut Image using height, width and x y coordinates parameters.
		BufferedImage dest = img.getSubimage(xcord, ycord, ImageWidth, ImageHeight);
		ImageIO.write(dest, "png", screen);

		return screen;
	}

	public static String captureFullScreenshot(String folder, String fileName) throws IOException {

		File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
		File targetFile = new File(folder, fileName + ".png");
			FileUtils.copyFile(screenshotFile, targetFile);
		return targetFile.getAbsolutePath();
	}

}
Screenshot [Web Element]

package test.java.util;

import java.awt.AWTException;
import java.awt.GraphicsConfiguration;
import java.awt.Rectangle;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.monte.media.Format;
import org.monte.media.Registry;
import org.monte.screenrecorder.ScreenRecorder;
 
public class SpecializedScreenRecorder extends ScreenRecorder {
 
    private String name;
 
    public SpecializedScreenRecorder(GraphicsConfiguration cfg,
           Rectangle captureArea, Format fileFormat, Format screenFormat,
           Format mouseFormat, Format audioFormat, File movieFolder,
           String name) throws IOException, AWTException {
         super(cfg, captureArea, fileFormat, screenFormat, mouseFormat,
                  audioFormat, movieFolder);
         this.name = name;
    }
 
    @Override
    protected File createMovieFile(Format fileFormat) throws IOException {
          if (!movieFolder.exists()) {
                movieFolder.mkdirs();
          } else if (!movieFolder.isDirectory()) {
                throw new IOException("\"" + movieFolder + "\" is not a directory.");
          }
                           
          SimpleDateFormat dateFormat = new SimpleDateFormat(
                   "yyyy-MM-dd HH.mm.ss");
                         
       return new File(movieFolder, name + "-" + dateFormat.format(new Date()) + "."
                  + Registry.getInstance().getExtension(fileFormat));
          
          // return new File(movieFolder, name +"."+ Registry.getInstance().getExtension(fileFormat));
    }
 }

No comments:

Post a Comment

Set up testlink with Selenium Framework

To Integrate Testlink with selenium add following jars in your setup testlink-api-client-2.0.jar xmlrpc-common-3.1.jar xmlrpc-client-...