0

I am using testNG and selenium Web Driver and I am always facing the issue with passing the driver instance. Here, the BaseClass is used to have my driver setup and which has @BeforeSuite annotations to call before all the things. But when the Test Class is executing, the driver value is getting NULL and its throwing an error.

 public class BaseClass {

    @BeforeSuite
    public void setup() {

        System.out.println("Initializing the driver");
        System.setProperty("webdriver.chrome.driver", "/Users/vinoth/Git/chromedriver");
        WebDriver driver = new ChromeDriver();
        driver.get("https://my.yocoboard.com");
    }

    @AfterSuite
    public void tearDown() {

        System.out.println("Quiting Everything");
    }

The Above class is my Base Class where I have initiated my driver setup.

    public class FirstClassYoCo extends BaseClass {

    WebDriver driver;
    WebDriverWait wait;
    SoftAssert softAssert;

    @Test(priority = 1, groups = "function")
    public void validateCurrentWeek() {

        //driver.get("https://my.yocoboard.com");
        driver.findElement(By.id("email")).sendKeys("vinothselvam070@gmail.com");
        driver.findElement(By.id("password")).sendKeys("vnotbs01");
        driver.findElement(By.xpath("//span[text()='Sign In']")).click();
        wait = new WebDriverWait(driver, 30);
        wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//div[@id='onLoadSpinner']/img")));
    }

The above one is my test class but while executing the first line of the "FirstClassYoCo", It throwing an Null pointer exception. The following one is the error statement

Starting ChromeDriver 87.0.4280.20 (c99e81631faa0b2a448e658c0dbd8311fb04ddbd-refs/branch-heads/4280@{#355}) on port 41085
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
[1613131336.877][WARNING]: This version of ChromeDriver has not been tested with Chrome version 88.
Feb 12, 2021 5:32:16 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Login to the application

java.lang.NullPointerException
    at org.yocotest.FirstClassYoCo.validateCurrentWeek(FirstClassYoCo.java:26)
    
PDHide
  • 18,113
  • 2
  • 31
  • 46
Vinoth BS
  • 21
  • 4
  • 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) – Alexey R. Feb 12 '21 at 13:26

3 Answers3

0

Try

static WebDriver driver; in BaseClass and use driver = new ChromeDriver(); in before setup.

In test case dont use Webdriver driver.

Issue will be resolved but better to use thread driver which helps in parallel execution.. TestNG: Parallel Execution: Only active window complete the test

murali selenium
  • 3,847
  • 2
  • 11
  • 20
0

in your BaseClass , there is no class variable webdriver. The variable is defined as local variable for the setup() method. So that is not available for the FirstClassYoCo class.

Also it is not in a constructor.

 public class BaseClass {

    @BeforeSuite
    public void setup() {

        System.out.println("Initializing the driver");
        System.setProperty("webdriver.chrome.driver", "/Users/vinoth/Git/chromedriver");
        WebDriver driver = new ChromeDriver();
        driver.get("https://my.yocoboard.com");
    }

    @AfterSuite
    public void tearDown() {

        System.out.println("Quiting Everything");
    }

Solution is :

 public class BaseClass {
    public WebDriver driver;

    @BeforeSuite
    public void setup() {

        System.out.println("Initializing the driver");
        System.setProperty("webdriver.chrome.driver", "/Users/vinoth/Git/chromedriver");
        driver = new ChromeDriver();
        driver.get("https://my.yocoboard.com");
    }

    @AfterSuite
    public void tearDown() {

        System.out.println("Quiting Everything");
    }
PDHide
  • 18,113
  • 2
  • 31
  • 46
0

The reason for null pointer is your class variable driver is not initialized. In order to fix it move class variable driver in base class and initialized it. For example:

public class BaseClass {
   protected static WebDriver driver;
   
   @BeforeSuite
   public void setup() {
        driver = new ChromeDriver();
        driver.get("https://my.yocoboard.com");
    }
}

This approach of having driver class variable is not recommended. There are opensource frameworks available as third party extensions to TestNG that provides driver management and other features with TestNG. You can start using any of one. I would recommend QAF that provides driver management, assertion and verification, resource management, locator repository, detailed reporting and more.

Your test case may look like below by just extending base class from QAF.

public class FirstClassYoCo extends WebDriverTestCase {

    @Test(priority = 1, groups = "function")
    public void validateCurrentWeek() {

        getDriver().get("https://my.yocoboard.com");
        //getDriver() .findElement(By.id("email")).sendKeys("vinothselvam070@gmail.com");
        getDriver().findElement("id=email").sendKeys("vinothselvam070@gmail.com");                      
        getDriver().findElement("id=password").sendKeys("vnotbs01");
        getDriver().findElement("xpath=//span[text()='Sign In']")).click();
        getDriver().findElement("xpath=//div[@id='onLoadSpinner']/img")).waitForNotVisible();
    }
user861594
  • 5,733
  • 3
  • 29
  • 45