Ảnh chụp màn hình với Dấu thời gian trong Selenium Webdriver.
Ở đây chúng ta sẽ xem cách chụp ảnh màn hình. Chụp ảnh màn hình là một việc rất quan trọng vì đôi khi có lỗi trên các trang cũng như để lưu giữ bằng chứng về kết quả kiểm tra, chúng ta cần chụp ảnh màn hình của trường hợp kiểm tra đó.
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);Sau khi chụp ảnh màn hình, bạn cần phải di chuyển tệp này đến vị trí cụ thể.
Chúng tôi sẽ làm điều đó với sự trợ giúp của một trong các lớp được gọi là FileHandler.copyFIle (srcFile, destFile).
FileHandler.copy(scrFile, destFile); * scrFile là tệp ảnh chụp màn hình của bạn.
* destFile , bạn sẽ tạo nó bằng tệp mới và lưu trữ nó vào vị trí bạn muốn.
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileHandler.copy(screenshot, new File("/home/users/bhanu.pratap/Documents/testScreenshot/" + FileName));Đây là cách chúng tôi sẽ chụp ảnh màn hình. Nhưng trước khi tiếp tục, chúng ta phải chỉ định fileName.
Vì chúng tôi muốn mỗi khi một tệp mới được tạo.
Vì vậy, vì vậy, chúng tôi sẽ thực hiện tem thời gian như dưới đây.
Có một lớp trong java được gọi là Date, cho phép tạo một đối tượng của lớp này.
Date d = new Date();String FileName = d.toString().replace(":", "_").replace(" ", "_") + ".png";Vì vậy, bây giờ tên tệp sẽ khác nhau mỗi lần.
Do đó, mã hoàn chỉnh trông giống như
Date d = new Date();
String FileName = d.toString().replace(":", "_").replace(" ", "_") + ".png";
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileHandler.copy(screenshot, new File("/home/users/bhanu.pratap/Documents/testScreenshot/" + FileName));
}Nhưng thay vì viết cùng một đoạn mã tại sao không tạo một tiện ích cho việc này như bên dưới
package bagisto;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.io.FileHandler;
public class capturingScreenshot {
public static WebDriver driver;
private static void captureScreenshot() throws IOException {
Date d = new Date();
String FileName = d.toString().replace(":", "_").replace(" ", "_") + ".png";
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileHandler.copy(screenshot, new File("/home/users/bhanu.pratap/Documents/testScreenshot/" + FileName));
}
}
Lợi ích của việc này là bất cứ nơi nào chúng tôi muốn chụp ảnh màn hình thay vì đưa ra toàn bộ, chúng tôi sẽ chỉ sử dụng captureScreenshot ().
Đây là mã mẫu
package bagisto;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.io.FileHandler;
public class capturingScreenshot {
public static WebDriver driver;
private static void captureScreenshot() throws IOException {
Date d = new Date();
String FileName = d.toString().replace(":", "_").replace(" ", "_") + ".png";
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileHandler.copy(screenshot, new File("/home/users/bhanu.pratap/Documents/testScreenshot/" + FileName));
}
public static void main(String[] args) throws IOException {
System.setProperty("webdriver.chrome.driver",
"/home/users/bhanu.pratap/eclipse-workspace/bagisto/chromedriver.exe");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("https://bagisto.com/en/extensions/");
captureScreenshot();
driver.findElement(By.xpath("//li[@id='menu-item-22']//a[contains(text(),'Features')]")).click();
captureScreenshot();
}
}
Cảm ơn đã đọc blog này!
Chúc bạn thử nghiệm vui vẻ !!!
Nguồn xem thêm tại: https://webkul.com/