1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
package TakeScreenshotReusable; 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.chrome.ChromeDriver; /** * * @author Sarang Holey * * 9:15:22 pm */ public class ScreenshotReusable { static WebDriver driver; public static void main(String[] args) { // Setting up browser properties System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); // creating object of ChromeDriver with the reference of WebDriver interface driver = new ChromeDriver(); // passing the required url driver.get("https://www.google.co.in"); // calling the re-usable function for taking screenshot of current page on browser takeScreenshot("google"); } //Reusbale function for capturing screenshot with unique filename /** * * @param fileName */ public static void takeScreenshot(String fileName) { // capturing screenshot as output type file from getScreenshotAs method by up casting the WebDriver reference to TakeScreenshot Interface File file = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); try { // Coping screenshot file into Screenshots folder with Filename from argument along with current time appended to file name FileUtils.copyFile(file, new File("../TakeScreenshot/src/test/resources/Screenshots/"+fileName+""+System.currentTimeMillis()+".png")); } catch (IOException e) { // catching the exception while file handling e.printStackTrace(); } } } |
Sarang , add code to take a full screen screen shot. This method will only take screens shots for HTML component of the GUI. What if I want to take screen shots for whole screen which should have URL and time stamp.
Also, what if I want to take screen shot of only specific section in the page and not the whole.