我是Selenium网络驱动程序的新手。 我试图创建Selenium Web驱动程序框架。但由于此空指针异常,我一直被困。 我创建了一个Testcase类,Base类和一个Page Object类。
TestCase类://类名
package com.uiautomation.com.uiautomation.testscripts;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
import com.uiautomation.base.LoginBase;
import com.uiautomation.setup.setBrowser;
public class LoginTest{
WebDriver driver;
setBrowser browser= new setBrowser();
@BeforeClass
public void beforeClass() {
browser.setupwebdriver("FF");
}
@Test()
public void loginPage() {
LoginBase base = new LoginBase(driver);//create a new instance of Login base class
base.loginToDummySite("Vishalbha", "abc123");//call base class method and pass parameters value
}
@AfterClass
public void afterClass() {
}
}
基类:
package com.uiautomation.base;
import org.openqa.selenium.WebDriver;
import com.uiautomation.pageobjects.LoginPO;
public class LoginBase {
WebDriver driver;
public LoginBase(WebDriver driver){
this.driver= driver;
}
public void loginToDummySite(String UserName, String Password){
System.out.println(UserName);
LoginPO loginPO= new LoginPO(driver);// create instance of loginPO class
loginPO.enterUserName(UserName);
loginPO.enterPassword(Password);
loginPO.loginButtonClick();
}
}
页面对象类:
package com.uiautomation.pageobjects;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class LoginPO {
WebDriver driver;
public LoginPO(WebDriver driver){
PageFactory.initElements(driver, this);
this.driver= driver;
}
@FindBy(id = "username")
private static WebElement userNameTextbox;
@FindBy(id="password")
private WebElement passwordTextbox;
@FindBy(id="login")
private WebElement loginButton;
public void enterUserName(String UserName){
userNameTextbox.clear();
userNameTextbox.sendKeys(UserName);
}
public void enterPassword(String Password){
passwordTextbox.clear();
passwordTextbox.sendKeys(Password);
}
public void loginButtonClick(){
loginButton.click();
}
}
执行我的脚本时,网站打开但是" userNameTextbox"页面对象获取错误空指针异常。 我正在使用selenium web驱动程序2.53.0。 请帮我解决这个问题。