Take a screenshot with webdriver

selenium

Now we want to take a screenshot of our webpage.
So first we need to open the browser and then take a snap and save it to our local.
like –

package com.webdriver_01;

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;

public class MyScreenshot {
	  
	public WebDriver driver;
	private String baseUrl;
	
	public static void main(String[] args){
		MyScreenshot ms = new MyScreenshot();
		ms.launch_browser();
	}
	public void launch_browser() {
		try{
		 driver = new FirefoxDriver();
		 baseUrl = "http://www.google.co.in";
		 driver.get(baseUrl);
		 driver.manage().window().maximize();
		 System.out.println("Open " + baseUrl);
		 File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
		 FileUtils.copyFile(scrFile, new File("D:\\screenshot1.png"));
		 driver.quit();
		}
		catch (Exception ex)
		{
			ex.getMessage();
		}
	}
  }

Related posts:

Leave a Reply

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