-1

I am making a maven project and at the time of opening the chrome browser is shows java.lang.NullPointerException exeption, i am unable to find what is missing here and why browser is not opening.

my base class is like this


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.AfterMethod;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;

import pages.LandingPage;
import pages.LoginPage;
import utils.ExtentReportManager;
import utils.DateUtils;


public class Base {
 public WebDriver driver;
 public Properties prop;
 public ExtentReports report = ExtentReportManager.getReportInstance();
 public ExtentTest logger;

 public void invokeBrowser(String browsername) {
     try {
         if (browsername.equalsIgnoreCase("Chrome")) {
             System.setProperty("webdriver.chrome.driver",
                     System.getProperty("user.dir") + "\\src\\resourc\\driver\\chromedriver.exe");
             driver = new ChromeDriver();//This is line 39 in Base
         } else if (browsername.equalsIgnoreCase("Firefox")) {
             System.setProperty("webdriver.gecko.driver",
                     System.getProperty("user.dir") + "\\src\\resourc\\driver\\geckodriver.exe");
             driver = new FirefoxDriver();
         } else {
             System.setProperty("webdriver.IEDriverServer.driver",
                     System.getProperty("user.dir") + "\\src\\resourc\\driver\\IEDriverServer.exe");
             driver = new InternetExplorerDriver();
         }
     } catch (Exception e) {
           e.printStackTrace();
     }
     driver.manage().timeouts().implicitlyWait(180, TimeUnit.SECONDS);//**This is line 53**
     driver.manage().window().maximize();
     driver.manage().timeouts().pageLoadTimeout(180, TimeUnit.SECONDS);


         prop = new Properties();

         try {
             FileInputStream file = new FileInputStream(
                     System.getProperty("user.dir") + "\\src\\resourc\\ObjectRepository\\projectConfig.properties");
             prop.load(file);
         } catch (Exception e) {
             e.printStackTrace();
         }

     }


 public LandingPage OpenApplication(String websiteURLKey ) {
     driver.get(prop.getProperty(websiteURLKey));
     return PageFactory.initElements(driver, LandingPage.class);
 }



 public void closebrowser() {
     driver.close();
 }

 @AfterMethod
 public void reportFlush() {
     report.flush();
 }


 /****************** Capture Screen Shot ***********************/
 public void takeScreenShotOnFailure() {
     TakesScreenshot takeScreenShot = (TakesScreenshot) driver;
     File sourceFile = takeScreenShot.getScreenshotAs(OutputType.FILE);

     File destFile = new File(System.getProperty("user.dir") + "/ScreenShots/" + DateUtils.getTimeStamp() + ".png");
     try {
         FileUtils.copyFile(sourceFile, destFile);
//          logger.addScreenCaptureFromPath(
//                  System.getProperty("user.dir") + "/ScreenShots/" + DateUtils.getTimeStamp() + ".png");

     } catch (IOException e) {
         e.printStackTrace();
     }

 }

}

and my test class is like this from where i am running all the files for project one by one

package test;

import org.apache.http.client.utils.DateUtils;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.Test;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;

import base.Base;
import pages.DisplayDetails;
import pages.HomePage;
import pages.LandingPage;
import pages.LoginPage;
import pages.UpcomingBikesPage;
import utils.ExtentReportManager;

public class ProjectTest extends Base{
    LandingPage landingpage;
    LoginPage loginpage;
    HomePage homepage;
    UpcomingBikesPage upcomingbikespage;
    DisplayDetails displaydetails;

    @Test
    public void openBrowser() throws Exception {

        logger = report.createTest("Zigwheels Website Automation");
        Base base = new Base();
        logger.log(Status.INFO, "Initializing the Browser");
        base.invokeBrowser("chrome");//**This is line 32 in test file**
        logger.log(Status.INFO, "Opening the Website");
        landingpage = base.OpenApplication("websiteURL");
        logger.log(Status.INFO, " Login initated Successfully");
        loginpage = landingpage.initiatelogin();
        logger.log(Status.INFO, "Entered Credentials Successfully");

        homepage = loginpage.login();
        logger.log(Status.INFO, "Login Successfull");

        upcomingbikespage = homepage.home();
        logger.log(Status.INFO, "Got Upcoming Honda Bikes Successfully");
        displaydetails=upcomingbikespage.hondamanufacturer();

        displaydetails.finaly();

        //logger.addScreenCaptureFromPath(System.getProperty("user.dir") + "/ScreenShots/" + DateUtils.getTimeStamp() + ".png");


    }

}

And in the console Error is as follows--

[RemoteTestNG] detected TestNG version 6.14.3
Starting ChromeDriver 80.0.3987.106 (f68069574609230cf9b635cd784cfb1bf81bb53a-refs/branch-heads/3987@{#882}) on port 36324
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
FAILED: openBrowser
java.lang.NullPointerException
    at base.Base.invokeBrowser(Base.java:53)
    at test.ProjectTest.openBrowser(ProjectTest.java:32)
    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)

Line 53 is Shown through comment that is implicit wait line Line 32 in test is also shown through comment that is base.invokeBrowser("chrome"); line.

org.openqa.selenium.SessionNotCreatedException: session not created: This version of ChromeDriver only supports Chrome version 80
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'PRANJAL', ip: '192.168.225.26', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_241'
Driver info: driver.version: ChromeDriver
remote stacktrace: Backtrace:
    Ordinal0 [0x00F80C83+1707139]
    Ordinal0 [0x00EE68F1+1075441]
    Ordinal0 [0x00E5DFC9+516041]
    Ordinal0 [0x00DF0554+66900]
    Ordinal0 [0x00DECCE2+52450]
    Ordinal0 [0x00E0BFD7+180183]
    Ordinal0 [0x00E0BDDD+179677]
    Ordinal0 [0x00E09D4B+171339]
    Ordinal0 [0x00DF1D4A+73034]
    Ordinal0 [0x00DF2DC0+77248]
    Ordinal0 [0x00DF2D59+77145]
    Ordinal0 [0x00EFBB67+1162087]
    GetHandleVerifier [0x0101A966+508998]
    GetHandleVerifier [0x0101A6A4+508292]
    GetHandleVerifier [0x0102F7B7+594583]
    GetHandleVerifier [0x0101B1D6+511158]
    Ordinal0 [0x00EF402C+1130540]
    Ordinal0 [0x00EFD4CB+1168587]
    Ordinal0 [0x00EFD633+1168947]
    Ordinal0 [0x00F15B35+1268533]
    BaseThreadInitThunk [0x75F9343D+18]
    RtlInitializeExceptionChain [0x77209812+99]
    RtlInitializeExceptionChain [0x772097E5+54]

    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.W3CHandshakeResponse.lambda$errorHandler$0(W3CHandshakeResponse.java:62)
    at org.openqa.selenium.remote.HandshakeResponse.lambda$getResponseFunction$0(HandshakeResponse.java:30)
    at org.openqa.selenium.remote.ProtocolHandshake.lambda$createSession$0(ProtocolHandshake.java:126)
    at java.util.stream.ReferencePipeline$3$1.accept(Unknown Source)
  • 1
    It's not good that you're eating the exception silently in your `Base` class. At least put `e.printStackTrace()` in your catch block. See that the exception you got prints the line number `at base.Base.invokeBrowser(Base.java:53)` - we don't know which line is 53rd, you need to point it to us. – pafau k. May 31 '20 at 12:49
  • At `System.setProperty()`, try the absolute path instead of `System.getProperty("user.dir") + "\\src\\resourc\\driver\\chromedriver.exe");` – Tony May 31 '20 at 15:48
  • @SUPREMESOUL Please point out which line is line number 53 in your code. Also please do a couple of checks, first see if the code is able to access driver paths and secondly why do you need both implicitwait and pageLoadTimeout wait? – Kishore Mohanavelu Jun 01 '20 at 02:36
  • @Tony given solution "absolute path"is not working – SUPREMESOUL Jun 01 '20 at 05:16
  • @KishoreMohanavelu @ pafau k i edited code and with catch the above eception also showing – SUPREMESOUL Jun 02 '20 at 09:44
  • @SUPREMESOUL please refer to the following link for your error https://stackoverflow.com/questions/60296873/sessionnotcreatedexception-message-session-not-created-this-version-of-chrome – Kishore Mohanavelu Jun 02 '20 at 10:03
  • 1
    @SUPREMESOUL Your webdriver session is not getting created. so if this is the case driver will be assigned null value which ultimately throws you null pointer exception. – Kishore Mohanavelu Jun 02 '20 at 10:05
  • @KishoreMohanavelu thanks for explaining, updating chrome driver worked – SUPREMESOUL Jun 03 '20 at 05:00

1 Answers1

1

I've built your code. It works fine; it runs passed that line.

I've used my own chromedriver absolute path.

Might be a typo somewhere for your path. I suggest using pwd in a terminal/powershell in that chromedriver directory.

If not, it's potentially a missing selenium library. Have you downloaded selenium libraries and manually imported them?

if (browsername.equalsIgnoreCase("Chrome")) {
                System.setProperty("webdriver.chrome.driver",
                        "/Users/user/Documents/qac-cv-management-system/AutomatedTestReact/chromedriver");
                driver = new ChromeDriver();//This is line 39 in 
}
Tony
  • 436
  • 1
  • 9
  • 17
  • Selenium is added and now an exception 'org.openqa.selenium.SessionNotCreatedException' also started showing – SUPREMESOUL Jun 02 '20 at 09:41
  • 1
    `This version of ChromeDriver only supports Chrome version 80` Your version of chromedriver doesn't support your current version of google chrome. I suggest using the latest version of [chromedriver.exe](https://sites.google.com/a/chromium.org/chromedriver/downloads) – Tony Jun 02 '20 at 15:36