我正在尝试使用POM创建一个测试框架,但是每当我使用PageFactory
时,它都会给出一个NullPointerException
。
我只是试图打开应用程序,并检查给定元素在页面上是否存在。
这是我的驱动程序类:
public class Driver extends BaseSetup
{
protected AndroidDriver<MobileElement> driver;
public Driver() {
this.driver = super.getDriver();
}
}
这是我的BaseSetup类:
public class BaseSetup {
private DesiredCapabilities capabilities;// = new DesiredCapabilities();
protected AndroidDriver<MobileElement> androidDriver = null;
private String appiumPort="4723";
private String serverIp="0.0.0.0";
//String workingDevice = "emulator-5554";
String workingDevice = "d6f08719";
@BeforeClass
public void setup() {
initDriver();
}
public AndroidDriver<MobileElement> getDriver() {
return androidDriver;
}
private void initDriver() {
System.out.println("Inside initDriver method");
// DesiredCapabilities cap = new DesiredCapabilities();
capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "OnePlus6T");
capabilities.setCapability("udid", workingDevice);
capabilities.setCapability("appPackage", "com.ServiceApp.Vivek");
capabilities.setCapability("appActivity", ".MainActivity");
capabilities.setCapability("noReset", "true");
String serverUrl = "http://" + serverIp + ":" + appiumPort + "/wd/hub";
try
{
System.out.println("Argument to driver object : " + serverUrl);
androidDriver = new AndroidDriver<MobileElement>(new URL(serverUrl), capabilities);
}
catch (NullPointerException | MalformedURLException ex) {
throw new RuntimeException("appium driver could not be initialised for device ");
}
System.out.println("Driver in initdriver is : "+androidDriver);
}
@AfterClass
public void tearDown() {
androidDriver.quit();
}
@BeforeMethod
public void beforeMethod(Method method) {
System.out.println("Starting Method :" + method.getName());
System.out.println("-----------------------------------------");
}
@AfterMethod
public void afterMethod(Method method) {
System.out.println("Ending Method :" + method.getName());
System.out.println("-----------------------------------------");
}
}
这是我的OpenApplication类:
public class OpenApplication extends Driver{
public OpenApplication(AppiumDriver<MobileElement> driver) {
super();
PageFactory.initElements(new AppiumFieldDecorator(driver), OpenApplication.class);
}
public void validatePageLoaded() throws InterruptedException {
System.out.println("timer start");
Thread.sleep(5000);
System.out.println("timer end");
String str=newUser.getText();
System.out.println(str);
System.out.println("Successfully");
if (newUser.isDisplayed() && existingCustomer.isDisplayed()
&& welcomeText.isDisplayed()) {
System.out.println("App open Successfully..!");
} else {
System.out.println("App is not loaded..!");
}
}
@CacheLookup
@AndroidFindBy(xpath = "//android.view.View[@index='2']")
public WebElement newUser;
@CacheLookup
@AndroidFindBy(xpath = "//android.view.View[@index='4']")
public WebElement existingCustomer;
@CacheLookup
@AndroidFindBy(xpath = "//android.view.View[@text='Welcome To Bajaj Finserv']")
public WebElement welcomeText;
}
这是我的OpenApplicationTest类:
public class OpenApplicationTest extends Driver {
@Test(priority = 0)
public void openApplicationValidation() throws InterruptedException {
OpenApplication openApplication= new OpenApplication(driver);
openApplication.validatePageLoaded();
}
}
我收到以下错误消息:
FAILED: openApplicationValidation
java.lang.NullPointerException
at com.datamato.pages.OpenApplication.validatePageLoaded(OpenApplication.java:34)
at com.datamato.tests.OpenApplicationTest.openApplicationValidation(OpenApplicationTest.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:645)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:822)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1130)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:113)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:206)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:177)
我该怎么做才能纠正它?
最佳答案
以下是您的程序流程
OpenApplication类对象调用参数化的构造函数,即
OpenApplication(AppiumDriver<MobileElement> driver) { }
然后super();
调用Driver类的构造函数public Driver() {this.driver = super.getDriver();}
然后super.getDriver();
方法调用BaseSetup类getDriver
方法,该方法返回androidDriver
;
我几乎可以确定的是,从未调用@BeforeClass
批注方法,因此androidDriver
没有初始化。
// System.out.println("Driver in initdriver is : "+androidDriver); This must have not printed.
这可能不是一个好的解决方案,但是您可以尝试按照
initDriver
中的put getDriver
方法进行操作,以便可以在使用驱动程序之前对其进行初始化。解:
public AndroidDriver<MobileElement> getDriver() {
initDriver();
return androidDriver;
}