错误 - “无法调用“org.openqa.selenium.WebElement.sendKeys(java.lang.CharSequence[])”,因为“this.UserName”为空

问题描述

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

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

请帮帮我。如果需要更多信息,请告诉我。 提前致谢。

fn example<'a>(text: &'a str) -> impl Iterator<Item = &'a str> + 'a {
    let mut i = 0;
    iter::from_fn(move || {
        if i <= text.len() {
            let chunk = &text[..i];
            i += 1;
            Some(chunk)
        } else {
            None
        }
    })
}

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();
    }

}

    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;

}
    
}

解决方法

看看这是否有效:-

 @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类的webelements后问题解决:

LoginPage login=PageFactory.initElements(driver,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.

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...