InterviewSolution
Saved Bookmarks
| 1. |
Can you capture a screenshot using Selenium? If yes, write a simple code to illustrate the same. |
|
Answer» Yes, using a web driver in Selenium, we can CAPTURE the SCREENSHOT. Following is the code to do the same: import org.junit.After;import org.junit.Before;import org.junit.Test;import java.io.File;import java.io.IOException;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;public CLASS TakeScreenshot {WebDriver drv; @ Before public void SETUP() throws Exception { driver = new FirefoxDriver(); drv.get("https://google.com");} @ After public void tearDown() throws Exception { drv.quit(); } @ Test public void test() throws IOException { // Capture the screenshot File scrFile = ((TakeScreenshot)drv).getScreenshotAs(OutputType.FILE); // Code for PASTING screenshot to a user-specified location FileUtils.copyFile(scrFile, new File("C:\\Screenshot\\Scr.jpg")) }} |
|