-1

Base class

package resources;

import java.io.FileInputStream;
import java.io.IOException;
import java.time.Duration;
import java.util.Properties;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;


public class base {

public WebDriver driver;
public Properties property;
public String url= "qwerty"; 


public WebDriver initializeDriver() throws IOException {
    
    
    property = new Properties();
    FileInputStream file = new FileInputStream("D:\\qwe\\rty\\src\\main\\java\\resources\\data.properties");
    
    property.load(file);
    String BrowserName = property.getProperty("browser");
     
    if(BrowserName.equals("chrome")) 
    {
     System.setProperty("webdriver.chrome.driver", "D:\qwe\\rty\\chromedriver.exe");   
        driver = new ChromeDriver();
        
    }
    else if(BrowserName.equals("firefox")) 
    {
        driver = new FirefoxDriver();
    }
    else if(BrowserName.equals("IE")) 
    {
        //Executes IE
    }
    driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
    return driver;
}

public WebDriver verifyPage() {
    
    driver.get(url);
    String Expected = driver.findElement(By.cssSelector("p[class='login-box-msg']")).getText();
    String Actual = "Sign in to start your session";
    Assert.assertEquals(Actual, Expected);
    System.out.println("Homepage is displayed");
    return driver;
}
}

Page Object class

package pageObjects;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;


public class LoginPage {

public WebDriver driver;

By username = By.xpath("//input[@type='text']");
By password = By.xpath("//input[@type='password']");
By login = By.xpath("//button[@type='submit']");
By profile = By.xpath("//li[contains(@class,'nav-item dropdown user-menu')]/a[1]/img");
By logout = By.xpath("//li[@class='user-footer']/a[1]");
 



public LoginPage(WebDriver driver) {
    
    this.driver = driver;
 
}


public WebElement user() {

    return driver.findElement(username);
}

public WebElement pass() {

    return driver.findElement(password);
}

public WebElement signIn() {

    return driver.findElement(login);
}

public WebElement pro() {

    return driver.findElement(profile);
}

public WebElement signOut() {

    return driver.findElement(logout);
} 



}

Testcase

package Admission;

import java.io.IOException;
import java.time.Duration;


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

import pageObjects.LoginPage;
import resources.base;


public class homePage extends base{


public WebDriver driver;
LoginPage lp = new LoginPage(driver);

@Test
public void loginDetails() throws IOException, InterruptedException
{
    
    driver=initializeDriver();
    verifyPage();
    
    
    WebElement aadharNo = lp.user();
    aadharNo.sendKeys("111111111111");
    
    WebElement password = lp.pass();
    password.sendKeys("21102021");
    
    WebElement submit = lp.signIn();
    submit.click();
    
    
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); 
    wait.until(ExpectedConditions.visibilityOf(lp.pro())).click();
    
    WebDriverWait wait1 = new WebDriverWait(driver, Duration.ofSeconds(10)); 
    wait1.until(ExpectedConditions.visibilityOf(lp.signOut())).click();
    
}

    @Test
    public void testCase() {
        
    WebElement aadharNo = lp.user();
    WebDriverWait wait2 = new WebDriverWait(driver, Duration.ofSeconds(30)); 
    wait2.until(ExpectedConditions.visibilityOf(aadharNo));
    aadharNo.sendKeys("111111111111");
    
    WebElement password = lp.pass();
    WebDriverWait wait3 = new WebDriverWait(driver, Duration.ofSeconds(20)); 
    wait3.until(ExpectedConditions.elementToBeSelected(password));
    password.sendKeys("hgfhg");
    lp.signIn();
}
}
5
  • 1
    Can you share the stack trace of the error? It's hard to follow up all your code. Nov 29 '21 at 8:08
  • java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.findElement(org.openqa.selenium.By)" because "this.driver" is null at pageObjects.LoginPage.user(LoginPage.java:30) at Admission.homePage.loginDetails(homePage.java:31) i.e Loginpage: return driver.findElement(username); Homepage: WebElement aadharNo = lp.user();
    – Varun
    Nov 29 '21 at 9:53
  • second error java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.findElement(org.openqa.selenium.By)" because "this.driver" is null at pageObjects.LoginPage.user(LoginPage.java:30) at Admission.homePage.testCase(homePage.java:52) i.e Loginpage: return driver.findElement(username); homepage: WebElement aadharNo = lp.user();
    – Varun
    Nov 29 '21 at 9:56
  • I am trying to design framework .. need help with this
    – Varun
    Nov 29 '21 at 11:10
  • Hi Joao Faris kindly help in this
    – Varun
    Nov 30 '21 at 5:23
1

Here

public WebDriver driver;
LoginPage lp = new LoginPage(driver);

You pass null to the constructor because driver field yet not initialized. The fact you later assign some non-null value to the field does not automatically rewrite reference in LoginPage. Hence you get NPE when access to that null field.

1
  • Yes I have initialised the same in Testcase but still getting NPE in page object class
    – Varun
    Nov 29 '21 at 13:12
1

As Alexey said, the problem is that you are passing a null to the creation of LoginPage.

public WebDriver driver;
LoginPage lp = new LoginPage(driver);

What you can do is to set up a LoginPage for each test:

@BeforeEach
public void setUp() {
  driver = initializeDriver();
  lp = new LoginPage(driver);
}

If you prefer to set it up only once for all tests in the class, you can use the annotation @BeforeClass.

1
  • got it thank you
    – Varun
    Dec 3 '21 at 5:37

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.