-2

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)

J-Unit error message

DeltaRage
  • 119
  • 1
  • 3
  • 16
  • Can you add the stack trace as code, please? – Tamas Rev Jun 28 '16 at 14:26
  • Ditto - unclear to me if the null pointer is **at** `screenshot.takeScreenshot()` (in which case it's because `screenshot` is null), or if it's **in** `takeScreenshot()` (where there are a number of options for things that could be null). – dcsohl Jun 28 '16 at 14:28
  • May be your `screenshot` element is `null` ? Please provide stack trace. – toro Jun 28 '16 at 14:29
  • @arizzle That's a filepath that's used on `OSX` so it seems correct. – RemcoW Jun 28 '16 at 14:37
  • @RemcoW Ah yes, you are right, my bad. – explv Jun 28 '16 at 14:41
  • @ParkerHalo how come you have marked my post down? This is NOT a duplicate?? I made sure i spent a few hours researching before posting a question!!! – DeltaRage Jun 28 '16 at 14:58

1 Answers1

7

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;
}
RemcoW
  • 4,196
  • 1
  • 22
  • 37
  • Hi thanks for the quick replies! I still get the same null pointer with the update you mentioned... :/ – DeltaRage Jun 28 '16 at 14:48
  • @Josh In that case I'm gonna need to see a stacktrace otherwise I can't see what's `null`. It might be that `screenshot` is `null`. Where / how are you creating an instance of your `Screenshot` class? – RemcoW Jun 28 '16 at 14:50
  • @Josh Judging by the screenshot you added to your post I'm assuming at the top of your test class you say something like: `private Screenshot screenshot;`. You never instantiate this class. So to your `setUp()` you should add: `screenshot = new Screenshot();` – RemcoW Jun 28 '16 at 14:53
  • This worked!!! :) Thanks so much! Really appreciate the help and the speed which you understood my problem!! Big thumbs up!!! :D – DeltaRage Jun 28 '16 at 14:56
  • @Josh Glas you got it working :). Feel free to accept my answer if that solved your problem so the question doesn't stay open forever. – RemcoW Jun 28 '16 at 14:58
  • I have done! Once again thanks alot! – DeltaRage Jun 28 '16 at 15:00