PageFactory给出了空指针异常

时间:2019-05-22 17:51:02

标签: java appium

我正在尝试使用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)

我该怎么做才能纠正它?

2 个答案:

答案 0 :(得分:0)

以下是程序的流程

  1. OpenApplication类对象调用参数化的构造函数,即   OpenApplication(AppiumDriver<MobileElement> driver) { }

  2. 然后super();调用Driver类的构造函数

    public Driver() {this.driver = super.getDriver();}

  3. 然后,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;
}

答案 1 :(得分:0)

@Amit Jain,感谢您的快速回复,因为您提到我的@BeforeClass方法未调用androidDriver,但其调用和打印操作也已声明。请在下面的控制台输出中找到以供参考。

[TestNG] Running:
  C:\Users\DL41\AppData\Local\Temp\testng-eclipse--572591171\testng-customsuite.xml

Inside initDriver method
Argument to driver object : http://0.0.0.0:4723/wd/hub
May 23, 2019 10:03:30 AM io.appium.java_client.remote.AppiumCommandExecutor$1 lambda$0
INFO: Detected dialect: W3C
Driver in initdriver is : io.appium.java_client.android.AndroidDriver, Capabilities: {appActivity=.MainActivity, appPackage=com.BajajServiceApp.VikramUAT, databaseEnabled=false, desired={platformName=android, appActivity=.MainActivity, appPackage=com.BajajServiceApp.VikramUAT, deviceName=OnePlus6T, noReset=true, udid=d6f08719}, deviceManufacturer=OnePlus, deviceModel=ONEPLUS A6010, deviceName=d6f08719, deviceScreenSize=1080x2340, deviceUDID=d6f08719, javascriptEnabled=true, locationContextEnabled=false, networkConnectionEnabled=true, noReset=true, platform=LINUX, platformName=Android, platformVersion=9, takesScreenshot=true, udid=d6f08719, warnings={}, webStorageEnabled=false}
Starting Method :openApplicationValidation
-----------------------------------------
timer start
timer end
Ending Method :openApplicationValidation
-----------------------------------------
FAILED: openApplicationValidation
java.lang.NullPointerException
    at com.datamato.pages.OpenApplication.validatePageLoaded(OpenApplication.java:34)
    at com.datamato.tests.OpenApplicationTest.openApplicationValidation(OpenApplicationTest.java:14)
    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)