03
Dec

Screenshots with Testng reports

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.apache.commons.io.FileUtils;
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.testng.Reporter;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

public class TestNGDefaultReport {
	static WebDriver driver;

	@BeforeSuite
	public void setup() {
		System.setProperty("webdriver.chrome.driver", "C:\\Browserdrivers_Selenium\\chromedriver.exe");

		System.setProperty("org.uncommons.reportng.escape-output", "false");
		driver = new ChromeDriver();
	}

	@BeforeMethod
	public void beforeEachMethod() {
		driver.get("http://google.com");
	}

	//Test case 1
	@Test
	public void cars() throws Exception {
		System.out.println("I am Test method and I am searching for cars");
		driver.findElement(By.name("q")).sendKeys("Cars");

		//Wait for the results to appear
		Thread.sleep(2000);
		takeScreenshot();

	}

	@AfterSuite
	public void endOfSuite() {
		System.out.println("I am the end of suite");
		driver.quit();
	}

	public static void takeScreenshot() throws Exception {
		String timeStamp;
		File screenShotName;
		File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
		//The below method will save the screen shot in d drive with name "screenshot.png"
		timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
		screenShotName = new File("C:\\" + timeStamp + ".png");
		FileUtils.copyFile(scrFile, screenShotName);

		String filePath = screenShotName.getAbsolutePath().toString();
		System.out.println(filePath);

		String path = "<img src=\"" + filePath + "\" width=200 height=200/>";
		System.out.println("Screenshot here" + path);
		Reporter.log("Screenshot here" + path);

	}
}