Interacting with the browser | Taking the screenshot of the page

In real life Test Automation projects we are generally required to take page screenshot periodically, especially if a step fails.

3 min read
  • WebDriver code for Taking Screenshot
  • Automating a sample test case

In the Previous Tutorial, we learned how to do cross-browser execution. In this tutorial, we'll learn how to take the page screenshot.

In real life Test Automation projects we are generally required to take page screenshot periodically, especially if a step fails. We can use the screenshot as steps to reproduce the failure.

WebDriver code for Taking Screenshot

import java.io.File;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.OutputType;
import org.apache.commons.io.FileUtils;

//Cast (convert) WebDriver to type 'TakesScreenshot' that is a Java Interface.
//Call its function 'getScreenshotAs' and pass output type as 'FILE' .
//Please be aware that at this stage the file format won't be an image type and it won't saves the file.
File fileScreenshot= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

//Copy the raw screenshot file to the local file system with the correct extension for image file type,e.g *.png
try {
 FileUtils.copyFile(fileScreenshot, new File("C://MyScreenshot.png"));
} catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
}

Automating a sample test case:

  • Navigate to the URL – https://cosmocode.io/wrong-url.html
  • If the field First Name is not present, take page screenshot and save it in the local machine.
  • If the field First Name is present, type some value to it.
  • Terminate the test.
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class ScriptClass {
 public static void main(String[] args) {
	
 System.setProperty("webdriver.chrome.driver", "C:\\teachmeselenium\\chromedriver.exe");
 WebDriver driver = new ChromeDriver();
 driver.manage().window().maximize();
 driver.get("https://cosmocode.io/wrong-url.html");
 //Try to find the element. If it does not exist, 'NoSuchElementException' will be thrown.
 try{
   WebElement eleFirstName = driver.findElement(By.id("firstname"));
   eleFirstName.sendKeys("This line wont be executed.");
 }catch(NoSuchElementException noEleEx){
 //Take screenshot as raw file type
 File fileScreenshot=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
  //Copy screenshot in local file system with *.png extension
  try{
   FileUtils.copyFile(fileScreenshot, new File("C://MyScreenshot.png"));
   }catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  driver.quit();
 } 
}

In the Next Tutorial, we will learn how to execute JavaScript using WebDriver.