CosmoCode (Formerly TeachMeSelenium)

Interacting with the browser | Taking the screenshot of the page

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();
}
driver.save_screenshot("C://my_screenshot.png")

Automating a sample test case:

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();
 } 
}
import org.openqa.selenium.NoSuchElementException
from selenium import webdriver
	
driver_path = "C:\\teachmeselenium\\chromedriver.exe")
driver = webdriver.Chrome(driver_path)
driver.get("https://cosmocode.io/wrong-url.html")
#Try to find the element. If it does not exist, 'NoSuchElementException' will be raised.
try:
    eleFirstName = driver.find_element_by_id("firstname")
    eleFirstName.sendKeys("This line wont be executed.");
except NoSuchElementException, e:
    #Take screenshot
    driver.save_screenshot("C://my_screenshot.png")
driver.quit()

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

Exit mobile version