eclipsejunitappiumios-ui-automationxcuitest

Second Test Method is having nullpointerexception error


I am doing automation testing of an Ios app using appium server. I have implemented two classes. First one is having UI elements finding attributes, actions and a constructor. And the other one is having test methods and driver configurations.

This is my first class that is 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;

}

And this is my second class that is 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);

    }

}

My first test case runs successfully but second test methods gives the following error.

i am using Eclipse IDE : Version: 2019-06 (4.12.0) Appium : Version 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$7(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$5(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$7(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$8(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$5(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$7(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$8(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$5(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$7(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$8(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$6(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)

Solution

  • Tests are executed separately one from each other. In the second test, you forgot to initialize the lPage object on this line:

    boolean check1=lPage.TestdoLoginWIthValues();
    

    Before to do that, you should use the constructor for LoginPage class.