Screenshots:
WebDriver Screenshot:
import java.io.File;
import java.io.FileOutputStream;
import java.util.concurrent.TimeUnit;
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 ScreenshotExample1 {
public static void main(String[] args) throws Exception {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.w3schools.com/");
File imageAsFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(imageAsFile, new File("/home/ranga/eclipse-workspace-testng/TestingPractice/screenshots/imageAsFile.jpg"));
byte[] imageAsBytes = ((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES);
FileOutputStream fos = new FileOutputStream("/home/ranga/eclipse-workspace-testng/TestingPractice/screenshots/imageAsBytes.jpg");
fos.write(imageAsBytes);
fos.close();
String imageAsBase64 = ((TakesScreenshot)driver).getScreenshotAs(OutputType.BASE64);
driver.quit();
}
}
WebElement Screenshot:
import java.io.File;
import java.io.FileOutputStream;
import java.util.concurrent.TimeUnit;
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 ScreenshotExample2 {
public static void main(String[] args) throws Exception {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.w3schools.com/");
WebElement searchBtn = driver.findElement(By.xpath("//form[@class='example']"));
File imageAsFile = ((TakesScreenshot)searchBtn).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(imageAsFile, new File("/home/ranga/eclipse-workspace-testng/TestingPractice/screenshots/imageAsFile.jpg"));
byte[] imageAsBytes = ((TakesScreenshot)searchBtn).getScreenshotAs(OutputType.BYTES);
FileOutputStream fos = new FileOutputStream("/home/ranga/eclipse-workspace-testng/TestingPractice/screenshots/imageAsBytes.jpg");
fos.write(imageAsBytes);
fos.close();
String imageAsBase64 = ((TakesScreenshot)searchBtn).getScreenshotAs(OutputType.BASE64);
driver.quit();
}
}
Full Page Screenshot:
Selenium does not support full page screenshots. We can use the Ashot library for full page screenshot.
import java.io.File;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;
public class ScreenshotExample3 {
public static void main(String[] args) throws Exception {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.w3schools.com/");
Screenshot fullPageScreeshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
ImageIO.write(fullPageScreeshot.getImage(), "jpg", new File("/home/ranga/eclipse-workspace-testng/TestingPractice/screenshots/fullPageScreeshot.jpg"));
driver.quit();
}
}