0

I was using Pojo class in my project and i have used PageFactory.initElements to initialize the elements. But when i try to sendkeys or click the element it throws null poniter exception. Please help me to get through this one.. thank you

I tried with pagefactory class to intialize the elemets. I except the element is working properly without any exception

MY POJO Class: public class LoginPojo extends BaseClass {

public LoginPojo() {
    PageFactory.initElements(driver, this);
}

@FindBy(id = "username")
private WebElement txtUser;
@FindBy(id = "password")
private WebElement txtPass;
@FindBy(id="login")
private WebElement btnLogin;

public WebElement getTxtUser() {
    return txtUser;
}
public WebElement getTxtPass() {
    return txtPass;
}
public WebElement getBtnLogin() {
    return btnLogin;
}

}

My main Class: public class AdactinHotel extends BaseClass {

LoginPojo loginPojo = new LoginPojo();

@Test(priority = 0)
public void launchBrowser() throws IOException {
    driverSetup(getProjectFileValue("browser"));
}

@Test(priority = 1)
public void urlSetup() throws IOException, InterruptedException {
    launchURL(getProjectFileValue("url"));
    maximizeWindow();
    Thread.sleep(2000);
}

@Test(priority = 2)
public void login() throws IOException {
    elementSendKeys(loginPojo.getTxtUser(), getProjectFileValue("user"));
    elementSendKeys(loginPojo.getTxtPass(), getProjectFileValue("pass"));
    elementClick(loginPojo.getBtnLogin());
}

}

BaseClass: public class BaseClass {

public WebDriver driver;
public void driverSetup(String browser) {
    switch (browser) {
    case "chrome":
        WebDriverManager.chromedriver().setup();
        ChromeOptions ops = new ChromeOptions();
        ops.addArguments("--remote-allow-origins=*");
        driver = new ChromeDriver(ops);
        break;

    case "edge":
        driver = new EdgeDriver();
        break;
        
    default:
        break;
    } 
    WebDriverManager.chromedriver().setup();
    ChromeOptions ops = new ChromeOptions();
    ops.addArguments("--remote-allow-origins=*");
    driver = new ChromeDriver(ops);
}

public void launchURL(String url) {
    driver.get(url);
}

public void maximizeWindow() {
    driver.manage().window().maximize();
}

public void elementSendKeys(WebElement element, String text) {
            element.sendKeys(text);
}

public void elementClick(WebElement element) {
    element.click();
}

public String getProjectPath() {
    String property = System.getProperty("user.dir");
    return property;
}
 
public String getProjectFileValue(String key) throws IOException {
    Properties properties = new Properties();
    FileInputStream inputStream = new FileInputStream(getProjectPath()+"\\Configuration\\config.properties");
    properties.load(inputStream);
    Object object = properties.get(key);
    String value = (String)object;
    System.out.println(value);
    return value;
}

Exception: [RemoteTestNG] detected TestNG version 6.14.3 chrome SLF4J: No SLF4J providers were found. SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See https://www.slf4j.org/codes.html#noProviders for further details. Dec 11, 2024 11:28:08 AM org.openqa.selenium.devtools.CdpVersionFinder findNearestMatch WARNING: Unable to find CDP implementation matching 131 Dec 11, 2024 11:28:08 AM org.openqa.selenium.chromium.ChromiumDriver lambda$new$5 WARNING: Unable to find version of CDP to use for 131.0.6778.109. You may need to include a dependency on a specific version of the CDP using something similar to org.seleniumhq.selenium:selenium-devtools-v86:4.12.0 where the version ("v86") matches the version of the chromium-based browser you're using and the version number of the artifact is the same as Selenium's. Dec 11, 2024 11:28:09 AM org.openqa.selenium.devtools.CdpVersionFinder findNearestMatch WARNING: Unable to find CDP implementation matching 131 Dec 11, 2024 11:28:09 AM org.openqa.selenium.chromium.ChromiumDriver lambda$new$5 WARNING: Unable to find version of CDP to use for 131.0.6778.109. You may need to include a dependency on a specific version of the CDP using something similar to org.seleniumhq.selenium:selenium-devtools-v86:4.12.0 where the version ("v86") matches the version of the chromium-based browser you're using and the version number of the artifact is the same as Selenium's. https://adactinhotelapp.com null PASSED: launchBrowser PASSED: urlSetup FAILED: login java.lang.NullPointerException at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:68) at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38) at com.sun.proxy.$Proxy7.sendKeys(Unknown Source) at com.baseclass.BaseClass.elementSendKeys(BaseClass.java:50) at com.adactin.main.AdactinHotel.login(AdactinHotel.java:30) 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:114) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

=============================================== Default test Tests run: 3, Failures: 1, Skips: 0

=============================================== Default suite Total tests run: 3, Failures: 1, Skips: 0

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.