-1

I am getting Java Null Pointer Exception when trying to run the below code. The test gets failed before the Web page even loads completely.

I have increased the page load timeout and Implicit wait time, but still that didn't work. The same web page is getting loaded within 4 seconds when directly launched through browser. Please find the below code:

//Loginpagetest.java

Loginpage loginpage = new Loginpage();
public LoginPageTest() throws IOException {
    super();
}

@BeforeMethod
public void setup(){
    initialize();
}

@Test(priority=1)
public void loginPageTitleTest(){
    String actualTitle = loginpage.validateLoginPageTitle();
    String expectedTitle = "#1 Free CRM software in the cloud for sales and service";
    Assert.assertEquals(actualTitle,expectedTitle);
}

//TestBase.java

   public void initialize(){
    String browsername = prop.getProperty("browser");
    if(browsername.equals("chrome")){
        System.setProperty("webdriver.chrome.driver","C:\\Users\\Pranaykumar\\Downloads\\"chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver(options);
    }else if(browsername.equals("mozilla")){
        System.setProperty("webdriver.gecko.driver","E:\\geckodriver.exe");
        driver = new FirefoxDriver();
    }

    driver.manage().window().maximize();
    //driver.manage().deleteAllCookies();
    driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

    driver.get(prop.getProperty("url"));
}

//Loginpage.java

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

//Console

    [RemoteTestNG] detected TestNG version 6.14.3
    Starting ChromeDriver 2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387) on port 42802
    Only local connections are allowed.
    [1548934220.587][WARNING]: Timed out connecting to Chrome, retrying...
    Jan 31, 2019 5:00:25 PM org.openqa.selenium.remote.ProtocolHandshake createSession
    INFO: Detected dialect: OSS
    FAILED: loginPageTitleTest
    java.lang.NullPointerException
at com.crm.qa.pages.Loginpage.validateLoginPageTitle(Loginpage.java:37)
at com.crm.qa.testcases.LoginPageTest.loginPageTitleTest(LoginPageTest.java:30)
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)
  • It is getting failed when trying to call validateLoginPageTitle(). As it is not able to acccess the validateLoginPageTitle() method, Java Null pointer exception is thrown. – Pranay Kumar Feb 01 '19 at 06:21
  • I doubt that... the more likely scenario is that you are creating a new instance of the driver inside your `Loginpage` page object instead of passing the existing instance so it's not initialized... thus the NPE. – JeffC Feb 01 '19 at 14:35
  • Yeah Jeff, you are right. I was using a new instance instead of passing the existence one. I have fixed it and now it is working fine. – Pranay Kumar Feb 04 '19 at 06:11
  • Great, I added my comment as an answer if you would accept it so that this question doesn't get left unanswered. – JeffC Feb 04 '19 at 14:27

4 Answers4

0

Looks like this is not timeout issue, It’s showing NullPointerException. Maybe driver is null. Check your chrome driver path is correct or not.

‘FAILED: loginPageTitleTest java.lang.NullPointerException at com.crm.qa.pages.Loginpage.validateLoginPageTitle(Loginpage.java:37)’

If it’s timeout issue, you should get TimeoutExcepton.

Anand Somani
  • 801
  • 1
  • 6
  • 15
  • I guess the browser itself will not be launched if the driver path is incorrect. I've tried to debug the issue, looks like driver.gettitle() function is returning null. – Pranay Kumar Feb 01 '19 at 06:38
0

Can you please try to wait explicitly for an specific object in initialize() function and after the driver.get(prop.getProperty("url")) line as below code :

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(objectShouldPresent)));
sayhan
  • 1,168
  • 1
  • 16
  • 22
  • I have launched the same web page by creating a simple test and I am able to access the elements. I guess this might be something related to object referencing. – Pranay Kumar Feb 01 '19 at 06:05
  • Yes, I also think like you after that answer because of NullPointerException. Try to to give valid object reference. – sayhan Feb 01 '19 at 06:12
  • Yeah, I have checked the same. I've tried to debug the issue, looks like driver.gettitle() function is returning null. – Pranay Kumar Feb 01 '19 at 06:39
0

Kindly remove double quotes(") before chromedriver_win32

System.setProperty("webdriver.chrome.driver","C:\\Users\\Pranaykumar\\Downloads\\"chromedriver_win32\\chromedriver.exe");
Bhavesh Soni
  • 188
  • 8
0

The likely scenario is that you are creating a new instance of the driver inside your Loginpage page object instead of passing the existing instance so it's not initialized... thus the NPE.

JeffC
  • 22,180
  • 5
  • 32
  • 55