我是 Java 和 Selenium 的新手。 下面是我的 POM、驱动程序实用程序和测试用例的 java 类。
当我将测试用例作为 TestNG 测试运行时,我有: 失败:验证登录 java.lang.NullPointerException:无法调用“org.openqa.selenium.WebElement.sendKeys(java.lang.CharSequence[])”,因为“this.userName”为空。
请帮帮我。如果需要更多信息,请告诉我。 提前致谢。
<----------------------------------------POM-- ------------------------------------------------>< /strong>
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();
}
}
<--------------------------------------测试用例--- ----------------------------------------->< /p>
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;
}
}
答案 0 :(得分:0)
看看这是否有效:-
@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;
答案 1 :(得分:0)
使用initElements初始化POM类的webelements后问题解决:
LoginPage login=PageFactory.initElements(driver, LoginPage.class);
答案 2 :(得分:0)
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.