@AfterTest selenium annotation getting executed before all the @Test's are executed

35 views
Skip to first unread message

Prakash P

unread,
May 18, 2016, 9:12:13 AM5/18/16
to testng-users
have worked on Selenium and TestNG. But I am not an expert. I tried searching a suitable answer for this but could not find. Hence requesting help.

I have a initialization class as below.


TestNG version: 6.9.10


public class Initialization {

    private static Logger logger = Logger.getLogger(Initialization.class);
    private WebDriver driver;

    @BeforeTest
    public void intiDriver() throws MalformedURLException {
        logger.debug("Creating driver");
        DesiredCapabilities dcap = DesiredCapabilities.firefox();
        driver = new RemoteWebDriver(new URL("http://127.0.0.1:4444/wd/hub"), dcap);
        logger.debug("Driver created");
    }

    @AfterTest
    public void closeDriver(){
        logger.debug("Quitting driver");
        if(driver != null){
            driver.close();
            driver.quit();
            logger.debug("Driver destroyed");
        }
    }

    public WebDriver getDriver() {
        return driver;
    }
}

And then I have 2 test case class file which extends the above class file (as shown below)

public class SampleTestCase1 extends Initialization {

    @Test
    public void sampleTest1(){
        System.out.println("getDriver1: "+ getDriver());
        getDriver().get("http://www.google.com");
    }

    @Test
    public void sampleTest2(){
        System.out.println("getDriver2: "+ getDriver());
        getDriver().get("http://www.bing.com");
    }
}

public class SampleTestCase2 extends Initialization {

    @Test
    public void sampleTest3(){
        System.out.println("getDriver3: "+ getDriver());
        getDriver().get("https://en.wikipedia.org/wiki/Main_Page");
    }

    @Test
    public void sampleTest4(){
        System.out.println("getDriver4: "+ getDriver());
        getDriver().get("http://www.rediff.com/");
    }
}

I am observing that sampleTest3 and sampleTest4 testcases are not being executed because getDriver() is returning null. I believe the driver instance is getting destroyed after I execute sampleTest1 and smapleTest2. I have used @afterTest annotation, hence ideally the driver instance should be destroyed after all the @Test's are executed.

I don't what is going wrong. Can somebody help?.

Stack Trace:

java.lang.NullPointerException
    at Sample.SampleTestCase2.sampleTest3(SampleTestCase2.java:16)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
    at org.testng.TestRunner.privateRun(TestRunner.java:782)
    at org.testng.TestRunner.run(TestRunner.java:632)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
    at org.testng.SuiteRunner.run(SuiteRunner.java:268)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
    at org.testng.TestNG.run(TestNG.java:1064)
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:74)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:121)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

Julien Herr

unread,
May 18, 2016, 11:20:28 AM5/18/16
to testng-users
Reply all
Reply to author
Forward
0 new messages