Cum să faceți o captură de ecran Selenium webdriver
⚡ Rezumat inteligent
Faceți o captură de ecran în Selenium WebDriver explică capturarea dovezilor vizuale pentru depanare și regresie vizuală, acoperind TakesScreenshot, capturi de pagină completă, capturi de elemente și AShot.

Captură de ecran în Selenium
A Captură de ecran în Selenium Webdriver este folosit pentru analiza erorilor. Selenium webdriver poate face automat capturi de ecran în timpul execuției. Dar dacă utilizatorii trebuie să captureze singuri o captură de ecran, trebuie să utilizeze metoda TakeScreenshot care notifică WebDrive să facă captură de ecran și să o stocheze în Selenium.
Cum să faceți o captură de ecran Selenium
Iată un proces pas cu pas despre cum să faceți captură de ecran în selenium WebDriver
Pas 1) Convertiți obiectul driverului web în TakeScreenshot
TakesScreenshot scrShot =((TakesScreenshot)webdriver);
Pas 2) Apelați metoda getScreenshotAs pentru a crea fișier imagine
File SrcFile=scrShot.getScreenshotAs(OutputType.FILE);
Pas 3) Copiați fișierul în Locația dorită
Exemplu: În acest exemplu vom face captură de ecran https://demo.guru99.com/V4/ & salvați-l ca C:/Test.png
Iată codul capturii de ecran în 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); } }
NOTĂ: Selenium versiunea 3.9.0 și mai sus nu oferă Apache Commons IO JAR. Puteți să le descărcați pur și simplu aici și numiți-i în proiectul dvs
Ce este API-ul Ashot?
Ashot este un utilitar terță parte de Yandex susținută de Selenium WebDriver pentru a captura capturi de ecran. Este nevoie de o captură de ecran a unui WebElement individual, precum și de o captură de ecran pe întreaga pagină a unei pagini, ceea ce este mai semnificativ decât dimensiunea ecranului.
Cum să descărcați și să configurați API-ul Ashot?
Există două metode de a configura API-ul Ashot
- Folosind Maven
- Manual, fără a utiliza niciun instrument
Pentru a configura prin Maven:
- Accesează https://mvnrepository.com/artifact/ru.yandex.qatools.ashot/ashot
- Faceți clic pe cea mai recentă versiune, deocamdată. Este 1.5.4
- Copiați codul de dependență și adăugați-l în fișierul pom.xml
- Salvați fișierul, iar Maven va adăuga borcanul în calea dvs. de construcție
- Și acum ești gata!!!
Pentru a configura manual fără niciun instrument de dependență
- Accesează https://mvnrepository.com/artifact/ru.yandex.qatools.ashot/ashot
- Faceți clic pe cea mai recentă versiune, deocamdată. Este 1.5.4
- Faceți clic pe borcan, descărcați-l și salvați-l pe aparat
- Adăugați fișierul jar în calea de construcție:
- In Eclipse, faceți clic dreapta pe proiect -> mergeți la proprietăți -> Build Path -> Libraries -> Add External jars
- Selectați fișierul jar
- Aplicați și închideți
Captură de ecran completă cu AShot API
Pas 1) Creați un obiect Ashot și apelați metoda takeScreenshot() dacă doriți doar captura de ecran pentru pagina cu dimensiunea ecranului.
Screenshot screenshot = new Ashot().takeScreenshot(driver);
Dar dacă doriți o captură de ecran a paginii mai mare decât dimensiunea ecranului, apelați metoda shootingStrategy() înainte de a apela metoda takeScreenshot() pentru a configura politica. Apoi apelați o metodă takeScreenshot() care trece webdriver-ul, de exemplu,
Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
Aici 1000 este derulat în milisecunde, așa că pentru a face o captură de ecran, programul va derula pentru fiecare 1000 msec.
Pasul 2): Acum, obțineți imaginea din captura de ecran și scrieți-o în fișier. Puteți furniza tipul de fișier ca jpg, png etc.
ImageIO.write(screenshot.getImage(), "jpg", new File(".\\screenshot\\fullimage.jpg"));
Realizarea unei capturi de ecran pe întreaga pagină a unei pagini care este mai mare decât dimensiunea ecranului.
Exemplu: Iată un exemplu de capturare a unei capturi de ecran pe toată pagina https://demo.guru99.com/test/guru99home/ și salvați în fișierul „screenshot.jpg”.
Datorită utilizării clasei ShootingStrategy a API-ului Ashot, vom putea captura o imagine completă a unei pagini mai mare decât dimensiunea ecranului.
Iată codul capturii de ecran din programul 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")); } }
Realizarea unei capturi de ecran a unui anumit element al paginii
Exemplu: Iată un exemplu de captură de ecran a unui element Guru Sigla 99 activată https://demo.guru99.com/test/guru99home/ pagina și salvați în fișierul „ElementScreenshot.jpg”.
Iată codul capturii de ecran în 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")); } }
Compararea imaginilor folosind 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() == true) { System.out.println("Images are same"); } else { System.out.println("Images are different"); } driver.quit(); } }



