我正在做一些编码来学习PageFactory,但是在尝试使用page factory调用另一个类中的方法时却遇到此错误
下面是我的Elements类:
package pulse.pom.tpr;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;
public class PulseElements {
WebDriver driver;
public Actions actions;
// Login Page Elements
@FindBy(how = How.NAME, using = "userId")
WebElement userid;
@FindBy(name = "password")
WebElement password;
@FindBy(name = "dcNumber")
WebElement dcnbr;
@FindBy(css = "body > ion-app > ng-component > ion-nav > page-login > ion-content > div.scroll-content > ion-card > ion-grid > form > ion-list > div.login-button > button > span")
WebElement login;
// TPR button Element
@FindBy(id = "tab-t0-2")
WebElement tpr;
// Send Associate button
@FindBy(xpath = "//*[@id='tabpanel-t0-2']/tpr-summary-page/ion-header[2]/ion-grid/ion-row/ion-col[3]/ion-row/button[1]")
WebElement sendasc;
// Next Button after selecting associate
@FindBy(xpath = "//*[@id='footers']/ion-toolbar/div[2]/ion-row/ion-col[2]/button")
WebElement next1;
// Next Button after selecting Area
@FindBy(xpath = "//*[@id='tabpanel-t0-2']/tpr-send-associates-page/div/ion-footer/button")
WebElement next2;
// Clockin Button
@FindBy(xpath = "//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-content/div[2]/div/div/ion-row[2]/ion-col/div[1]")
WebElement clockin;
public PulseElements(WebDriver driver) {
this.driver = driver;
actions = new Actions(driver);
}
@Test(priority = 1)
public void pulseLogin(String uid, String pwd, String dc) {
actions.moveToElement(userid).click().sendKeys(uid);
actions.build().perform();
actions.pause(java.time.Duration.ofSeconds(1));
actions.moveToElement(password).click().sendKeys(pwd);
actions.build().perform();
actions.pause(java.time.Duration.ofSeconds(1));
actions.moveToElement(dcnbr).click().sendKeys(dc);
actions.build().perform();
actions.pause(java.time.Duration.ofSeconds(1));
actions.moveToElement(login).click();
actions.build().perform();
}
@Test(priority = 2)
public void tprClick() {
actions.moveToElement(tpr).click();
actions.build().perform();
}
@Test(priority = 3)
public void sendAssociateButton() {
actions.moveToElement(sendasc).click();
actions.build().perform();
}
public void selectNext1() {
actions.moveToElement(next1).click();
actions.build().perform();
}
public void selectNext2() {
actions.moveToElement(next2).click();
actions.build().perform();
}
public void selectClockin() {
actions.moveToElement(clockin).click();
actions.build().perform();
}
}
下面是我的登录页面的第一个测试类:
package pulse.pom.tpr;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.Test;
public class PulseLogin {
public WebDriver driver;
//public PulseElements locateElements=PageFactory.initElements(driver, PulseElements.class);
@Test(priority=1)
public void pulseLoginPage() throws InterruptedException
{
System.setProperty("webdriver.chrome.driver",
"C:\\MyChromeDriver\\chromedriver_win32\\chromedriver.exe");
driver=new ChromeDriver();
driver.get("https://mysite/");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
PulseElements locateElements=PageFactory.initElements(driver, PulseElements.class);
locateElements.pulseLogin("sysadmin", "1234#", "7036");
locateElements.tprClick();
locateElements.sendAssociateButton();
}
@Test(priority=2)
public void selectAssociate() {
System.out.println("Please select any associate");
Scanner asc = new Scanner(System.in);
asc.close();
}
@Test(priority=3)
public void selectNextButton1(){
PulseElements locateElements=PageFactory.initElements(driver, PulseElements.class);
locateElements.selectNext1();
}
@Test(priority = 4)
public void selectArea() {
System.out.println("Please select area");
Scanner area = new Scanner(System.in);
area.close();
}
@Test(priority=5)
public void selectNextButton2()
{
PulseElements locateElements=PageFactory.initElements(driver, PulseElements.class);
locateElements.selectNext2();
}
}
我的第二堂课:
package pulse.pom.tpr;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.Test;
public class ClockinToClockout {
public WebDriver driver;
public PulseLogin login=PageFactory.initElements(driver,PulseLogin.class);
@Test(priority=1)
public void launchBrowser() throws InterruptedException {
System.setProperty("webdriver.chrome.driver",
"C:\\MyChromeDriver\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
login.pulseLoginPage();
login.selectAssociate();
login.selectNextButton1();
login.selectArea();
login.selectNextButton2();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test(priority=2)
public void makeMove() {
PulseElements ele=PageFactory.initElements(driver, PulseElements.class);
ele.selectClockin();
}
}
其他一切都正常,但是我第二个测试类中的makeMove()函数给出了空指针异常:
PASSED: launchBrowser
FAILED: makeMove
java.lang.NullPointerException
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.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:51)
at com.sun.proxy.$Proxy7.getCoordinates(Unknown Source)
at org.openqa.selenium.interactions.internal.MouseAction.getActionLocation(MouseAction.java:65)
at org.openqa.selenium.interactions.MoveMouseAction.perform(MoveMouseAction.java:43)
at org.openqa.selenium.interactions.CompositeAction.perform(CompositeAction.java:36)
at org.openqa.selenium.interactions.Actions$BuiltAction.perform(Actions.java:641)
at pulse.pom.tpr.PulseElements.selectClockin(PulseElements.java:100)
at pulse.pom.tpr.ClockinToClockout.makeMove(ClockinToClockout.java:37)
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:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
答案 0 :(得分:1)
那是因为您需要在类级别而不是像在PulseElements
类中那样在@Test
级别初始化PulseLogin
。
因此,像这样在类级别初始化PulseElements
:
public class ClockinToClockout {
public WebDriver driver;
public PulseLogin login=PageFactory.initElements(driver,PulseLogin.class);
public PulseElements ele=PageFactory.initElements(driver, PulseElements.class);
@Test(priority=1)
public void launchBrowser() throws InterruptedException {
System.setProperty("webdriver.chrome.driver",
"C:\\MyChromeDriver\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
login.pulseLoginPage();
login.selectAssociate();
login.selectNextButton1();
login.selectArea();
login.selectNextButton2();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test(priority=2)
public void makeMove() {
ele.selectClockin();
}
}