Reading list Switch to dark mode

    Screenshot with Timestamp in Selenium Webdriver.

    Updated 13 January 2020

    Here we gonna see how to capture a screenshot. Capturing a screenshot is very important thing because at times there are failures on pages as well as to keep evidence of test result we need to take screenshot of that test case.


    Capturing screenshot is little tricky thing in Webdriver because there is no straight forward method that says driver.captureScreenshot.
    But there is an interface called as takesscreenshot which we will use along with one method named as getScreenshotAs().

    File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

    Once the screenshot is captured you need to move this file to particular location.
    We will be doing it with the help of one of class called as FileHandler.copyFIle(srcFile, destFile).

     FileHandler.copy(scrFile, destFile);

    *scrFile is your screenshot file.

    Start your headless eCommerce
    now.
    Find out More

    *destFile, you will be creating it with new file and store it into the location where you want.

    File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    FileHandler.copy(screenshot, new File("/home/users/bhanu.pratap/Documents/testScreenshot/" + FileName));

    This is how we will be capturing screenshot. But before moving ahead we have to specify fileName.

    Since we want every time a new file should be generated.

    So for that we will be implementing time stamp as bellow.

    There is class in java called as Date, lets create an object of this class.

    Date d = new Date();


    So it will return current system date along with time, but here we have to replace”:” and “space” because this thing is not supported in the file name. so for this we will convert this into string , then we will call replace method and concatenate .png .

    String FileName = d.toString().replace(":", "_").replace(" ", "_") + ".png";

    So now file name will be different every time.

    Hence complete code looks like

    		Date d = new Date();
    
    		String FileName = d.toString().replace(":", "_").replace(" ", "_") + ".png";
    
    		File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    
    		FileHandler.copy(screenshot, new File("/home/users/bhanu.pratap/Documents/testScreenshot/" + FileName));
    
    	}

    But instead of writing same code every time why not lets create a utility for this as below

    package bagisto;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.Date;
    import java.util.concurrent.TimeUnit;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.OutputType;
    import org.openqa.selenium.TakesScreenshot;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.io.FileHandler;
    
    public class capturingScreenshot {
    
    	public static WebDriver driver;
    
    	private static void captureScreenshot() throws IOException {
    
    		Date d = new Date();
    
    		String FileName = d.toString().replace(":", "_").replace(" ", "_") + ".png";
    
    		File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    
    		FileHandler.copy(screenshot, new File("/home/users/bhanu.pratap/Documents/testScreenshot/" + FileName));
    
    	}
    }

    The benefit of doing this is wherever we wanna capture screenshot instead of giving complete thing we will use only captureScreenshot().

    Here’s the Sample code

    package bagisto;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.Date;
    import java.util.concurrent.TimeUnit;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.OutputType;
    import org.openqa.selenium.TakesScreenshot;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.io.FileHandler;
    
    public class capturingScreenshot {
    
    	public static WebDriver driver;
    
    	private static void captureScreenshot() throws IOException {
    
    		Date d = new Date();
    
    		String FileName = d.toString().replace(":", "_").replace(" ", "_") + ".png";
    
    		File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    
    		FileHandler.copy(screenshot, new File("/home/users/bhanu.pratap/Documents/testScreenshot/" + FileName));
    
    	}
    
    	public static void main(String[] args) throws IOException {
    
    		System.setProperty("webdriver.chrome.driver",
    				"/home/users/bhanu.pratap/eclipse-workspace/bagisto/chromedriver.exe");
    		driver = new ChromeDriver();
    		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    		driver.manage().window().maximize();
    
    		driver.get("https://bagisto.com/en/extensions/");
    		captureScreenshot();
    		driver.findElement(By.xpath("//li[@id='menu-item-22']//a[contains(text(),'Features')]")).click();
    
    		captureScreenshot();
    
    	}
    
    }

    Thanks for reading this blog!

    Happy testing!!!

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    2 comments

  • simran
    • Bhanu (Moderator)
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home