Running Appium with Page Object

Hello, I would appreciate if someone could help me with this test. It loads up the app but fail to perform the tasks in @Test. It appears it’s not initialising the testLogin() . Thank you

package EverSmart_POF;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.BeforeSuite;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.pagefactory.AndroidFindBy;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.remote.MobilePlatform;

public class LogInJourney {

	 protected AndroidDriver driver;		 

	  public LogInJourney(AppiumDriver driver) {
		//This is a constructor, as every page need a base driver to find web elements
	    	this.driver = (AndroidDriver) driver;
	    	PageFactory.initElements(new AppiumFieldDecorator(driver, 5, TimeUnit.SECONDS), this);
	    	}
	  
        //@AndroidFindBy(xpath = "//android.widget.Button[@text = 'Log in' and @clickable='true']")
        //public WebElement SignInButton;

	    @AndroidFindBy(id = "")
        public WebElement SignInButton;
       
        @AndroidFindBy(id = "")
        public WebElement Username;
       
        @AndroidFindBy(id = "")
        public WebElement Password;

        @AndroidFindBy(id = "")
        public WebElement CheckBox;
        	       
        @AndroidFindBy(id = "")
        public WebElement ClickSignIn;
        
        
	          
    	public WebElement SignInButton ()  
    	{ 
    	    return SignInButton;
    	}
    	
    	public WebElement Username () 
    	{
    		return Username;
    	}
    	
    	public WebElement Password () 
    	{
    		return Password;
    	}
    	
    	public WebElement CheckBox () 
    	{
    		return CheckBox;
    	}
    	
    	public WebElement ClickSignIn () {
    		return ClickSignIn;
    	
   		}
	 
	}

package EverSmart_POF;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.remote.MobilePlatform;
import EverSmart_POF.LogInJourney;

// Specify the file location I used .operation here because we have object repository inside project directory only
// File src=new File(".Object_Repo.properties");

// Create FileInputStream object
//FileInputStream fis=new FileInputStream(src);

// Create Properties class object to read properties file
//Properties pro=new Properties();

// Load file so we can use into our script
//pro.load(fis);

public class LoginTest {
 AppiumDriver driver;

 private LogInJourney loginPage;
		
@BeforeSuite
public static void setUp () throws MalformedURLException
 {
		     
        File appDir=new File("src");

		File app = new File(appDir,"base.apk");
        
        DesiredCapabilities cap=new DesiredCapabilities();
		cap.setCapability(MobileCapabilityType.PLATFORM_NAME,MobilePlatform.ANDROID);
		cap.setCapability(MobileCapabilityType.DEVICE_NAME,"Android Emulator");

		cap.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 100);
		cap.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
		AndroidDriver driver = new AndroidDriver (new URL ("http://127.0.0.1:4723/wd/hub"),cap);
		driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
	}


@Test
public void testLogin() throws InterruptedException
{
			
	loginPage = new LogInJourney(driver);
	Thread.sleep(13000L);
    loginPage.SignInButton().click();
    loginPage.Username().sendKeys("");
    loginPage.Password().sendKeys("");
    loginPage.CheckBox().click();
    loginPage.ClickSignIn().click();
	System.out.println("Login Successful");
   

}

}

AndroidDriver driver = new AndroidDriver (new URL (“http://127.0.0.1:4723/wd/hub”),cap);

change to

driver = new AndroidDriver (new URL (“http://127.0.0.1:4723/wd/hub”),cap);

The issue is not the driver. The configuration is fine, it installs the app and launches the landing screen.

The issue is that: @Test is not running.

Below is the ERROR LOG.

FAILED: testLogin
java.lang.NullPointerException
at io.appium.java_client.pagefactory.AppiumElementLocator.(AppiumElementLocator.java:72)
at io.appium.java_client.pagefactory.AppiumElementLocatorFactory.createLocator(AppiumElementLocatorFactory.java:26)
at io.appium.java_client.pagefactory.AppiumFieldDecorator.decorate(AppiumFieldDecorator.java:65)
at org.openqa.selenium.support.PageFactory.proxyFields(PageFactory.java:112)
at org.openqa.selenium.support.PageFactory.initElements(PageFactory.java:104)
at EverSmart_POF.LogInJourney.(LogInJourney.java:35)
at EverSmart_POF.LoginTest.testLogin(LoginTest.java:77)
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:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)

===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0

===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0

[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@506e6d5e: 12 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@17c68925: 39 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 22 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@6a5fc7f7: 173 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@7a79be86: 138 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@17f6480: 17 ms

loginPage = new LogInJourney(driver); here driver is referring to global variable AppiumDriver driver;

in your setup AndroidDriver driver = new AndroidDriver (new URL (“http://127.0.0.1:4723/wd/hub”),cap); driver is local

so change it to driver = new AndroidDriver (new URL (“http://127.0.0.1:4723/wd/hub”),cap); so that it will assign to global variable AppiumDriver driver;

Hi harigovind,
I have just made the edit and the driver is throwing error.

Can you share error log… might be problem with type cast

Here is the Error below …

FAILED: testLogin
java.lang.NullPointerException
at io.appium.java_client.pagefactory.AppiumFieldDecorator.getTypeForProxy(AppiumFieldDecorator.java:233)
at io.appium.java_client.pagefactory.AppiumFieldDecorator.proxyForAnElement(AppiumFieldDecorator.java:248)
at io.appium.java_client.pagefactory.AppiumFieldDecorator.access$0(AppiumFieldDecorator.java:246)
at io.appium.java_client.pagefactory.AppiumFieldDecorator$3.proxyForLocator(AppiumFieldDecorator.java:121)
at org.openqa.selenium.support.pagefactory.DefaultFieldDecorator.decorate(DefaultFieldDecorator.java:61)
at io.appium.java_client.pagefactory.AppiumFieldDecorator.decorate(AppiumFieldDecorator.java:172)
at org.openqa.selenium.support.PageFactory.proxyFields(PageFactory.java:112)
at org.openqa.selenium.support.PageFactory.initElements(PageFactory.java:104)
at EverSmart_POF.LogInJourney.(LogInJourney.java:52)
at EverSmart_POF.LoginTest.testLogin(LoginTest.java:79)
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:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)

===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0

===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0

[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@506e6d5e: 16 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@17c68925: 21 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 15 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@6a5fc7f7: 176 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@7a79be86: 47 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@17f6480: 10 ms

@Tutsy
at EverSmart_POF.LogInJourney.(LogInJourney.java:52) ->

    	public WebElement SignInButton ()  
    	{ 
    	    return SignInButton;
    	}

means that SignInButton NOT found. thus SignInButton.click() actually means null.click() in this case which cause NullPointerException.