In this Selenium tutorial, you will learn how to take screenshots in Selenium WebDriver using Java. The tutorial includes real-time code examples to help you understand and implement screenshot capture in your automated tests effectively.

Different Ways to Take Screenshots in Selenium WebDriver Java

  • Using the TakesScreenshot Interface: Capture the entire webpage by casting the WebDriver instance to TakesScreenshot and calling the getScreenshotAs method.
  • Capturing Specific Section: Use WebDriver’s findElement method to locate the element of the specific section and then take a screenshot of that particular element using AShot or similar libraries.
  • Capturing Specific Elements: Use WebDriver’s findElement method to locate the element and then take a screenshot of that particular element using AShot or similar libraries.
  • Capturing the Entire Page: Utilize third-party libraries like AShot to capture the full page, including parts not visible on the screen.

Importance of Taking Screenshots in Selenium Java

  • Debugging: Screenshots help identify issues by visually highlighting where tests fail, aiding in debugging code.
  • Documentation: Captured screenshots serve as documentation, providing a visual record of test execution and results.
  • Verification: Screenshots validate that web elements and pages render correctly, ensuring proper functionality.
  • Reporting: Including screenshots in reports enhances clarity and understanding of test outcomes for stakeholders.
  • Analysis: Screenshots aid in analyzing UI changes, identifying regressions, and tracking application behavior over time.

How to take full screenshot in selenium java

package seleniumbasics;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ScreenshotGuide {

	public static void main(String[] args) throws InterruptedException, IOException {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub

				WebDriverManager.chromedriver().setup();

		        // Create an instance of the ChromeDriver
		        WebDriver driver = new ChromeDriver();
		        
		        //Delete all cookies.
		        driver.manage().deleteAllCookies();
		        
		        //Maximize the browser.
		        driver.manage().window().maximize();

		        // Navigate to a website
		        driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");
		        
		        Thread.sleep(2000);
		        
		        TakesScreenshot ts =  (TakesScreenshot) driver;
		        
		        
		        // Take the screenshot
		        File src = ts.getScreenshotAs(OutputType.FILE);
		        
		        // Save the screenshot to a desired location
		        File trg = new File(System.getProperty("user.dir")+"//Screenshots//"+"orangehrm.png");
		      
		        
		        FileUtils.copyFile(src, trg);
		        
		        
		        Thread.sleep(2000);
		        
		        driver.close();

	}

}

How to take screenshot of particular element in selenium java

import java.io.File;
import java.io.IOException;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
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;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ScrrenshotParticularSection {

	public static void main(String[] args) throws InterruptedException, IOException {
		// TODO Auto-generated method stub

				WebDriverManager.chromedriver().setup();

		        // Create an instance of the ChromeDriver
		        WebDriver driver = new ChromeDriver();
		        
		        //Delete all cookies.
		        driver.manage().deleteAllCookies();
		        
		        //Maximize the browser.
		        driver.manage().window().maximize();

		        // Navigate to a website
		        driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");
		        
		        Thread.sleep(3000);
		        
		        
		        driver.findElement(By.name("username")).sendKeys("Admin");
		        
		        
		        driver.findElement(By.name("password")).sendKeys("admin123");
		        
		        driver.findElement(By.xpath("/html/body/div/div[1]/div/div[1]/div/div[2]/div[2]/form/div[3]/button")).click();
		        
		      
		        
		        WebElement timeSction = driver.findElement(By.xpath("/html/body/div/div[1]/div[2]/div[2]/div/div[1]/div"));
		
		        //TakesScreenshot ts = (TakesScreenshot) driver;
		        
		        File src = timeSction.getScreenshotAs(OutputType.FILE);
		        
		        // Save the screenshot to a desired location
		        File trg = new File(System.getProperty("user.dir")+"//Screenshots//"+FileName+".png");
		        
		        FileUtils.copyFile(src, trg);
		        
		        Thread.sleep(2000);
		        
		        // Close the browser
		        driver.quit();
		
	}

}

By Avinash

Leave a Reply

Your email address will not be published. Required fields are marked *