Friday, September 13, 2019

Post#41.Capturing Screenshot in Selenium Part-7 (Converting to image from different OutputType)


Convert BASE64 to an image using convertFromBase64Png

We have inbuilt methods to convert the BASE64 string into an image, convertFromBase64Png used to convert the BASE64 string to an image file (image extension could be anything like png, jpg,tif...).

Complete program to convert BASE64 to an image


public class BASE64toImageInbuiltMethod {
              public static void main(String[] args) throws Exception {
                             // set the geckodriver.exe property
                             System.setProperty("webdriver.gecko.driver", "C:/PATH/geckodriver.exe");
                             WebDriver driver =new FirefoxDriver();
                             driver.manage().window().maximize();

                             // set time limit to find the element
                             driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

                             // Go to URL
                             driver.get("http://newtours.demoaut.com/");

                             // Take a ScreenShot
                             String scrBase64 = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BASE64);
                             // convert the BASE64 to File type
                             File file = OutputType.FILE.convertFromBase64Png(scrBase64);
                             // store the converted file as Image on D driver
                             FileUtils.copyFile(file, new File("D:\\BASE64-Conerted-Image.png"), true);
  }
}

Convert BYTES screenshot to an image in selenium Webdriver

We have inbuilt methods to convert the BYTES screenshot into an image, convertFromPngBytesused to convert the BYTES string to an image file(image extension could be anything like png, jpg,tif...).

Complete program to convert BYTES to an image

public class BytesToImage {
              public static void main(String[] args) throws Exception {
                             // set the geckodriver.exe property
                             System.setProperty("webdriver.gecko.driver", "C:/PATH/geckodriver.exe");
                             WebDriver driver =new FirefoxDriver();
                             driver.manage().window().maximize();

                             // set time limit to find the element
                             driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

                             // Go to URL
                             driver.get("http://newtours.demoaut.com/");

                             // Take a ScreenShot
                             byte[] scrBytes = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
                             // convert the Bytes to File type
                             File file = OutputType.FILE.convertFromPngBytes(scrBytes);
                             // store the converted file as Image on D driver
                             FileUtils.copyFile(file, new File("D:\\Bytes-Conerted-Image.png"), true);
  }
}

Convert OutputType.BASE64 to an image without using in-built Methods

We cannot directly convert BASE64 into an image; we have to take help of java image related classes to overcome this situation.

Complete program for Converting Screenshot OutputTYpe.BASE64 to an image.

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import org.apache.commons.codec.binary.Base64;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Base64ToImage {

  public static void main(String[] args) throws Exception {
                             // set the geckodriver.exe property
                             System.setProperty("webdriver.gecko.driver", "C:/PATH/geckodriver.exe");
                             WebDriver driver =new FirefoxDriver();
                             driver.manage().window().maximize();

                             // set time limit to find the element
                             driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

                             // Go to URL
                             driver.get("http://newtours.demoaut.com/");

                             // Take a ScreenShot
                             String scrBase64 = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BASE64);

                             // decode the base4 to bytes,
                             byte[] base64Val=Base64.decodeBase64(scrBase64);

                             //create an image file in local system
                             File imgFile = new File("D:\\pageScreenshotBase64.png");

                             // convert the bytes to an image using ImageIo class
                             BufferedImage img = ImageIO.read(new ByteArrayInputStream(base64Val));

                             // write the image to the image file that we have created
                             ImageIO.write(img, "png", imgFile);
  }
}

Convert Bytes Screenshot to an image in Selenium Webdriver without using in-built Methods

We can convert the OutputType.BYTES to an image, selenium doesn't provide any methods to convert perform this operation too. Again we have to Depend on Java Image processing classes.

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class ConvertBytesToImage {
              public static void main(String[] args) throws Exception {
                             // set the geckodriver.exe property
                             System.setProperty("webdriver.gecko.driver", "C:/PATH/geckodriver.exe");
                             WebDriver driver =new FirefoxDriver();
                             driver.manage().window().maximize();

                             // set time limit to find the element
                             driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

                             // Go to URL
                             driver.get("http://newtours.demoaut.com/");

                             // Take a ScreenShot
                             byte[] scrBytes = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);

                             //create an image file in local system
                             File imgFile = new File("D:\\pageScreenshotBytes.png");

                             // convert the bytes to an image using ImageIo class
                             BufferedImage img = ImageIO.read(new ByteArrayInputStream(scrBytes));

                             // write the image to the image file we have create
                             ImageIO.write(img, "png", imgFile);
  }
}

No comments:

Post a Comment