0

TestBase Class:-

public class TestBase {
    public static WebDriver driver;
    public static Properties prop;

    // TestBase class constructor is used to initialize the properties object to
    // fetch config variables from config.properties file.
    public TestBase() {

        try {

            // File src = new File("./src/main/java/com/config/config.properties");
            File src = new File(".\\src\\main\\java\\com\\config\\config.properties");
            FileInputStream fs = new FileInputStream(src);
            prop = new Properties();
            prop.load(fs);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void initialization() throws InterruptedException {
        String browserName = prop.getProperty("Browser");
        if (browserName.equals("chrome")) {
            System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver_win32\\chromedriver.exe");
            driver = new ChromeDriver();
        } else {
            System.out.println("Oops! Exception has Caught");
        }

        driver.manage().window().maximize();
        driver.get(prop.getProperty("URL"));
    }
}

HomePage Class:-

public class HomePage extends TestBase {

    TestUtil testUtil;

    @FindBy(xpath = "//a[@title='Update your profile, personal settings, and more']")
    WebElement updateProfile;


    @FindBy(xpath = "//a[@title='Create a New Post']")
    WebElement createNewPost;

    @FindBy(xpath="(//span[@class='count'])[2]")
    WebElement drafts;

    @FindBy(xpath="//a[@class='button masterbar__recent-drafts-see-all is-compact is-borderless']")
    WebElement seeAll;

    public HomePage() {
        PageFactory.initElements(driver, this);
    }

    public ProfilePage updateYourProfile() throws AWTException, InterruptedException {
        updateProfile.click();
        Thread.sleep(3000);
        return new ProfilePage();
    }

    public NewPostPage createNewPost() throws InterruptedException {
        createNewPost.click();
        Thread.sleep(3000);
        return new NewPostPage();
    }

    public DraftsPage draftsPage() throws InterruptedException {
        createNewPost.click();
        Thread.sleep(3000);
        drafts.click();
        Thread.sleep(3000);
        seeAll.click();
        Thread.sleep(3000);
        return new DraftsPage();
    }
}

DraftsPage Class:-

public class DraftsPage extends TestBase {

    @FindBy(xpath="(//button[@title='Toggle menu'])[1]")
    WebElement toggleMenu;

    @FindBy(xpath="(//button[@role='menuitem'])[2]")
    WebElement trash;

    public void toggleMenu() throws InterruptedException {
        toggleMenu.click();
        Thread.sleep(3000);
        trash.click();
        Thread.sleep(3000);
    }
}

DraftsPageTest Class:-

public class DraftsPageTest extends TestBase {

    ProfilePage myProfile;
    LoginPage loginpage;
    HomePage homepage;
    NewPostPage newPostPage;
    DraftsPage draftspage;

    public DraftsPageTest() {
        super();
    }

    @BeforeMethod
    public void setUp() throws InterruptedException {
        initialization();
        loginpage = new LoginPage();
        loginpage.login();

        Thread.sleep(3000);
        loginpage.userName(prop.getProperty("username"));
        Thread.sleep(3000);
        homepage = loginpage.password(prop.getProperty("password"));
        Thread.sleep(3000);
        myProfile = new ProfilePage();
        Thread.sleep(3000);
        newPostPage = new NewPostPage();
        Thread.sleep(3000);
        draftspage = new DraftsPage();
        Thread.sleep(3000);
    }

    @Test
    public void DraftsPageTest() throws InterruptedException {
        homepage.draftsPage();
        Thread.sleep(3000);
        draftspage.toggleMenu();
        Thread.sleep(3000);
    }

    @AfterMethod
    public void close() {
        driver.close();
    }
}

Error Message:-

FAILED: DraftsPageTest
java.lang.NullPointerException
    at com.pages.DraftsPage.toggleMenu(DraftsPage.java:18)
    at com.testCases.DraftsPageTest.DraftsPageTest(DraftsPageTest.java:51)
    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)

Description:-

I am trying to develop TestNG Framework with the help of Selenium. I have developed HomePage, LoginPage, ProfilePage, NewPostPage, DraftsPage. While I was running the DraftsPageTest.java, @BeforeMethod was working fine and the moment control comes to @Test, homepage.draftsPage(); is also working fine. But the moment control comes to draftspage.toggleMenu(); it is throwing

java.lang.NullPointerException.

Any help will be highly appreciated.

Thanks in advance:)

3
  • Can you post a MCVE of your problem? Commented Aug 27, 2018 at 8:31
  • I am afraid the code I have posted i.e the MCVE of my problem. Commented Aug 27, 2018 at 8:38
  • @ManideepLatchupatula I would strongly suggest to use either implicit or explicit waits instad of Thread.sleep Commented Aug 27, 2018 at 14:30

1 Answer 1

2

There's a bug in your code.

Please add a constructor which calls PageFactory.initElements(driver, this); to your com.pages.DraftsPage class [ similar to the HomePage constructor in your shared code ]. That should fix the problem.

The reason behind the NullPointerException is because, there's no call to PageFactory.initElements() the WebElement toggleMenu is not being initialised.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank You so much @Krishnan Mahadevan for your clear explanation!
Krishnan Mahadevan Could you define Hybrid Framework in the question where I have posted earlier?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.