Cara Mengambil Screenshot di Selenium webdriver
⚡ Ringkasan Cerdas
Ambil Screenshot di Selenium WebDriver menjelaskan cara mengambil bukti visual untuk debugging dan regresi visual, meliputi TakesScreenshot, tangkapan layar halaman penuh, tangkapan elemen, dan AShot.

Tangkapan layar masuk Selenium
A Tangkapan layar masuk Selenium Driver web digunakan untuk analisis bug. Selenium WebDriver does not take screenshots on its own. To capture one, you cast the driver to the TakesScreenshot interface and call getScreenshotAs(). Test frameworks such as TestNG can call this automatically, for example from a listener when a test fails.
Cara Mengambil Screenshot di Selenium
Berikut adalah proses langkah demi langkah tentang cara mengambil tangkapan layar di Selenium WebDriver
Langkah 1) Konversikan objek web driver ke TakeScreenshot
TakesScreenshot scrShot =((TakesScreenshot)webdriver);
Langkah 2) Panggil metode getScreenshotAs untuk membuat file gambar.
File SrcFile=scrShot.getScreenshotAs(OutputType.FILE);
Langkah 3) Salin file ke Lokasi yang Diinginkan
Contoh: Pada contoh ini kita akan mengambil tangkapan layar dari https://demo.guru99.com/V4/ & menyimpannya sebagai C:/Test.png
Berikut adalah kode tangkapan layar dalam selenium:
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 Guru99TakeScreenshot {
@Test
public void testGuru99TakeScreenShot() throws Exception{
WebDriver driver ;
System.setProperty("webdriver.gecko.driver","C:\\geckodriver.exe");
driver = new FirefoxDriver();
//goto url
driver.get("https://demo.guru99.com/V4/");
//Call take screenshot function
this.takeSnapShot(driver, "c://test.png") ;
}
/**
* This function will take screenshot
* @param webdriver
* @param fileWithPath
* @throws Exception
*/
public static void takeSnapShot(WebDriver webdriver,String fileWithPath) throws Exception{
//Convert web driver object to TakeScreenshot
TakesScreenshot scrShot =((TakesScreenshot)webdriver);
//Call getScreenshotAs method to create image file
File SrcFile=scrShot.getScreenshotAs(OutputType.FILE);
//Move image file to new destination
File DestFile=new File(fileWithPath);
//Copy file at destination
FileUtils.copyFile(SrcFile, DestFile);
}
}
CATATAN: Selenium versi 3.9.0 ke atas tidak menyediakan Apache Commons IO JAR. Anda cukup mengunduhnya di sini dan panggil mereka di proyek Anda
Apa itu API Ashot?
Ashot adalah utilitas pihak ketiga oleh Yandex didukung oleh Selenium WebDriver untuk mengambil Tangkapan Layar. Dibutuhkan tangkapan layar dari WebElement individual serta tangkapan layar satu halaman penuh dari suatu halaman, yang lebih penting daripada ukuran layar.
Bagaimana cara mengunduh dan mengkonfigurasi Ashot API?
Ada dua metode untuk mengonfigurasi Ashot API
- Menggunakan Maven
- Secara manual tanpa menggunakan alat apapun
Untuk mengonfigurasi melalui Maven:
- Pergi ke https://mvnrepository.com/artifact/ru.yandex.qatools.ashot/ashot
- Klik pada versi terbaru, untuk saat ini. Ini adalah 1.5.4
- Salin kode Dependensi dan tambahkan ke file pom.xml Anda
- Simpan file, dan Maven akan menambahkan toples ke jalur build Anda
- Dan sekarang Anda siap!!!
Untuk mengonfigurasi secara manual tanpa alat ketergantungan apa pun
- Pergi ke https://mvnrepository.com/artifact/ru.yandex.qatools.ashot/ashot
- Klik pada versi terbaru, untuk saat ini. Ini adalah 1.5.4
- Klik pada toples, unduh dan simpan di mesin Anda
- Tambahkan file jar di jalur build Anda:
- In Eclipse, klik kanan pada proyek -> buka properti -> Build Path -> Libraries -> Add External jars
- Pilih file jarnya
- Terapkan dan Tutup
Tangkap Tangkapan Layar Halaman Penuh dengan Ashot API
Langkah 1) Buat objek Ashot dan panggil metode takeScreenshot() jika Anda hanya menginginkan tangkapan layar untuk halaman dengan ukuran layar tertentu.
Screenshot screenshot = new Ashot().takeScreenshot(driver);
Namun jika Anda ingin tangkapan layar laman lebih besar dari ukuran layar, panggil metode shootingStrategy() sebelum memanggil metode takeScreenshot() untuk menyiapkan kebijakan. Kemudian panggil metode takeScreenshot() yang meneruskan webdriver, misalnya,
Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
Di sini 1000 waktu digulir dalam milidetik, jadi untuk mengambil tangkapan layar, program akan menggulir untuk setiap 1000 mdetik.
Langkah 2): Sekarang, ambil gambar dari tangkapan layar dan tulis ke dalam file. Anda dapat menentukan tipe file sebagai jpg, png, dll.
ImageIO.write(screenshot.getImage(), "jpg", new File(".\\screenshot\\fullimage.jpg"));
Mengambil tangkapan layar satu halaman penuh dari halaman yang lebih besar dari ukuran layar.
Contoh: Berikut adalah contoh pengambilan tangkapan layar halaman penuh dari https://demo.guru99.com/test/guru99home/ dan menyimpannya ke file "screenshot.jpg."
Karena menggunakan kelas ShootingStrategy dari Ashot API, kami akan dapat menangkap gambar penuh dari halaman yang lebih besar dari ukuran layar.
Berikut ini adalah kode tangkapan layar dalam program Selenium:
package Guru99;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;
public class TestScreenshotUsingAshot {
public static void main(String[] args) throws IOException {
System.setProperty("webdriver.chrome.driver", "c:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://demo.guru99.com/test/guru99home/");
driver.manage().window().maximize();
Screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
ImageIO.write(screenshot.getImage(), "jpg", new File("c:\\ElementScreenshot.jpg"));
}
}
Mengambil tangkapan layar dari elemen halaman tertentu
Contoh: Berikut adalah contoh pengambilan tangkapan layar elemen. Guru logo 99 pada https://demo.guru99.com/test/guru99home/ Buka halaman dan simpan ke file "ElementScreenshot.jpg".
Berikut adalah kode tangkapan layar dalam selenium:
package Guru99;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;
public class TestElementScreenshotUsingAshot {
public static void main(String[] args) throws IOException {
System.setProperty("webdriver.chrome.driver", "c:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://demo.guru99.com/test/guru99home/");
driver.manage().window().maximize();
// Find the element to take a screenshot
WebElement element = driver.findElement(By.xpath ("//*[@id=\"site-name\"]/a[1]/img"));
// Along with driver pass element also in takeScreenshot() method.
Screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver,element);
ImageIO.write(screenshot.getImage(), "jpg", new File("c:\\ElementScreenshot.jpg"));
}
}
Perbandingan Gambar menggunakan Ashot
package Guru99;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.comparison.ImageDiff;
import ru.yandex.qatools.ashot.comparison.ImageDiffer;
public class TestImageComaprison {
public static void main(String[] args) throws IOException {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://demo.guru99.com/test/guru99home/");
// Find the element and take a screenshot
WebElement logoElement = driver.findElement(By.xpath("//*[@id=\"site-name\"]/a[1]/img"));
Screenshot logoElementScreenshot = new AShot().takeScreenshot(driver, logoElemnent);
// read the image to compare
BufferedImage expectedImage = ImageIO.read(new File("C:\\Guru99logo.png"));
BufferedImage actualImage = logoElementScreenshot.getImage();
// Create ImageDiffer object and call method makeDiff()
ImageDiffer imgDiff = new ImageDiffer();
ImageDiff diff = imgDiff.makeDiff(actualImage, expectedImage);
if (diff.hasDiff()) {
System.out.println("Images are different");
} else {
System.out.println("Images are same");
}
driver.quit();
}
}



