0

I am getting java.lang.NullPointerException while executing my test case , I am using Page Object Modle with PageFactory ,following are the code snippets

TestBase Class:

ackage com.base;


import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import pagefactory.Scorex_LoginPage;
import pagefactory.Scorex_HomePage;

public class TestBase {

       protected WebDriver driver;
       protected String baseUrl = "http://scorex.domain.com/";
       protected Scorex_LoginPage objLogin;
       protected Scorex_HomePage objHome;

       @BeforeMethod
       public void initializeBrowser() {
             driver = new FirefoxDriver();
             driver.get(baseUrl);
             driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
             driver.manage().window().maximize();

       }
       @AfterMethod
       public void closeBrowser(){
             driver.quit();

       }
    }

Test Case Class:

package tests;

import org.testng.Assert;
import org.testng.annotations.Test;
import com.base.TestBase;

public class Scorex_LoginTestCases extends TestBase{

    @Test(priority=1) //TC01
    public void Scorex_valid_email_password()
    {
        objLogin.loginScoreAs("superadmin@domain.com", "superadmin");
        String DashboardUserName = objHome.getHomePageUserName();
        Assert.assertTrue(DashboardUserName.contains("Super Admin"));
    }
}
1

On the Scorex_LoginTestCases (Btw, we do not use underscore in names in Java (See conventions), the objects objLogin and objHome are not initialized.

On the TestBase, you should initialize them using the PageFactory's method initElements. You can see a sample here.

0

If you are using Page Factory we need to initialize the Page Object: You can initialize with the help of below sample code:

driver = new FirefoxDriver();
driver.get(baseUrl);
Scorex_LoginTestCases loginTest = PageFactory.initElements(driver,Scorex_LoginTestCases.class);
loginTest.Scorex_valid_email_password();

And please follow the conventions as method name always starts with a small character and please avoid using underscore. For more information on conventions please click here.

For more information on Page Factory please click here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.