0

TestBase which contains the intialization method which is creating problem :

package TestBase;

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

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.events.EventFiringWebDriver;

    public class testBasesetup {
        public static Properties prop;
        public static WebDriver driver;
        public static testBasesetup testBaseObj;
        public  static EventFiringWebDriver e_driver;

        public testBasesetup() throws Exception 
        {   
            try {
                prop = new Properties();
                File src = new File("C:\\Users\\LENOVO\\eclipse-workspace\\Demon\\src\\main\\java\\Config\\config.properties");
                FileInputStream ip = new FileInputStream(src) ;

                prop.load(ip);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public static void initialization() throws Exception 
        {
            String browsername = prop.getProperty("browser");
            if(browsername.equals("chrome")) 
            {
                System.setProperty("webdriver.chrome.driver", "F:\\Eclipse\\chromedriver.exe");
                WebDriver driver = new ChromeDriver();

            }
            else if(browsername.equals("firefox"))
            {
                System.setProperty("webdriver.gecko.driver", "F:\\Eclipse\\browser\\geckodriver.exe");
                WebDriver driver = new FirefoxDriver();
            }   

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

            String URLtoTest = prop.getProperty("URL");
            Thread.sleep(1000);
        }

    }

Test Class which I am running and this is creating issue. I guess there is some issue with driver variable but don't know the error:

    package PageTest;

    import org.testng.annotations.AfterMethod;
    import org.testng.annotations.AfterTest;
    import org.testng.annotations.BeforeMethod;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.Test;

    import Pages.LoginPage;
    import TestBase.testBasesetup;

    public class LoginPageTest extends testBasesetup{
        LoginPage LoginPageObj;
        testBasesetup testBaseObj;
        public LoginPageTest() throws Exception 
        {
            super();
        }

        @BeforeMethod
        public void setup() throws Exception 
        {
            initialization();
            LoginPageObj = new LoginPage();
            Thread.sleep(2000);
        }

        @Test()
        public void LoginCheck() throws Exception 
        {
            LoginPageObj.login("first.customer@kkr.com","Potatok");
        }


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

Image of error:

Image of error Code

Justin Ko
  • 46,526
  • 5
  • 91
  • 101
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Guy Nov 10 '19 at 08:39

1 Answers1

0

Your static instance of WebDriver remain uninitialized. You need to initialize static instance instead of local driver instance in initialization() method. Like Below:

public class testBasesetup {
        public static Properties prop;
        public static WebDriver driver;
        public static testBasesetup testBaseObj;
        public  static EventFiringWebDriver e_driver;

        public testBasesetup() throws Exception 
        {   
            try {
                prop = new Properties();
                File src = new File("C:\\Users\\LENOVO\\eclipse-workspace\\Demon\\src\\main\\java\\Config\\config.properties");
                FileInputStream ip = new FileInputStream(src) ;

                prop.load(ip);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public static void initialization() throws Exception 
        {
            String browsername = prop.getProperty("browser");
            if(browsername.equals("chrome")) 
            {
                System.setProperty("webdriver.chrome.driver", "F:\\Eclipse\\chromedriver.exe");
                **driver = new ChromeDriver();**

            }
            else if(browsername.equals("firefox"))
            {
                System.setProperty("webdriver.gecko.driver", "F:\\Eclipse\\browser\\geckodriver.exe");
                **driver = new FirefoxDriver();**
            }   

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

            String URLtoTest = prop.getProperty("URL");
            Thread.sleep(1000);
        }

    }
Mrunal Gosar
  • 4,595
  • 13
  • 48
  • 71