Search code examples
selenium-webdrivertestngtestng-eclipse

NullPointer Exception while running a test in TestNG and Selenium


I have 4 methods which invokes my browser, logs in to the page and does some actions. When I run the code as a Java application, the browser opens up and the actions are performed, but when I run as a TestNG test, the console shows all 4 test passed, but it also shows Null Pointer Exception. Also, when running as testng test, it's not invoking my browser and performing the actions.

Code:

package pulse.tpr;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.*;

public class LoginInterface {

    public WebDriver driver;
    public Actions actions;

    public void launchApplication() {

        System.setProperty("webdriver.chrome.driver",
                "C:\\MyChromeDriver\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("myURL");
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
        actions = new Actions(driver);
    }

    public void closeApplication() {
        driver.close();
    }

    @Test(priority = 0)
    public void adminLogin() {

        try {
            actions.moveToElement(driver.findElement(By.name("userId")));
            actions.click();
            actions.sendKeys("sysadmin");
            actions.build().perform();

            actions.pause(java.time.Duration.ofSeconds(1));

            actions.moveToElement(driver.findElement(By.name("password")));
            actions.click();
            actions.sendKeys("MMNext13#");
            actions.build().perform();

            actions.pause(java.time.Duration.ofSeconds(1));
                      actions.moveToElement(driver.findElement(By.name("dcNumber")));
            actions.click();
            actions.sendKeys("7036");
            actions.build().perform();

        } catch (Exception e) {

            e.printStackTrace();
        }
    }

    @Test(priority = 1)
    public void tprPage() {

        try {
            actions.moveToElement(driver.findElement(By
                    .cssSelector("body > ion-app > ng-component > ion-nav > page-login > ion-content > div.scroll-content > ion-card > ion-grid > form > ion-list > div.login-button > button > span")));
            actions.click();
            actions.build().perform();

            actions.moveToElement(driver.findElement(By.id("tab-t0-2")));
            actions.click();
            actions.build().perform();

            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        } catch (Exception e) {

            e.printStackTrace();
        }
    }

    @Test(priority = 2)
    public void createMove() {
        try {
            actions.moveToElement(driver.findElement(By
                    .className("send-associates-button")));
            actions.click();
            actions.build().perform();

            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

            actions.moveToElement(driver.findElement(By
                    .cssSelector("#tabpanel-t0-2 > send-associate-page > ion-content > div.scroll-content > div > ion-list > ion-item:nth-child(4)")));
            actions.click();
            actions.build().perform();

            actions.moveToElement(driver.findElement(By
                    .xpath("//*[@id='footers']/ion-toolbar/div[2]/ion-row/ion-col[2]/button")));
            actions.click();
            actions.build().perform();

            actions.moveToElement(driver.findElement(By
                    .xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-content/div[2]/div/div/ion-col[2]")));
            actions.click();
            actions.build().perform();

            actions.moveToElement(driver.findElement(By
                    .xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-content/div[2]/div/div/ion-col[2]/ion-row[2]")));
            actions.click();
            actions.build().perform();

            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

            // Select to area
            actions.moveToElement(driver.findElement(By
                    .xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-content/div[2]/div/div/ion-col[2]")));
            actions.click();
            actions.build().perform();

            // Select to area
            actions.moveToElement(driver.findElement(By
                    .xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-content/div[2]/div/div/ion-col[2]/ion-row[2]")));
            actions.click();
            actions.build().perform();

            // Next button in to area page
            actions.moveToElement(driver.findElement(By
                    .xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-footer/button")));
            actions.click();
            actions.build().perform();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test(priority = 3)
    public void rightNowToClockout() {
        try {
            // Right Now Time

            actions.moveToElement(driver.findElement(By
                    .xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-content/div[2]/div/div/ion-row[4]")));
            actions.click();
            actions.build().perform();

            // Next Button
            actions.moveToElement(driver.findElement(By
                    .xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-footer/button")));
            actions.click();
            actions.build().perform();

            // Clockout time
            actions.moveToElement(driver.findElement(By
                    .xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-content/div[2]/div/div/ion-row[2]/ion-col")));
            actions.click();
            actions.build().perform();

            // Next button
            actions.moveToElement(driver.findElement(By
                    .xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-footer/button")));
            actions.click();
            actions.build().perform();

            // Send Button
            actions.moveToElement(driver.findElement(By
                    .xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-footer/ion-row/ion-row")));
            actions.click();
            actions.build().perform();

            WebDriverWait wait = new WebDriverWait(driver, 60);
            wait.until(ExpectedConditions.visibilityOfElementLocated(By
                    .xpath("//*[@id='tabpanel-t0-2']/tpr-summary-page/ion-content/div[2]/div/div[2]/div/ion-card/ion-row[1]/ion-col/ion-row[2]")));
            String element = driver
                    .findElement(
                            By.xpath("//*[@id='tabpanel-t0-2']/tpr-summary-page/ion-content/div[2]/div/div[2]/div[1]/ion-card/ion-row[1]/ion-col/ion-row[2]/div[1]/label[1]"))
                    .getText();
            Assert.assertTrue(element.contains("1:35"));
        } catch (Exception e) {

            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        LoginInterface obj = new LoginInterface();
        obj.launchApplication();
        obj.adminLogin();
        obj.tprPage();
        obj.createMove();
        obj.rightNowToClockout();
    }
}

And below is the testNG error:

java.lang.NullPointerException
    at pulse.tpr.LoginInterface.adminLogin(LoginInterface.java:38)
    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:124)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
java.lang.NullPointerException
    at pulse.tpr.LoginInterface.tprPage(LoginInterface.java:67)
    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:124)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)

Solution

  • Instead of running the script in the main method, you should run the script as a "TestNG Test" and to do the initial setup before running the tests you can use, BeforeSuite and AfterSuite.

    So, you need to make two changes in your script:

    1. Insert a tag @BeforeSuite before the method launchApplication() and insert a tag @AfterSuite before the method closeApplication() which would perform the setup and the closing of the driver before and after executing the script.

    2. Remove the whole main method that you have written and then run the script as "TestNG Test", your tests should run as expected.