2

I am facing some trouble and got stuck since yesterday; unable to figure out the cause for it. Tried solution of answers here.

I created @Test, with sample of code to login and check dashaboard of application. and @AfterMethod, for when Assert is false, should capture a screenshot.

If i comment the Aftermethod code it works fine without any issue;

It used to run fine w/o any problem earlier.

Could you please help me in finding some solution. (it may be very small thing for you.. but pls do help me)

(EDITED) Error i am getting is

[TestNG] Running:
  C:\Users\Lenovo\AppData\Local\Temp\testng-eclipse--1410131027\testng-customsuite.xml

Exception while taking screenshot null
FAILED CONFIGURATION: @AfterMethod teardown([TestResult name=dashboardSanityTest status=FAILURE method=loginMain.dashboardSanityTest()[pri:0, instance:demotest.loginMain@1bce4f0a] output={null}])
java.lang.NullPointerException
    at demotest.loginMain.teardown(loginMain.java:144)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:510)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:211)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:703)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
    at org.testng.TestRunner.privateRun(TestRunner.java:774)
    at org.testng.TestRunner.run(TestRunner.java:624)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
    at org.testng.SuiteRunner.run(SuiteRunner.java:261)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
    at org.testng.TestNG.run(TestNG.java:1048)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)

SKIPPED CONFIGURATION: @AfterMethod teardown
FAILED: dashboardSanityTest
java.lang.NullPointerException
    at demotest.loginMain.dashboardSanityTest(loginMain.java:105)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
    at org.testng.TestRunner.privateRun(TestRunner.java:774)
    at org.testng.TestRunner.run(TestRunner.java:624)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
    at org.testng.SuiteRunner.run(SuiteRunner.java:261)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
    at org.testng.TestNG.run(TestNG.java:1048)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)

@AfterMethod Code

@AfterMethod
public void teardown(ITestResult result)
    {
  if (result.getStatus()==ITestResult.FAILURE)
  {
      String screenshotPath = Utils.captureScreenshot(driver, result.getName());
      String image = logger.addScreenCapture(screenshotPath);
  }

  report.flush();

}

Utils class

public class Utils {

public static String captureScreenshot(WebDriver driver, String ScreenshotName)
{
    try 
    {
        TakesScreenshot ts = (TakesScreenshot)driver;
        File source = ts.getScreenshotAs(OutputType.FILE);
        String dest="./screenshot/"+ScreenshotName+".png";
        File snapshotDest =new File(dest);
        FileUtils.copyFile(source, snapshotDest);

        System.out.println("Screenshot Taken at "+System.currentTimeMillis());

        return dest;

    } 

    catch (Exception e) 
    {
    System.out.println("Exception while taking screenshot "+e.getMessage());
    return e.getMessage();
        }
    }
 }
Sumit Gupta
  • 23
  • 1
  • 5
  • Please include the complete stack trace of your failure from your AfterMethod. That should help shed some light around the reason for the failure. Also please help share relevant sections of how the webdriver is being created and how its being cleaned up. – Krishnan Mahadevan Jan 18 '17 at 02:50
  • Hi All, Thanks for looking into it. I was out for couple of days, so couldn't respond. I have updated the error getting on Console. Hope it will serve the purpose. – Sumit Gupta Jan 21 '17 at 07:22

2 Answers2

3

Since you have not mentioned complete error stacktrace, it is a bit difficult to identify exact cause of failure. Please check whether driver value is getting null.

I would suggest you to leverage ITestListener instead of using method with @AfterMethod. You simply need to implement ITestListenerwhich will take the screenshot for you on test failure.

public class Listener implements ITestListener {
    public WebDriver driver;

    @Override
    public void onStart(ITestContext arg0) {
        Reporter.log("About to begin executing Test " + arg0.getName(), true);
    }

    @Override
    public void onFinish(ITestContext arg0) {
        Reporter.log("Completed executing test " + arg0.getName(), true);
    }

    @Override
    public void onTestFailure(ITestResult arg0) {
        try {
            String fileName = String.format("Screenshot-%s.jpg", Calendar
                    .getInstance().getTimeInMillis());
            driver = (WebDriver) arg0.getTestContext().getAttribute("WebDriver");
            TakesScreenshot ts = (TakesScreenshot)driver;
            File source = ts.getScreenshotAs(OutputType.FILE);
            String dest="./screenshot/"+ fileName;
            File snapshotDest =new File(dest);
            FileUtils.copyFile(source, snapshotDest);
            Reporter.log("Screen Shots file :  " + dest);
        } catch (Exception e) {
            throw new RuntimeException("Failed to take screenshot !", e);
        }
    }

}

Thanks !

Sameer Patil
  • 441
  • 3
  • 10
  • Sure. If you still get **null** for **driver** then use google [inject](https://github.com/google/guice/wiki/Injections) library for setting driver in testContext and use it while taking screen shot. Let me know if you still need more information. – Sameer Patil Jan 23 '17 at 05:38
0

Please check if your destination folder path is incorrect, you should include double \\ instead of \, that's why configuration issue is occurring.

user16217248
  • 3,119
  • 19
  • 19
  • 37