Take ScreenShot sử dụng Selenium WebDriver
Screenshot trong Selenium Webdriver
Screenshot trong Selenium Webdriver được sử dụng để phân tích lỗi. Selenium webdriver có thể tự động Take ScreenShot trong quá trình thực thi. Nhưng nếu người dùng cần tự Take ScreenShot, họ cần sử dụng phương pháp TakeScreenshot thông báo cho WebDrive để Take ScreenShot và lưu trữ trong Selenium.
- HF là chất gì? HF là chất điện li mạnh hay yếu? Hóa học 11
- Tập trung hay tập chung đúng? 2 lý do dễ nhầm lẫn khi viết
- Hướng dẫn trồng trọt trong Minecraft
- Công văn 2153/BGDĐT-KHTC Xây dựng mức học phí và lộ trình tăng học phí phù hợp
- Tổng hợp Giftcode Hero AFK Vương Quyền Chiến VNG mới nhất tháng 6 năm 2022 và cách nhập code chính xác

Take ScreenShot bằng Selenium WebDriver
Take ScreenShot trong Selenium là một quy trình 3 bước
Bước 1) Chuyển đổi đối tượng trình điều khiển web thành TakeScreenshot
TakesScreenshot scrShot = ((TakesScreenshot) webdriver);
Bước 2) Gọi phương thức getScreenshotAs để tạo tệp hình ảnh
Tệp SrcFile = scrShot.getScreenshotAs (OutputType.FILE);
Bước 3) Sao chép tệp vào Vị trí mong muốn
Ví dụ: Trong ví dụ này, chúng tôi sẽ Take ScreenShot của http://demo.guru99.com/V4/ và lưu nó dưới dạng C: /Test.png
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(“http://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);
}
}
LƯU Ý: Phiên bản Selenium 3.9.0 trở lên không cung cấp Apache Commons IO JAR. Bạn chỉ cần tải chúng xuống tại đây và gọi chúng trong dự án của bạn
API Ashot là gì?
Ashot là một tiện ích bên thứ ba của Yandex được hỗ trợ bởi Selenium WebDriver để Take ScreenShot. Nó Take ScreenShot của một WebElement riêng lẻ cũng như Screenshot toàn trang của một trang, điều này quan trọng hơn kích thước màn hình.
Làm cách nào để tải xuống và định cấu hình Ashot API?
Có hai phương pháp để định cấu hình API Ashot
- 1. sử dụng Maven
- 2. thủ công mà không cần sử dụng bất kỳ công cụ nào
Để định cấu hình thông qua Maven:
- Tới https://mvnrepository.com/artifact/ru.yandex.qatools.ashot/ashot
- Nhấp vào phiên bản mới nhất, ngay bây giờ. Nó là 1.5.4
- Sao chép mã Phụ thuộc và thêm vào tệp pom.xml của bạn

- Lưu tệp và Maven sẽ thêm jar vào đường dẫn xây dựng của bạn
- Và bây giờ bạn đã sẵn sàng !!!
Để cấu hình thủ công mà không cần bất kỳ công cụ phụ thuộc nào
- Tới https://mvnrepository.com/artifact/ru.yandex.qatools.ashot/ashot
- Nhấp vào phiên bản mới nhất, ngay bây giờ. Nó là 1.5.4
- Nhấp vào bình, tải xuống và lưu trên máy của bạn

- Thêm tệp jar vào đường dẫn xây dựng của bạn:
- Trong Eclipse, nhấp chuột phải vào dự án -> chuyển đến thuộc tính -> Đường dẫn xây dựng -> Thư viện -> Thêm bình bên ngoài
- Chọn tệp jar
- Áp dụng và Đóng
Take ScreenShot toàn trang với API AShot
Bước 1) Tạo một đối tượng Ashot và gọi phương thức takeScreenshot () nếu bạn chỉ muốn Screenshot cho trang kích thước màn hình.
Screenshot screenshot = new Ashot().takeScreenshot(driver);
Nhưng nếu bạn muốn Screenshot của trang lớn hơn kích thước màn hình, hãy gọi phương thức shootingStrategy () trước khi gọi phương thức takeScreenshot () để thiết lập chính sách. Sau đó, gọi một phương thức takeScreenshot () chuyển qua webdriver, ví dụ:
Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
Ở đây 1000 là thời gian cuộn ra tính bằng mili giây, vì vậy để Take ScreenShot, chương trình sẽ cuộn trong mỗi 1000 mili giây.
Bước 2): Bây giờ, lấy hình ảnh từ Screenshot và ghi nó vào tệp. Bạn có thể cung cấp loại tệp là jpg, png, v.v.
ImageIO.write(screenshot.getImage(), “jpg”, new File(“.\\screenshot\\fullimage.jpg”));
Take ScreenShot toàn trang của một trang lớn hơn kích thước màn hình.
Ví dụ: Đây là ví dụ Take ScreenShot toàn trang của http://demo.guru99.com/test/guru99home/ và lưu vào tệp “screenshot.jpg”.
Do sử dụng lớp ShootingStrategy của Ashot API, chúng tôi sẽ có thể chụp toàn bộ hình ảnh của một trang lớn hơn kích thước màn hình. Đây là chương trình:
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(“http://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”));
}
}
Take ScreenShot của một phần tử cụ thể của trang
Ví dụ: Đây là ví dụ Take ScreenShot phần tử của logo Guru 99 trên trang http://demo.guru99.com/test/guru99home/ và lưu vào tệp “ElementScreenshot.jpg”. Đây là mã:
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(“http://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”));
}
}
So sánh hình ảnh bằng 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(“http://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() == true) {
System.out.println(“Images are same”);
} else {
System.out.println(“Images are different”);
}
driver.quit();
}
}
Bản tóm tắt
- Ashot API là một phần mềm miễn phí từ Yandex.
- Nó là một tiện ích để Take ScreenShot trong Selenium.
- Nó giúp bạn Take ScreenShot của từng WebElement trên các nền tảng khác nhau như trình duyệt máy tính để bàn, iOS Simulator Mobile Safari, Android Emulator Browser.
- Nó có thể Take ScreenShot trang của một trang lớn hơn kích thước màn hình.
- Tính năng này đã bị loại bỏ trong phiên bản 3 của selen, vì vậy Ashot API là một lựa chọn tốt.
- Nó có thể trang trí các Screenshot.
- Nó cung cấp một so sánh Screenshot.
Được thực hiện nhờ sự đóng góp của Shradhdha Dave