How can I initialize web driver globally?

32.2k views Asked by At

I am working on automation using selenium webdriver , java. Getting nullpointerexception and it says driver is null.

My code structure is given below :

Package Utility

  • Base Class
  • Login Class
  • App_constant Class

Package Add user

  • Add user Class

Utility package code :

 package Utility;

 import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.chrome.ChromeDriver;

public class Base {

public static WebDriver driver = null;


//CALL WEB BROWSER AND OPEN WEBSITE
public static void openURL()
{

    try{

    System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
    driver = new ChromeDriver();
    driver.get(Constant_value_utility.URL);
    }catch(Exception E)

    {
        E.printStackTrace();


    }
}

 }

package Utility;

public class Constant_value_utility {


//OPEN URL
public static final String URL = "Site URL";


//LOGIN FIELDS
public static final String loginbox = "UserName";
public static final String passbox = "Password";

//LOGIN DATA
public static final String username = "test";
public static final String password = "test";
public static final String loginbt = "btnsubmit";

  }

 package Utility;

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


 public class Login_Page {

public static WebDriver driver;


public static void login()
{

       Base.openURL();


       driver.manage().window().maximize();

       driver.findElement(By.id(Constant_value_utility.loginbox)).sendKeys(Constant_value_utility.username);
       driver.findElement(By.id(Constant_value_utility.passbox)).sendKeys(Constant_value_utility.password);
       driver.findElement(By.id(Constant_value_utility.loginbt)).click();

  }

}


Add user Package code

package Adduser;
import Utility.Base;
 import Utility.Login_Page;

 import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.chrome.ChromeDriver;

 public class Add_User {

  public static void main(String[] args){
   {


    //LOGIN TO SITE
     Base.openURL();
     Login_Page.login();
    }}}

Now My question is I have already created public static method openurl() in base class and webdriver is initialized there. But When I call same method in other class of same package and other packages , Why it is giving me nullpointerexception for webdriver?

Is that necessary to write code to initialize webdriver and call browser in every class. How can I initialize web driver globally so I declare it once and can call any where in my project.

1

There are 1 answers

7
Manuel Jain On BEST ANSWER

your webDriver is declared in the other class too, so

   Base.openURL();


   driver.manage().window().maximize();

driver is not initialized here.

try to rewrite your Base.openUrl() method in order to return the webDriver

edit: your class fields are visible to other classes, thats true. but in order to get the one from the correct class you should try something like Base.driver because Base.driver != Login_Page.driver

edit2: here is one example of how a working class could look like

   package Utility;

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


   public class Login_Page {

   //public static WebDriver driver;


   public static void login()
   {

   Base.openURL();

   //note the change from driver  to Base.driver
   Base.driver.manage().window().maximize();
   Base.driver.findElement(By.id(Constant_value_utility.loginbox)).sendKeys(Constant_value_utility.username);
   Base.driver.findElement(By.id(Constant_value_utility.passbox)).sendKeys(Constant_value_utility.password);
   Base.driver.findElement(By.id(Constant_value_utility.loginbt)).click();

}