0

I am getting the following error message in my IntelliJ

enter image description here

The "TestBase.java:33" and "LoginPageTest.java25" essentially both point to the

if(browserName.equals("chrome")) line of code in the base class below.

This is what my base, POM, and test class looks like

Base class:


import com.parabank.qa.util.TestUtil;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

public class TestBase {

   public static WebDriver driver;
   public static Properties prop;

   public TestBase() {
       try {
           prop = new Properties();
           FileInputStream ip = new FileInputStream("/Users/rasha011/Dev/Parabankparasoft/src/main/java/com/parabank/qa/config/config.properties");
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       }
   }

   public static void initialization(){
       String browserName = prop.getProperty("browser");

       if(browserName.equals("chrome")){
           System.setProperty("webdriver.chrome.driver", "/Users/rasha011/Downloads/chromedriver");
           driver = new ChromeDriver();
       } else if(browserName.equals("FireFox")){
           System.setProperty("webdriver.chrome.driver", "/Users/rasha011/Dev/Parabankparasoft/src/main/resources/geckodriver");
           driver = new FirefoxDriver();
       }

       driver.manage().window().maximize();
       driver.manage().deleteAllCookies();
       driver.manage().timeouts().pageLoadTimeout(TestUtil.PAGE_LOAD_TIMEOUT, TimeUnit.SECONDS);
       driver.manage().timeouts().implicitlyWait(TestUtil.IMPLICIT_WAIT, TimeUnit.SECONDS);
       driver.get(prop.getProperty("url"));
   }

}

POM class:


import com.parabank.qa.base.TestBase;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class LoginPage extends TestBase {

    //Page Factory
    @FindBy(name="username")
    private WebElement username;

    @FindBy(name="password")
    private WebElement password;

    @FindBy(xpath = "//input[@type='submit']")
    private WebElement loginBtn;

    @FindBy(xpath = "//button[contains(text(),'Sign Up')]")
    private WebElement signUpBtn;

    @FindBy(xpath ="//img[contains(@class,'img-responsive')]")
    private WebElement crmLogo;

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

    public String validateLoginPageTitle(){
        return driver.getTitle();
    }

    public boolean validateCRMImage(){
        return crmLogo.isDisplayed();
    }

    public HomePage login(String un, String pwd){
        username.sendKeys(un);
        password.sendKeys(pwd);
        loginBtn.clear();
        return new HomePage();
    }
}

Test class:


import com.parabank.qa.base.TestBase;
import com.parabank.qa.pages.HomePage;
import com.parabank.qa.pages.LoginPage;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class LoginPageTest extends TestBase {

    LoginPage loginPage;
    HomePage homepage;

    //I call the base class using the "super" keyword. This way I call the test base constructor which
    //enable me to access the username and password from the properties file
    public LoginPageTest() {
        super();
    }

    //This before method will initialize the chrome driver, set waits, and get the web URL
    @BeforeMethod
    public void setUp(){
        initialization();
        loginPage = new LoginPage();
    }

    @Test(priority=1)
    public void loginPageTitleTest(){
        String title = loginPage.validateLoginPageTitle();
        Assert.assertEquals(title, "CRMPRO  - CRM software for customer relationship management, sales, and support.");
    }

    @Test(priority = 2)
    public void crmLogoImageTest(){
        Assert.assertTrue(loginPage.validateCRMImage());
    }

    @Test(priority = 3)
    public void loginTest(){
        homepage = loginPage.login(prop.getProperty("username"), prop.getProperty("password"));
    }

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

I've doubled checked that the chromedriver exists in the location I specified but I still can't seem to run the test successfully.

2
  • This is the exact question and issue as this tagged one. – PDHide May 2 '20 at 8:53
  • Even the code is identical – PDHide May 2 '20 at 8:53
2

You haven't loaded your file to properties object hence you get null when try to obtain a property value. Your code should look like

   public TestBase() {
       try {
           prop = new Properties();
           FileInputStream ip = new FileInputStream("/Users/rasha011/Dev/Parabankparasoft/src/main/java/com/parabank/qa/config/config.properties");
           prop.load(ip);
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       }
   }

By the way it is a good practice to call equals of a constant, not of a variable. This will let you avoid npe. For example:

Not this: if(browserName.equals("chrome"))

But this: if("chrome".equals(browserName))

6
  • the issue is wit h if(browserName.equals("chrome")) changing it to browserName.contains("chrome") fixes the issue – PDHide May 2 '20 at 8:54
  • How changing from equals to contains can resolve NullPointerException? – Alexey R. May 2 '20 at 9:06
  • 1
    Despite the questions look similar indeed, the one you're referring still has the line prop.load(ip) (unlike this one) so I believe for this particular piece of code (and the line where exception occurs according to OP) the problem is in that properties have not been loaded from a file hence taking prop.getProperty("browser"); return null and obviuosly calling either equals or contains would result in NPE. – Alexey R. May 2 '20 at 9:12
  • 1
    true , i missed that – PDHide May 2 '20 at 9:15

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