Selenium: Capturing the screenshot
As an QA engineer, we all know that taking the screenshots is the main task we do during testing particular application. (It works as a 'Brahmastra' while fighting with developers and clients !!). Similarly, while running automation scripts it is necessary to take screenshots of important events and functionalities as it is imperative for bug analysis.
Selenium offers great advantage of taking automatic screenshots during the execution. We can convert our WebDriver instance to TakesScreenshot. Below we will see one example of taking the screenshots in our selenium automation script.
[source: www.qaautomated.com ] |
Example:
Here, simply we will try to open Google, will take the screenshot of that screen and we will store it to our local machine.
// importing the needed jar files
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import com.sun.jna.platform.FileUtils;
//creating class ScreenShot
@SuppressWarnings("unused")
public class ScreenShot {
//instantiating WebDriver object
WebDriver driver;
//main method
public void main(String[] args) throws InterruptedException, IOException {
//creating chrome driver instance
System.setProperty("webdriver.chrome.driver","C:\\Program Files\\chromedriver.exe");
driver=new ChromeDriver();
driver.get("https://www.google.com");
//including wait
Thread.sleep(1000);
driver.manage().window().maximize();
//performing type casting and converting WebDriver to TakesScreenshot instance
TakesScreenshot scrShot=((TakesScreenshot)driver);
//storing it as source image file
File scrFile =scrShot.getScreenshotAs(OutputType.FILE);
//providing the destination path on local machine in scrshot folder on desktop, image name can be anything but you need to provide it here
File destFile=new File("C:\\Users\\hp\\Desktop\\scrshot\\test1.png");
//it will copy the screenshot image from source to destination
FileUtils.copyFile(scrFile, destFile);
}
}
Cheers..!
Comments
Post a Comment