第二个测试方法有 nullpointerexception 错误

Second Test Method is having nullpointerexception error

我正在使用 appium 服务器对 Ios 应用程序进行自动化测试。 我已经实施了两个 classes。第一个是让 UI 元素查找属性、操作和构造函数。还有一个是有测试方法和驱动配置。

这是我的第一个 class 即 LoginPage.java

public class LoginPage {

String orgIdentifer;
String personIdentifer;
String userPasswrod;
IOSDriver<IOSElement> driver;

public LoginPage() {
   }
   public LoginPage(IOSDriver<IOSElement> driver) {
       this.driver = driver;
      // PageFactory.initElements(new AppiumFieldDecorator(driver), this);
       PageFactory.initElements(new AppiumFieldDecorator(driver), this);
   }    

public boolean validateLoginpage(){
    boolean elements = false;
    if(organizationIdentifier.isDisplayed()){
        if(personIdentifier.isDisplayed()){
            if(password.isDisplayed()){
                if(loginButton.isDisplayed()){
                    elements = true;
                }
            }
        }
    }
    else{
        elements = false;
    }
    return elements;


}
int sum()
{
    int a=5;
    int b=6;
    return a+b;
}

public void  firstThreePopoClick()
{

    firstPopUp.click();
    secondPopUp.click();
    thirdPopUp.click();
    organizationIdentifier.sendKeys("testauto");
    personIdentifier.sendKeys("manager");
    password.sendKeys("123456");

    //OKPopupBtn.click();
}
public boolean TestdoLoginWIthValues()
{
    boolean element=false;

    loginButton.click();
    if(logoutDoneButtonWheel.isDisplayed())
    {
        element=true;
    }

    return element;
}

public boolean loginTestCase()
{
    logoutDoneButtonWheel.click();
    boolean check=false;
    if(acceptPopupBtn.isDisplayed())
    {
        check=true;

    }
    else
    {
        check=false;


    }
    return check;
}






    @FindBy(name = "Allow")
    public MobileElement firstPopUp;
    //@CacheLookup
   // @FindBy(name = "Allow")



    @FindBy(name = "OK")
    public MobileElement secondPopUp;


    @FindBy(name = "Ok")
    public MobileElement thirdPopUp;


    @FindBy(xpath = "/XCUIElementTypeApplication[@name=\"Human Focus Dev\"]/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther[2]/XCUIElementTypeTextField[1]")
    public MobileElement organizationIdentifier;

    @FindBy(xpath = "//XCUIElementTypeApplication[@name=\"Human Focus Dev\"]/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther[2]/XCUIElementTypeTextField[2]")
    public MobileElement personIdentifier;


    @FindBy(xpath = "//XCUIElementTypeApplication[@name=\"Human Focus Dev\"]/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther[2]/XCUIElementTypeSecureTextField")
    public MobileElement password;


    @FindBy(id =  "Login")
    public MobileElement loginButton;


    @FindBy(xpath =  "//XCUIElementTypeButton[@name=\"Done\"]")
    public MobileElement logoutDoneButtonWheel;



    @FindBy(name =  "Accept")
    public MobileElement acceptPopupBtn;


    @CacheLookup
    @FindBy(name =  "Ok")
    public MobileElement OKPopupBtn;

}

这是我的第二个 class 即 TestClass.java

class TesClass {
 String appiumPort ="4723";
 String serverIp ="127.0.0.1";
 static IOSDriver<IOSElement> driver;
 LoginPage lPage=null;
 DesiredCapabilities cap;


@Order(1)
@Test
void test() throws MalformedURLException  {
    cap= new DesiredCapabilities();


      cap.setCapability("deviceName", "Muhammad’s iPhone");
      cap.setCapability("platformName", "iOS");
      cap.setCapability("platformVersion","12.1.2");
      cap.setCapability("automationName", "XCUITest");
      cap.setCapability("app", "/Users/ahmsam/Downloads/MainApp-2.ipa");
      cap.setCapability("xcodeOrgId","BNL4VQ2576");
      cap.setCapability("xcodeSigningId","iPhone Developer");
      cap.setCapability("udid","240476512a6dd29a2f82fc8211ef4ea1bf6b5891");
      cap.setCapability("updateWDABundleId","5SN9XXLNWB.uk.org.humanfocus.WildCard.Dev");

      String serverUrl = "http://" + serverIp + ":" + appiumPort + "/wd/hub";
      driver = new IOSDriver<IOSElement>(new URL(serverUrl), cap);
      driver.manage().timeouts().implicitlyWait(55,TimeUnit.SECONDS);

      lPage=new LoginPage(driver);
      lPage.firstThreePopoClick();
      boolean check= lPage.TestdoLoginWIthValues();

     // = lPage.loginTestCase();

     Assert.assertTrue(check);


     //fail("Not yet implemented");
}

 @Order(2)
 @Test
    void test1() 
    {
        System.out.println("came in test1 method");
        boolean check1=lPage.TestdoLoginWIthValues();
        System.out.println("Now here");
        Assert.assertTrue(check1==true);

    }

}

我的第一个测试用例成功运行,但第二个测试方法出现以下错误。

我正在使用 Eclipse IDE:版本:2019-06 (4.12.0) Appium:版本 1.13.0 (1.13.0.20190505.5) Junit 5

java.lang.NullPointerException
at TesClass.test1(TesClass.java:65)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:628)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:117)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod(TestMethodTestDescriptor.java:184)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:180)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:127)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:68)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:135)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:229)
at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute(DefaultLauncher.java:197)
at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:211)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:191)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:137)
at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

测试彼此分开执行。 在第二个测试中,您忘记在这一行初始化 lPage 对象:

boolean check1=lPage.TestdoLoginWIthValues();

在此之前,您应该使用 LoginPage class 的构造函数。