How to resolve null pointer exception where running class using testng test. Trying to use three classes to make framework for automation. I tried calling "driver.get(baseURL);" in setup of base class. it worked, but stuck at next method to enter user name. any help as stuck at one step?
//base class
package com.crmnext.testCases;
import java.time.Duration;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import io.github.bonigarcia.wdm.WebDriverManager;
public class BaseClass {
public String baseURL = "https://my.crm.com/crmnext/login/login";
public String username = "[email protected]";
public String password = "abc11";
public static WebDriver driver = null;
@BeforeClass
public void setUp ()
{
System.out.println("in setup");
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().window().maximize();
}
@AfterClass
public void tearDown()
{
// driver.close();
driver.quit();
}
}
//test case class
package com.crmnext.testCases;
import org.testng.annotations.Test;
import com.crmnext.pageObjects.LoginPage;
public class TC1_LoginTest_001 extends BaseClass
{
@Test
public void loginTest()
{
driver.get(baseURL);
LoginPage lp = new LoginPage(driver);
lp.setUserName(username);
lp.setPwd(password);
lp.clickLogin();
}
}
//Page class
package com.crmnext.pageObjects;
import java.time.Duration;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class LoginPage {
WebDriver ldriver;
public LoginPage(WebDriver rdriver)
{
System.out.println("In Login Page object class");
ldriver = rdriver;
PageFactory.initElements(ldriver, this);
}
@FindBy(css="#TxtName")
@CacheLookup
WebElement txtUsername;
@FindBy (css = "[name='SecureTextBox.Text']")
@CacheLookup
WebElement txtPassword;
@FindBy (css ="body.bg-light-gray:nth-child(2) div.body-content.min-vh-100.login-background.h-100:nth-child(1) div.flexbox-row.w-100 div.login-box.relative div.login-container.flexbox-row.items-center.min-vh-100.h-100:nth-child(2) div.flex-1.login-box--inner div:nth-child(1) div.login-form form:nth-child(1) > input.login-button.button.w-100:nth-child(4)")
@CacheLookup
WebElement btnLogin;
//Function to enter user name
public void setUserName (String uname)
{
System.out.println("in set user name " uname);
ldriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
txtUsername.sendKeys(uname);
System.out.println("After enter in user name");
}
//Function to Enter password
public void setPwd (String pwd)
{
System.out.println("in set password");
txtPassword.sendKeys(pwd);
}
// Function to click button
public void clickLogin ()
{
btnLogin.click();
}
}
Getting null pointer exception. Tried similar queries posted. But, none helped.
FAILED: loginTest
java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.get(String)" because "com.crmnext.testCases.TC1_LoginTest_001.driver" is null
It seems driver is getting null.
CodePudding user response:
There is local driver
variable in setUp
method which is initialized but it is lost upon finishing the setUp
method. Thus, static field driver
remains set to null
and this causes NPE when the static field is referenced.
Fix setUp
to remove declaration of local variable:
public void setUp () {
System.out.println("in setup");
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().window().maximize();
}
CodePudding user response:
You have multiple (two) WebDriver instances initialized at different levels.
One at the class (
BaseClass
) level as:public static WebDriver driver = null;
Another at the method (
setUp()
) level as:WebDriver driver = new ChromeDriver();
Once the program execution comes out of setUp()
method, the BaseClass
level WebDriver instance is accesed which is NULL. hence you face nullpointerexception
Solution
Declare the WebDriver instance at the class level as it is:
public static WebDriver driver;
At the setUp()
method level use the same WebDriver instance as:
driver = new ChromeDriver();
CodePudding user response:
It worked post answer from Alex.
Base class needed updates.
@BeforeClass public void setUp () { System.out.println("in setup"); WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().window().maximize();
}