You are making a new reference for WebDriver
in your Screenshot
class. This WebDriver
is never instantiated, thus will give you a NullPointerException
. Instead you should pass the WebDriver
instance as a parameter to the method.
public class Screenshot {
File source;
public void takeScreenshot(WebDriver webDriver) {
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();
}
}
}
And the test case:
@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);
*******************************
driver.findElement(By.id("btn_next")).click();
driver.findElement(By.id("DETAILS_STUDENT_YOU_false")).click();
Edit: Alternatively you could set the WebDriver
in the constructor of Screenshot
public void Screenshot(WebDriver webDriver){
this.webDriver = webDriver;
}