0

Pls help me with this error "java.lang.NullPointerException". Chrome driver is getting initiaze properly but at the time of running test it is giving this error.
In my test class I'm extending setup class which extends driver class.
This way i m using same driver instance in all of my classes. Then why this exception is occurring? Below is my code

 public class GetDriver {

    public WebDriver driver;
    public WebDriver getD(){
          return driver;
            }
        }

  public class util{   

       public static void launchbrowser(WebDriver driver ){        
            System.setProperty("webdriver.chrome.driver","path to chromedriver.exe");
            driver = new ChromeDriver();                
        }
       public static void login(String user, String password, WebDriver driver) throws Exception{
        LoginPage loginpage = PageFactory.initElements(driver, LoginPage.class);
        loginpage.loginid(user, password);
        loginpage.clicksubmit();        
       }    
    }

   public class Setup extends GetDriver {

        @BeforeClass
        public void startTest(){                
            util.launchbrowser(url, driver);
            driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);  
        }        
    }

   public class login extends Setup {

    @Test(priority=0) 
    public void TestLogin() throws Exception{
        String url = "website";
        driver.get(url);
        util.login("website","password",driver);    

        boolean bool = driver.findElement(By.xpath("//span[contains(text(),'Your username or password is incorrect')]")).isDisplayed();     
        assertTrue(!bool,"password incorrect");

    }
}
  • 2
    Could share the error stacktrace, so we can undertand where the NEP originates? – João Farias Feb 9 at 12:55
1

This is a standard Java question. I believe the problem lies here:

    public static void launchbrowser(WebDriver driver ){        
        System.setProperty("webdriver.chrome.driver","path to chromedriver.exe");
        driver = new ChromeDriver();                
    }

All this does is overwrite the parameter you passed in to this. Instead, put this into the base class.

public class GetDriver {

    protected final WebDriver driver; // protected allows for subclasses to access

    public GetDriver {
        if(System.getProperty("webdriver.chrome.driver") == null) {
            System.setProperty("webdriver.chrome.driver", "path.to.driver");
        }
        this.driver = new ChromeDriver();
    }
    public WebDriver getDriver(){
          return driver;
    }
}

And use it later as you currently are.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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