错误 - "Cannot invoke "org.openqa.selenium.WebElement.sendKeys(java.lang.CharSequence[])" 因为 "this.UserName" 为空

Error - "Cannot invoke "org.openqa.selenium.WebElement.sendKeys(java.lang.CharSequence[])" because "this.UserName" is null

我是 Java 和 Selenium 的新手。 下面是我的 java 类 的 POM,Driver 实用程序和测试用例。

当我 运行 将测试用例作为 TestNG 测试时,我有: 失败:验证登录 java.lang.NullPointerException:无法调用“org.openqa.selenium.WebElement.sendKeys(java.lang.CharSequence[])”,因为“this.userName”为空。

请帮帮我。让我知道是否需要更多信息。 提前致谢。

<------------------------------------ -POM-------------------------------------------- -->

package com.inetBankingV1.pageObjects;

import org.openqa.selenium.WebDriver;

import com.inetBankingV1.utilities.callBrowserDriver;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;

public class LoginPage {
    
    WebDriver driver;
    
    @FindBy(how=How.LINK_TEXT, using="Log in")
    WebElement logUser;
    
    @FindBy(how=How.NAME, using="username")
    WebElement userName;
    
    @FindBy(how=How.NAME, using="password")
    WebElement passWord;
    
    
    //constructor
    public LoginPage(WebDriver driver)
    {
        this.driver=driver;
    }
    
    public  void loginCheck(String uname,String passwd)
    {
        userName.sendKeys(uname);
        passWord.sendKeys(passwd);
        logUser.click();
    }

}

<------------------------------------ -Driver---------------------------------------- ---->

    package com.inetBankingV1.utilities;

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.firefox.FirefoxOptions;
import org.openqa.selenium.remote.DesiredCapabilities;

public class callBrowserDriver {
    
    public WebDriver startApplication(String browser, String baseURL, WebDriver driver)
    
    {
    
    if(browser.equalsIgnoreCase("firefox"))
            {
        System.setProperty("webdriver.gecko.driver", "./Drivers/geckodriver.exe");
        FirefoxOptions firefoxOptions = new FirefoxOptions();
        firefoxOptions.setCapability("marionette", true);
         driver = new FirefoxDriver(firefoxOptions);
         
            }
    
    if(browser.equalsIgnoreCase("chrome"))
            {
        System.setProperty("webdriver.chrome.driver", "./Drivers/chromedriver.exe");
        driver=new ChromeDriver();
            }
    
    driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
    driver.manage().window().maximize();
    driver.navigate().to(baseURL);
    
    return driver;

}
    
}

<------------------------------------测试案例-------------------------------------------->

    package com.inetBankingV1.testCases;

import com.inetBankingV1.utilities.callBrowserDriver;

import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;

import com.inetBankingV1.pageObjects.LoginPage;

public class TC2_VerifyLogin {

    WebDriver driver;
    callBrowserDriver browserDriver=new callBrowserDriver();

    @Test
    public void verifyLogin()
    {
        
        driver=browserDriver.startApplication("chrome","https://demosite.com/",this.driver);
        LoginPage login=new LoginPage(driver);

    login.loginCheck("admin","Password");   
    }
}

看看这是否有效:-

 @FindBy(how=How.ID, using="inline-search-submit")
 WebElement logUser;
    
 @FindBy(how=How.NAME, using="user")
 WebElement userName;
    
 @FindBy(how=How.NAME, using="pass")
 WebElement passWord;
    

使用 initElements:

初始化 POM class 的网络元素后问题得到解决

登录页面登录=PageFactory.initElements(驱动程序,LoginPage.class);

Use below line in your constructor
PageFactory.initElements( driver, this);

----------------------------------------------

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

This should solve your problem.