-1

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)
  • Can you please format your code and post? – dzieciou Dec 27 '19 at 8:30
  • I can see my code formatted only. What do you mean by formatting it , can you explain? – Ravi Kumar Dec 27 '19 at 8:59
  • The code has been formatted right now by João Farias. You can learn how to format by the code at: meta.stackexchange.com/questions/22186/… You can add language-specific tag (like java) to have your syntax hightlighted. – dzieciou Dec 27 '19 at 9:36
  • 1
    Your NPE is on the line 38 of the loginApp method. Which line is it? loginLink.click();, username.sendKeys(emailId);, password.sendKeys(passWord);, or loginButton.click()? – João Farias Dec 27 '19 at 9:36
  • 1
    NPE is occurring on loginLink.click(); – Ravi Kumar Dec 27 '19 at 10:32
0

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);
  • Hi Anupam, I tried that also but still getting the same error. Can you please give me detailed answer. – Ravi Kumar Dec 28 '19 at 19:43
  • I saw the code but still not working – Ravi Kumar Dec 28 '19 at 19:52
0

I can figure out these things

1- You have not instantiated pageFactory in LoginPage class. Please do it using below

      public LoginPage(WebDriver driver){

      PageFactory.initElements(driver, this);
      }  

2-In loginToCRM class create object of LoginPage as given below.

     LoginPage loginpage = new LoginPage(driver);

3-Can you also try in LoginPage class

 @FindBy(xpath= "//a[href=\"https://ui.freecrm.com\"]") 
 WebElement loginLink;

behalf of this

 @FindBy(how = How.XPATH, using = "//a[href=\"https://ui.freecrm.com\"]") 
 WebElement loginLink

try above steps simultaneously.Hope it will be helpfull. If not then please let us know line no 38 in LoginPage class.

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.