There are two way you can achieve this if you are using TestNG as you unit testing framework.
Refer the following blog post to see how to do this using TestNG.
http://qtp-help.blogspot.in/2010/07/testng-take-screenshot-of-failed-test.html
Below we will discuss how to do the same thing using WebDriver listeners. Webdriver has a WebDriverEventListener interface which you can implement and override the particular listener you want. WebDriverEventListener interface has onException method which is being called whenever a WebDriver exception occurs, So we will override this method and will write down the code to take screenshot in it.
WebDriverEventListenerDemo.java
you need to implement all the methods of the WebDriverEventListener class, here I haven't show that to make code looks small.
http://qtp-help.blogspot.in/2010/07/testng-take-screenshot-of-failed-test.html
Below we will discuss how to do the same thing using WebDriver listeners. Webdriver has a WebDriverEventListener interface which you can implement and override the particular listener you want. WebDriverEventListener interface has onException method which is being called whenever a WebDriver exception occurs, So we will override this method and will write down the code to take screenshot in it.
WebDriverEventListenerDemo.java
you need to implement all the methods of the WebDriverEventListener class, here I haven't show that to make code looks small.
import java.io.*; import org.apache.commons.io.FileUtils; import org.openqa.selenium.*; import org.openqa.selenium.support.events.WebDriverEventListener; /*** * Implements WebDriverEventListener and overrides onException method * @author Gaurang * */ public class WebDriverEventListenerDemo implements WebDriverEventListener { @Override public void onException(Throwable exception, WebDriver driver) { File screenShot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); try { FileUtils.copyFile(screenShot, new File("c:\\tmp\\exception.png")); } catch (IOException e) { e.printStackTrace(); } } }TestWithEvenRegistered.java
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.events.EventFiringWebDriver; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; /*** * Class registers the Event listern class we created * @author gshah * */ public class TestWithEvenRegistered { WebDriver driver = new FirefoxDriver(); EventFiringWebDriver eventFiringWebDriver; @BeforeClass public void setup() { eventFiringWebDriver = new EventFiringWebDriver(driver); eventFiringWebDriver.register(new WebDriverEventListenerDemo()); eventFiringWebDriver.get("http://google.com"); } /** * "qq" is wrong element id and so NoElementFound Exception will * be generated, which will call our overridden method of WebDriverEventListenerDemo * class and will take the screenshot. */ @Test public void test(){ eventFiringWebDriver.navigate().to("http://google.com"); eventFiringWebDriver.findElement(By.name("qq")); } }