我已经创建了两个页面对象类,并尝试使用这些页面对象类中的方法单击某些菜单链接,并且我得到了一个空点异常错误以下是代码段。我执行了leftmenulink类文件
leftmenulink class
package leftmenulinks;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import pageclasses.LeftMenuPage;
import pageclasses.LoginPage;
public class LeftMenuLinks {
WebDriver driver;
String baseUrl;
JavascriptExecutor js;
LoginPage login;
LeftMenuPage menu;
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.gecko.driver", "geckodriver-v0.15.0-win32\\geckodriver.exe");
driver = new FirefoxDriver();
login = new LoginPage(driver);
menu = new LeftMenuPage(driver);
baseUrl = "";
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(baseUrl);
}
@Test
public void f() throws Exception {
login.giveUsername();
login.givePw();
login.clickLogin();
Thread.sleep(3000);
System.out.println("done0");
menu.clickCustomerview();
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement elem2 = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='rt-modal']/div/a")));
elem2.click();
menu.clickDashboard();
}
@AfterMethod
public void afterMethod() {
//driver.quit();
}
}
Leftmanupage对象类
package pageclasses;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class LeftMenuPage {
WebDriver driver;
public LeftMenuPage(WebDriver driver){
driver = this.driver;
PageFactory.initElements(driver, this);
}
@FindBy(xpath = "//div[@id='sidebar']/div[3]/div[1]//a[@href='http://']")
WebElement dashboard;
@FindBy(xpath = "//div[@id='sidebar']/div[3]/div[1]//a[@href='http://']")
WebElement customerView;
@FindBy(xpath = "//div[@id='sidebar']/div[3]/div[1]//a[@href='http://']")
WebElement customerFeedback;
@FindBy(xpath = "//div[@id='sidebar']/div[3]/div[1]//a[@href='http://']")
WebElement offerBuilder;
@FindBy(xpath = "//div[@id='sidebar']/div[3]/div[1]//a[@href='http://']")
WebElement newsBuilder;
@FindBy(xpath = "//div[@id='sidebar']/div[3]/div[1]//a[@href='http://']")
WebElement eventsBuilder;
@FindBy(xpath = "//div[@id='sidebar']/div[3]/div[1]//a[@href='http://']")
WebElement smsTemplateBuilder;
@FindBy(xpath = "//div[@id='sidebar']/div[3]/div[1]//a[@href='http://']")
WebElement emailTemplateBuilder;
@FindBy(xpath = "//div[@id='sidebar']/div[3]/div[1]//a[@href='http://']")
WebElement targetListBuilder;
@FindBy(xpath= "//div[@id='sidebar']/div[3]/div[1]//a[@href='http://']")
WebElement campaignManager;
@FindBy(xpath = "//div[@id='sidebar']/div[3]/div[1]//a[@href='http://']")
WebElement referralCampaignManager;
@FindBy(xpath = "//div[@id='sidebar']/div[3]/div[1]//a[@href='http://']")
WebElement loyalty;
@FindBy(xpath = "//div[@id='sidebar']/div[3]/div[1]//a[@href='http://']")
WebElement advertising;
@FindBy(xpath = "//div[@id='sidebar']/div[3]/div[1]//a[@href='http://']")
WebElement products;
@FindBy(xpath = "//div[@id='sidebar']/div[3]/div[1]//a[@href='http://']")
WebElement merchants;
@FindBy(xpath = "//div[@id='sidebar']/div[3]/div[1]//a[@href='http://]")
WebElement partners;
@FindBy(xpath = "//div[@id='sidebar']/div[3]/div[1]//a[@href='http://']")
WebElement contentCreation;
@FindBy(xpath = "//div[@id='sidebar']/div[3]/div[1]//a[@href='http://']")
WebElement tour;
public void clickDashboard() {
dashboard.click();
}
public void clickCustomerview(){
customerView.click();
}
public void clickFeedback(){
customerFeedback.click();
}
public void clickOffer(){
offerBuilder.click();
}
public void clickNews(){
newsBuilder.click();
}
public void clickEvents(){
eventsBuilder.click();
}
public void clickSms(){
smsTemplateBuilder.click();
}
public void clickEmail(){
emailTemplateBuilder.click();
}
public void clickList(){
targetListBuilder.click();
}
public void clickCampaign(){
campaignManager.click();
}
public void clickReferral(){
referralCampaignManager.click();
}
public void clickLoyalty(){
loyalty.click();
}
public void clickAds(){
advertising.click();
}
public void clickProducts(){
products.click();
}
public void clickMerchants(){
merchants.click();
}
public void clickPartners(){
partners.click();
}
public void clickContent(){
contentCreation.click();
}
public void clickTour(){
tour.click();
}
}
登录页面对象类
package pageclasses;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class LoginPage {
WebDriver driver;
@FindBy(id="email")
WebElement inputusername;
@FindBy(id="password")
WebElement inputpw;
@FindBy(xpath="//div[@class='control-group large-12']//input[@value='Login']")
WebElement loginbtn;
public LoginPage(WebDriver driver){
this.driver = driver;
PageFactory.initElements(driver, this);
}
public void giveUsername() {
inputusername.sendKeys("");
}
public void givePw() {
inputpw.sendKeys("");
}
public void clickLogin() {
loginbtn.click();
}
}
错误是
java.lang.NullPointerException
at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
at com.sun.proxy.$Proxy7.click(Unknown Source)
at pageclasses.LeftMenuPage.clickCustomerview(LeftMenuPage.java:78)
at leftmenulinks.LeftMenuLinks.f(LeftMenuLinks.java:47)
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:108)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:744)
at org.testng.TestRunner.run(TestRunner.java:602)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
at org.testng.TestNG.runSuites(TestNG.java:1144)
at org.testng.TestNG.run(TestNG.java:1115)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)