Friday, September 13, 2019

Post#40.Capturing Screenshot in Selenium Part-6 (Add Screenshot to PDF File)


Add Screenshot to PDF File

With selenium, we can take screenshot but only with the help of java we can store the screenshot in the format of the image file

Sometimes we may need to store the captured 
screenshot in a PDF file while running the selenium tests. But selenium doesn't facilitate this, so we have to take the help of libraries present in the Java language.


iText jar files provide the utility to take the screenshot and store the screenshot in PDF file.

To store the screenshot in PDF file, either we have to store it in a file system as an image after capturing the screenshot type as FILE, but that is a complex job.

So we would be storing the screenshot as bytes[] array and we would be passing it to the iText utility classes.
Complete code for Storing the image/screenshot in PDF file.

package pack6;
import java.io.FileOutputStream;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class GeneratePDF {
              public static void main(String[] args) throws Exception {

                             System.setProperty("webdriver.chrome.driver", "D:\\BrowserDrivers\\chromedriver.exe");
                             WebDriver driver = new ChromeDriver();
                             driver.manage().window().maximize();
                             driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
                             driver.get("https://www.google.co.in");

                             // take the screenshot and store it in byte[] array format
                             byte[] input = ((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES);
                             Document document = new Document();
                  String output = "E:\\ScreenshotFile.pdf";
                  FileOutputStream fos = new FileOutputStream(output);

                  // Instantiate the PDF writer
                  PdfWriter writer = PdfWriter.getInstance(document, fos);

                  // open the pdf for writing
                  writer.open();
                  document.open();

                  // process content into image
                  Image im = Image.getInstance(input);

                  //set the size of the image
                  im.scaleToFit(PageSize.A4.getWidth()/2, PageSize.A4.getHeight()/2);

                  // add the image to PDF
                  document.add(im);
                  document.add(new Paragraph(" "));

                  //close the files and write to local system
                  document.close();
                  writer.close();
                  driver.close();
    }
}
Scenario:
Navigate to application URL.
Capture all the links on home page and navigate to all links one by one. Along with this capture screenshots of every link (after navigation) and store all the screenshots in a pdf file.

Here I have created 2 classes. One is to Generate PDF and another is for navigating to every link and capturing screenshots. (Assuming all links will open on same window)

package pack4;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class GeneratePdf {
             
              public static void writeScreenShotToDocument(ArrayList<byte[]>  screenshots,ArrayList<String>  screenshotNames  ) throws Exception {

                            
                             // take the screenshot and store it in byte[] array format
                             //byte[] input = ((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES);
                             Document document = new Document();
                  String output = "E:\\ScreenshotFile.pdf";
                  FileOutputStream fos = new FileOutputStream(output);

                  // Instantiate the PDF writer
                  PdfWriter writer = PdfWriter.getInstance(document, fos);

                  // open the pdf for writing
                  writer.open();
                  document.open();

                  for(int i=0;i<screenshots.size();i++) {
                  // process content into image
                  Image im = Image.getInstance(screenshots.get(i));

                  //set the size of the image
                  im.scaleToFit(PageSize.A4.getWidth(), PageSize.A4.getHeight()/2);
                  //document.add(new Paragraph(" "));
                             document.add(new Paragraph(screenshotNames.get(i)));
                  // add the image to PDF
                  document.add(im);
                  document.add(new Paragraph(" "));
                  }

                  //close the files and write to local system
                  document.close();
                  writer.close();
                 
    }

}

package pack4;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class CaptureScreenshotsOfLinks {

              public static void main(String[] args) throws Exception {
                             // TODO Auto-generated method stub
                             ArrayList<byte[]>  screenshots = new ArrayList<byte[]>();
                             ArrayList<String>  screenshotNames = new ArrayList<String>();
                             System.setProperty("webdriver.chrome.driver", "D:\\BrowserDrivers\\chromedriver.exe");
                             ChromeOptions fo = new ChromeOptions();
                             // set the page load strategy
                             fo.setPageLoadStrategy(PageLoadStrategy.NORMAL);

                             ChromeDriver driver = new ChromeDriver(fo);
                             driver.manage().window().maximize();
                             driver.get("http://newtours.demoaut.com/");
                             List<WebElement> links = driver.findElements(By.tagName("a"));
                             System.out.println(links.size());
                             for(int i=0;i<links.size();i++)
                             {
                                           if(!links.get(i).getText().isEmpty())
                                           {
                                                          System.out.println(links.get(i).getText());
                                                          String linkname = links.get(i).getText();
                                                          try {
                                                          links.get(i).click();
                                           screenshots.add(((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES));
                                                          screenshotNames.add(linkname);
                                                         
                                                          driver.navigate().back();
                                                         links=driver.findElements(By.tagName("a"));
                                                          }catch(Exception e) {
                                                                       
                                                          }
                                           }
                             }
              GeneratePdf.writeScreenShotToDocument(screenshots,screenshotNames);
                            
              }

}

No comments:

Post a Comment