Captura de pantalla/ScreenShot en Selenium
Se utiliza una captura de pantalla o screenshot en Selenium Webdriver para el análisis de errores o también como evidencia de ejecución. Selenium webdriver puede tomar automáticamente capturas de pantalla/screenshot durante la ejecución. Pero si los usuarios necesitan capturar una captura de pantalla por su cuenta, deben usar el método TakeScreenshot que notifica a WebDrive que tome la captura de pantalla y la almacene en Selenium.

Cómo tomar una captura de pantalla en Selenium
Aquí hay un proceso paso a paso sobre cómo capturar una captura de pantalla en Selenium WebDriver
Paso 1) Convierta el objeto WebDriver en TakeScreenshot
TakesScreenshot scrShot =((TakesScreenshot)webdriver);
Paso 2) Llame al método getScreenshotAs para crear un archivo de imagen
Archivo SrcFile=scrShot.getScreenshotAs(OutputType.FILE);
Paso 3) Copie el archivo a la ubicación deseada
Ejemplo: en este ejemplo, tomaremos una captura de pantalla https://www.tutorialselenium.com/
y la guardaremos en la ruta C:/Test.png
Aquí está el código de captura de pantalla en selenio:
package Guru99TakeScreenshot; import java.io.File; 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.firefox.FirefoxDriver; import org.testng.annotations.Test; public class SeleniumTakeScreenshot { @Test public void testSeleniumTakeScreenShot() throws Exception{ WebDriver driver ; System.setProperty("webdriver.gecko.driver","C:\\geckodriver.exe"); driver = new FirefoxDriver(); //Ir a la url driver.get("https://www.tutorialselenium.com/"); //Llamar a la función take screenshot this.takeSnapShot(driver, "c://test.png") ; } /** * Esta función hará una captura de pantalla * @param webdriver * @param fileWithPath * @throws Exception */ public static void takeSnapShot(WebDriver webdriver,String fileWithPath) throws Exception{ //Convertir el objeto WebDriver en TakeScreenshot TakesScreenshot scrShot =((TakesScreenshot)webdriver); //Llamar al método getScreenshotAs method para crear el archivo de imagen File SrcFile=scrShot.getScreenshotAs(OutputType.FILE); //Mover el archivo de imagen a la nueva ruta de destino File DestFile=new File(fileWithPath); //Copiar el archivo en el destino FileUtils.copyFile(SrcFile, DestFile); } }