Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

จะจำลองปุ่ม Print screen โดยใช้ selenium webdriver ใน Java ได้อย่างไร?


เราสามารถจำลองปุ่ม Print screen ด้วย Selenium ภาพหน้าจอถูกจับภาพด้วยปุ่มพิมพ์หน้าจอ การจับภาพหน้าจอเป็นกระบวนการสามวิธี เป็นก้าวสำคัญสู่การวิเคราะห์ความล้มเหลว

เราจะแปลงวัตถุไดรเวอร์เป็น TakeScreenshot อินเทอร์เฟซ

ไวยากรณ์

TakesScreenshot s = (TakesScreenshot)driver;

จากนั้นด้วย getScreenshotAs วิธีที่เราจะต้องมีไฟล์รูปภาพและคัดลอกไฟล์นั้นไปยังตำแหน่งที่มี FileUtils.copyFile วิธีการ

ไวยากรณ์

File sp=s.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(sp, new File("path of image file"));

ตัวอย่าง

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.apache.commons.io.FileUtils;
import java.io.File;
public class PrintScreenSimulate {
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver",
      "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.get("https://www.tutorialspoint.com/index.htm");
      // screenshot capturing
      File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
      FileUtils.copyFile(src, new File("logopage.png"));
      driver.quit();
   }
}