स्क्रीनशॉट कैसे लें Selenium वेबड्राइवर
स्क्रीनशॉट में Selenium
A स्क्रीनशॉट में Selenium वेबड्राइवर बग विश्लेषण के लिए उपयोग किया जाता है। Selenium वेबड्राइवर निष्पादन के दौरान स्वचालित रूप से स्क्रीनशॉट ले सकता है। लेकिन अगर उपयोगकर्ताओं को अपने आप स्क्रीनशॉट लेने की आवश्यकता है, तो उन्हें TakeScreenshot विधि का उपयोग करने की आवश्यकता है जो स्क्रीनशॉट लेने और इसे स्टोर करने के लिए वेबड्राइव को सूचित करता है Selenium.
स्क्रीनशॉट कैसे लें Selenium
सेलेनियम वेबड्राइवर में स्क्रीनशॉट कैप्चर करने की चरण-दर-चरण प्रक्रिया यहां दी गई है
चरण 1) वेब ड्राइवर ऑब्जेक्ट को TakeScreenshot में बदलें
TakesScreenshot scrShot =((TakesScreenshot)webdriver);
चरण 2) छवि फ़ाइल बनाने के लिए getScreenshotAs विधि को कॉल करें
File SrcFile=scrShot.getScreenshotAs(OutputType.FILE);
चरण 3) फ़ाइल को इच्छित स्थान पर कॉपी करें
उदाहरण: इस उदाहरण में हम स्क्रीन कैप्चर लेंगे https://demo.guru99.com/V4/ & इसे 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("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);
}
}
नोट: Selenium संस्करण 3.9.0 और इसके बाद के संस्करण अपाचे कॉमन्स IO JAR प्रदान नहीं करते हैं। आप उन्हें आसानी से डाउनलोड कर सकते हैं यहाँ उत्पन्न करें और उन्हें अपने प्रोजेक्ट में कॉल करें
एशॉट एपीआई क्या है?
Ashot एक तृतीय पक्ष उपयोगिता है Yandex द्वारा समर्थित Selenium स्क्रीनशॉट कैप्चर करने के लिए WebDriver का उपयोग करें। यह एक व्यक्तिगत WebElement का स्क्रीनशॉट लेता है और साथ ही एक पेज का पूर्ण-पृष्ठ स्क्रीनशॉट भी लेता है, जो स्क्रीन आकार से अधिक महत्वपूर्ण है।
Ashot API को कैसे डाउनलोड और कॉन्फ़िगर करें?
Ashot API को कॉन्फ़िगर करने के दो तरीके हैं
- मावेन का उपयोग करना
- किसी भी उपकरण का उपयोग किए बिना मैन्युअल रूप से
मावेन के माध्यम से कॉन्फ़िगर करने के लिए:
- https://mvnrepository.com/artifact/ru.yandex.qatools.ashot/ashot
- अभी के लिए नवीनतम संस्करण पर क्लिक करें। यह 1.5.4 है
- निर्भरता कोड की प्रतिलिपि बनाएँ और इसे अपनी pom.xml फ़ाइल में जोड़ें
- फ़ाइल को सेव करें, और Maven आपके बिल्ड पथ में jar जोड़ देगा
- और अब आप तैयार हैं!!!
किसी भी निर्भरता उपकरण के बिना मैन्युअल रूप से कॉन्फ़िगर करने के लिए
- https://mvnrepository.com/artifact/ru.yandex.qatools.ashot/ashot
- अभी के लिए नवीनतम संस्करण पर क्लिक करें। यह 1.5.4 है
- जार पर क्लिक करें, डाउनलोड करें और इसे अपनी मशीन पर सेव करें
- अपने बिल्ड पथ में jar फ़ाइल जोड़ें:
- In Eclipse, प्रोजेक्ट पर राइट-क्लिक करें -> गुण पर जाएं -> बिल्ड पथ -> लाइब्रेरीज़ -> बाहरी जार जोड़ें
- jar फ़ाइल का चयन करें
- लागू करें और बंद करें
AShot API के साथ पूर्ण पृष्ठ स्क्रीनशॉट कैप्चर करें
चरण 1) यदि आप केवल स्क्रीन आकार वाले पृष्ठ के लिए स्क्रीनशॉट चाहते हैं तो Ashot ऑब्जेक्ट बनाएं और takeScreenshot() विधि को कॉल करें।
Screenshot screenshot = new Ashot().takeScreenshot(driver);
लेकिन अगर आप स्क्रीन साइज़ से बड़े पेज का स्क्रीनशॉट चाहते हैं, तो पॉलिसी सेट अप करने के लिए takeScreenshot() विधि को कॉल करने से पहले shootingStrategy() विधि को कॉल करें। फिर वेबड्राइवर को पास करते हुए takeScreenshot() विधि को कॉल करें, उदाहरण के लिए,
Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
यहां 1000 मिलीसेकंड में स्क्रॉल आउट समय है, इसलिए स्क्रीनशॉट लेने के लिए, प्रोग्राम प्रत्येक 1000 मिसे के लिए स्क्रॉल करेगा।
चरण 2): अब, स्क्रीनशॉट से छवि प्राप्त करें और इसे फ़ाइल में लिखें। आप फ़ाइल प्रकार jpg, png, आदि प्रदान कर सकते हैं।
ImageIO.write(screenshot.getImage(), "jpg", new File(".\\screenshot\\fullimage.jpg"));
किसी ऐसे पृष्ठ का पूर्ण-पृष्ठ स्क्रीनशॉट लेना जो स्क्रीन आकार से बड़ा हो।
उदाहरण: यहाँ एक पूर्ण पृष्ठ का स्क्रीनशॉट लेने का उदाहरण दिया गया है https://demo.guru99.com/test/guru99home/ और “screenshot.jpg” फ़ाइल में सहेजें।
एशॉट एपीआई के शूटिंगस्ट्रेटजी वर्ग का उपयोग करने के कारण, हम स्क्रीन आकार से बड़े पृष्ठ की पूरी छवि कैप्चर करने में सक्षम होंगे।
सेलेनियम प्रोग्राम में स्क्रीनशॉट कोड इस प्रकार है:
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"));
}
}
पृष्ठ के किसी विशेष तत्व का स्क्रीनशॉट लेना
उदाहरण: Here is the example of capturing element screenshot of Guru 99 logo on https://demo.guru99.com/test/guru99home/ पेज खोलें और “ElementScreenshot.jpg” फ़ाइल में सहेजें।
सेलेनियम में स्क्रीनशॉट कोड यहां दिया गया है:
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"));
}
}
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();
}
}
सारांश
- Ashot API एक फ्रीवेयर है Yandex.
- यह स्क्रीनशॉट लेने के लिए एक उपयोगिता है Selenium.
- यह आपको डेस्कटॉप ब्राउज़र, आईओएस सिम्युलेटर मोबाइल सफारी जैसे विभिन्न प्लेटफार्मों पर एक व्यक्तिगत वेबएलिमेंट का स्क्रीनशॉट लेने में मदद करता है। Android एम्यूलेटर ब्राउज़र.
- यह स्क्रीन आकार से बड़े पेज का स्क्रीनशॉट ले सकता है।
- यह सुविधा सेलेनियम संस्करण 3 में हटा दी गई है, इसलिए Ashot API एक अच्छा विकल्प है।
- यह स्क्रीनशॉट को सजा सकता है।
- यह एक स्क्रीनशॉट तुलना प्रदान करता है।



