I have wrote a Java Class which uses selenium webdriver to quickly pass through a website and test various functions. I have also wrote a seperate class which will be used to execute the takeScreenshot() method. Once the test hits the code which executes the screenshot method, the browser closes and the J-Unit test fails and points to the line where I am calling the takeScreenshot() method. It is a null pointer exception but I cant figure out whats going wrong.. I have read through many articles and cant find an answer. I have read all of the posts on here which identify how to take a screenshot using selenium... The code is below:
**** Screenshot Class ****
public class Screenshot {
private WebDriver webDriver;
File source;
public void takeScreenshot() {
try {
source = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(source, new File ("/Users/joshuadunn/Desktop/claimsScreenShot.png"));
System.out.println("Screenshot Taken!!!!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
I then create a Screenshot Object and execute its takeScreenshot() method as follows:
@Test
public void testClaimsTestToCalcNode() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.id("becsStartCalculator")).click();
driver.findElement(By.id("MARITAL_STATUS_false")).click();
driver.findElement(By.id("btn_next")).click();
driver.findElement(By.id("HOME_ABROAD_false")).click();
*** This is where the null pointer is ***
*******************************
screenshot.takeScreenshot();
*******************************
driver.findElement(By.id("btn_next")).click();
driver.findElement(By.id("DETAILS_STUDENT_YOU_false")).click();
I hope you understand my problem!
EDIT **** - This is not a duplicate.. I am totally aware of what a null pointer is and how it can be fixed. Obviously something in my code is coming through as null, but I dont know what... The screenshot below is the only error i get (stacktrace)
screenshot.takeScreenshot()
(in which case it's becausescreenshot
is null), or if it's intakeScreenshot()
(where there are a number of options for things that could be null).screenshot
element isnull
? Please provide stack trace.OSX
so it seems correct.