简体   繁体   中英

TestNG @Test and @AfterMethod are not executing and also not take the screenshot

The @BeforeMethod executes however, it will not execute @Test and @AfterMethod

Not sure why it will not execute after the @BeforeMethod and also not take the ScreenShot

Console

[RemoteTestNG] detected TestNG version 7.0.0
Starting ChromeDriver 2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb) on port 33501
Only local connections are allowed.
Mar 06, 2020 4:05:25 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Starting ChromeDriver 2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb) on port 43792
Only local connections are allowed.
Mar 06, 2020 4:05:31 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS

Failed Test

Failed Test

===============================================
    Default test
    Tests run: 0, Failures: 0, Skips: 0
===============================================

java.lang.NullPointerException
    at utility.CapSc.captureScreenShot(CapSc.java:16)
    at autobillAutomation.listeners.CustomListener.onTestFailure(CustomListener.java:32)
    at org.testng.internal.TestListenerHelper.runTestListeners(TestListenerHelper.java:66)
    at org.testng.internal.TestInvoker.runTestResultListener(TestInvoker.java:210)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:814)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:145)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.util.ArrayList.forEach(Unknown Source)
    at org.testng.TestRunner.privateRun(TestRunner.java:770)
    at org.testng.TestRunner.run(TestRunner.java:591)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:402)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:396)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:355)
    at org.testng.SuiteRunner.run(SuiteRunner.java:304)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1180)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1102)
    at org.testng.TestNG.runSuites(TestNG.java:1032)
    at org.testng.TestNG.run(TestNG.java:1000)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

Classes:

public class Setup {

    public static WebDriver driver;

    public static void initialize() {
        driver = new ChromeDriver();
        System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\chromedriver_win32\\chromedriver_1.exe");

        // deleteCookies
        driver.manage().deleteAllCookies();
        // navigateToPage
        driver.navigate().to("https://www.gmail.com");
        // setWindowSize | 1920x1080
        driver.manage().window().setSize(new Dimension(1920, 1080));
        // manageTimeouts
        driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
        // switchToTheTestWindow
        String currentWindow = driver.getWindowHandle();
        driver.switchTo().window(currentWindow);
    }

}


@Listeners(CustomListener.class)
public class Login {

    public static WebDriver driver;


    @BeforeMethod
    public void setUp() throws Exception{
        Setup.initialize();
    }
    @Test
    public void login() throws InterruptedException {
        // Test name: Login
        // Step # | name | target | value | comment
        // 3 | click | id=email | |
        driver.findElement(By.id("email")).click();
        // 4 | type | id=email | test@email.com |
        driver.findElement(By.id("email")).sendKeys("test@email.com");
        // 5 | click | id=password | |
        driver.findElement(By.id("password")).click();
        // 6 | type | id=password | pwd |
        driver.findElement(By.id("password")).sendKeys("pwd");
        // 7 | click | css=.login-button | |
        driver.findElement(By.cssSelector(".login-button")).click();
        // Assert.assertTrue(driver.getTitle().contains("Dashboard"));
        // 8 | click | css=.place-holder-image | |
        driver.findElement(By.cssSelector(".place-holder-image")).click();
        // 9 | click | css=.new-list .hide-other-tabs-li > .new-text | |
        driver.findElement(By.cssSelector(".new-list .hide-other-tabs-li > .new-text")).click();
        // 10 | click | css=.user-image > .place-holder-image | |
        driver.findElement(By.cssSelector(".user-image > .place-holder-image")).click();
        // 11 | click | css=a > .new-item > .new-text | |
        driver.findElement(By.cssSelector("a > .new-item > .new-text")).click();
        // 12 | close | | |
        // driver.close();
    }
    @AfterMethod (alwaysRun = true)
    public void tearDown() {
        driver.quit();

    }
}

public abstract class CapSc {
    public static WebDriver driver;


    public static void captureScreenShot (String screenshotName)  {

        File source = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

        try {
            FileUtils.copyFile(source, new File ("D:\\Maven\\eclipse-workspace\\mavenwebdriverjunit\\ScreenShots"+screenshotName+".png"));
            System.out.println("Screenshot Taken");
        } catch (IOException e) {

            System.out.println("Exception while Taking Screenshot " + e.getMessage());
        }


    }
}

public class CustomListener implements ITestListener{

    @Override
    public void onFinish(ITestContext result) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStart(ITestContext result) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTestFailure(ITestResult result) {
        System.out.println("Failed Test");
        CapSc.captureScreenShot(result.getClass().getName()+"_"+result.getMethod().getMethodName());


    }

    @Override
    public void onTestSkipped(ITestResult result) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTestStart(ITestResult result) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTestSuccess(ITestResult result) {
        // TODO Auto-generated method stub

    }

}

The exception NullPointerException under @Test method of Login class because Setup.initialize(); initialize the driver in Setup class only You wont get the same reference under Login class and the driver instance under Login class remain null.

Either You need to return the driver instance from Setup class and use that in Login class or extend the Setup class in Login class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM