Getting java.lang.NullPointerException while trying to instantiate the Webdriver driver under @ Annotation



  • Please find the code below and let me know why am I getting NullPointer. I am having no clue.

    package com.seleniumhybridframework.utility;
    
    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.ie.InternetExplorerDriver;
    
    public class BrowserFactory {
    
        public BrowserFactory() {
            super();
    
        }
    
        public static WebDriver startApplication(WebDriver driver, String browser, String appURL) {
    
        if(browser.equals("Chrome")) {
            System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
            driver = new ChromeDriver();
        }   
    
        else if(browser.equals("Firefox")) {
            System.setProperty("webdriver.gecko.driver", "./drivers/geckodriver.exe");
            driver = new FirefoxDriver();       
        }
    
        else if(browser.equals("InternetExplorer")) {
            System.setProperty("webdriver.IE.driver", "./drivers/IE.exe");
            driver = new InternetExplorerDriver();      
        }
    
        else
            System.out.println("Provided browser is invalid");
    
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get(appURL);
        driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
    
        return driver;
        }
    
        public static void closeBrowser(WebDriver driver) {
            driver.quit();
        }
    }
    

    LoginPage:

    package com.seleniumhybridframework.pages;
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.FindBy;
    import org.openqa.selenium.support.How;
    
    
    public class LoginPage {
    
    
        WebDriver driver;
    
        public LoginPage(WebDriver ldriver) {
            this.driver = ldriver;
    
        }
    
    
    
        @FindBy(how = How.XPATH, using = "//a[href=\"https://ui.freecrm.com\"]") WebElement loginLink;
        @FindBy(how=How.NAME, using="email") WebElement username;
        @FindBy(how=How.NAME, using="password") WebElement password;
        @FindBy(how=How.XPATH, using="//div[text()=\"Login\"]") WebElement loginButton;
    
        public void loginApp(String emailId, String passWord) {
    
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                System.out.println("Timed Out");
            }
    
            loginLink.click();
            username.sendKeys(emailId);
            password.sendKeys(passWord);
            loginButton.click();
    
        }
    
    }
    

    BaseClass:

    package com.seleniumhybridframework.pages;
    
    import org.openqa.selenium.WebDriver;
    import org.testng.ITestResult;
    import org.testng.annotations.AfterClass;
    import org.testng.annotations.AfterMethod;
    import org.testng.annotations.BeforeClass;
    import org.testng.annotations.BeforeSuite;
    
    import com.seleniumhybridframework.utility.BrowserFactory;
    import com.seleniumhybridframework.utility.ConfigDataProvider;
    import com.seleniumhybridframework.utility.ExcelReader;
    import com.seleniumhybridframework.utility.Helper;
    
    public class BaseClass {
    
        public WebDriver driver;
        public ExcelReader excel;
        public ConfigDataProvider config;
    
        @BeforeSuite
        public void setupSuite() {
            excel = new ExcelReader("./testData/", "Data1.xlsx");
            config = new ConfigDataProvider("./configFiles/config.properties");
        }
    
        @BeforeClass
        public void setup() {
            System.out.println(config.getValue("appURL"));
            BrowserFactory.startApplication(driver, config.getBrowser(), config.getValue("appURL"));
        }
    
        @AfterClass
        public void teardown() {
            BrowserFactory.closeBrowser(driver);
        }
    
        @AfterMethod
        public void tearDownMethod(ITestResult result) {
            if(result.getStatus()==ITestResult.FAILURE) {
                Helper.captureScreenshot(driver);
            }
    
    
        }
    
    }
    

    LoginCRM:

    package com.seleniumhybridframework.testCases;
    
    
    import org.openqa.selenium.support.PageFactory;
    import org.testng.annotations.Test;
    
    import com.seleniumhybridframework.pages.BaseClass;
    import com.seleniumhybridframework.pages.LoginPage;
    
    
    public class LoginCRM extends BaseClass{
    
    
    
        @Test
        public void loginToCRM() {
    
            LoginPage loginpage = PageFactory.initElements(driver, LoginPage.class);
            loginpage.loginApp(excel.getStringData(0, 0, 0), excel.getStringData(0, 0, 1));
    
        }
    }
    

    Error Result--

    FAILED: loginToCRM
    java.lang.NullPointerException
        at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
        at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
        at com.sun.proxy.$Proxy11.click(Unknown Source)
        at com.seleniumhybridframework.pages.LoginPage.loginApp(LoginPage.java:38)
        at com.seleniumhybridframework.testCases.LoginCRM.loginToCRM(LoginCRM.java:19)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
        at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
        at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
        at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
        at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
        at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
        at org.testng.TestRunner.privateRun(TestRunner.java:648)
        at org.testng.TestRunner.run(TestRunner.java:505)
        at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
        at org.testng.SuiteRunner.run(SuiteRunner.java:364)
        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
        at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
        at org.testng.TestNG.runSuites(TestNG.java:1049)
        at org.testng.TestNG.run(TestNG.java:1017)
        at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
        at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
        at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)
    


  • You need to instantiate driver in BaseClass.

    You need to intialize the elements using PageFactory class in LoginPage constructor.

    public LoginPage(WebDriver driver) {           this.driver = driver; PageFactory.initElements(driver, this); }
    

    Create LoginPage class object in test as

    LoginPage loginpage = new LoginPage(driver);
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2