Error running TestNG test

Hi, I have spent more than a day over this error so any and every help will be greatly appreciated. Here is my code:

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

import org.openqa.selenium.By;
//import org.openqa.selenium.WebDriver;
//import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Optional;
import org.testng.annotations.Test;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
//import io.appium.java_client.TouchAction;

public class forAndDemo {

private static AndroidDriver<MobileElement> driver;

@BeforeTest
	//public static void main (String [] args) throws MalformedURLException, InterruptedException
public void firstatest() throws MalformedURLException, InterruptedException
	{
	 

	DesiredCapabilities capabilities = new DesiredCapabilities();
	capabilities.setCapability("automationName", "Appium"); 
	capabilities.setCapability("appium-version", "1.4.0"); 
	capabilities.setCapability("platformName", "Android"); 
	capabilities.setCapability("platformVersion", "4.4.4"); 
	capabilities.setCapability("deviceName", "SGH-M919");
	capabilities.setCapability("app", "/Users/sbhatt/Desktop/android-master/app/build/outputs/apk/app-debug.apk"); 
	capabilities.setCapability("appPackage", "com.tigertext.beta"); 
	capabilities.setCapability("appActivity", "com.tigertext.onboarding.activities.OnboardingWelcomeActivity");
	//capabilities.setCapability("udid", "594c69db1eec686cd22c93071a4d05ec0addea39"); 
	URL serveraddress = new URL("http://127.0.0.1:4723/wd/hub");  
	AppiumDriver<MobileElement> driver = new AndroidDriver<MobileElement>(serveraddress, capabilities);
	driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
	//return capabilities;
	
	
	
	//TouchAction action = new TouchAction(driver);
	//action.press(289, 80).waitAction(2000).moveTo(55, 80).release().perform();
	}

	@Test
	public void LoginFlow()
	{
	driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]")).click();
	driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]/android.widget.EditText[1]")).sendKeys("[email protected]");
	driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.Button[1]")).click();
	driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]/android.widget.EditText[1]")).sendKeys("Testpass1");
	driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.Button[1]")).click();
	driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.ImageView[1]")).click();
	}

}

Here is the error I keep getting if I run it:
FAILED: LoginFlow
java.lang.NullPointerException
at forAndDemo.LoginFlow(forAndDemo.java:56)
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:497)
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.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

PLEASE, any help would be greatly appreciated.

I tried this code too and then I get a different error

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

import org.openqa.selenium.By;
//import org.openqa.selenium.WebDriver;
//import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Optional;
import org.testng.annotations.Test;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
//import io.appium.java_client.TouchAction;

public class forAndDemo {

//private static AndroidDriver<MobileElement> driver;

@BeforeTest
	//public static void main (String [] args) throws MalformedURLException, InterruptedException
public void pehlatest() throws MalformedURLException, InterruptedException
	{
	 

	DesiredCapabilities capabilities = new DesiredCapabilities();
	capabilities.setCapability("automationName", "Appium"); 
	capabilities.setCapability("appium-version", "1.4.0"); 
	capabilities.setCapability("platformName", "Android"); 
	capabilities.setCapability("platformVersion", "4.4.4"); 
	//capabilities.setCapability("platformVersion", "5.1");
	capabilities.setCapability("deviceName", "SGH-M919");
	//capabilities.setCapability("deviceName", "Nexus_5_API_22_x86");
	capabilities.setCapability("app", "/Users/sbhatt/Desktop/android-master/app/build/outputs/apk/app-debug.apk"); 
	capabilities.setCapability("appPackage", "com.tigertext.beta"); 
	capabilities.setCapability("appActivity", "com.tigertext.onboarding.activities.OnboardingWelcomeActivity");
	//capabilities.setCapability("udid", "594c69db1eec686cd22c93071a4d05ec0addea39"); 
	URL serveraddress = new URL("http://127.0.0.1:4723/wd/hub");  
	AppiumDriver<MobileElement> driver = new AndroidDriver<MobileElement>(serveraddress, capabilities);
	driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
	//return capabilities;
	
	
	
	//TouchAction action = new TouchAction(driver);
	//action.press(289, 80).waitAction(2000).moveTo(55, 80).release().perform();
	}

	@Test
	public void LoginFlow(@Optional AndroidDriver<MobileElement> driver)
	{
	driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]")).click();
	driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]/android.widget.EditText[1]")).sendKeys("[email protected]");
	driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.Button[1]")).click();
	driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]/android.widget.EditText[1]")).sendKeys("Testpass1");
	driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.Button[1]")).click();
	driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.ImageView[1]")).click();
	}

}

Here is the error I get for this:
SKIPPED: LoginFlow
org.testng.TestNGException:
Method LoginFlow requires 1 parameters but 0 were supplied in the @Test annotation.
at org.testng.internal.Parameters.checkParameterTypes(Parameters.java:198)
at org.testng.internal.Parameters.createParameters(Parameters.java:134)
at org.testng.internal.Parameters.createParameters(Parameters.java:373)
at org.testng.internal.Parameters.handleParameters(Parameters.java:450)
at org.testng.internal.Invoker.handleParameters(Invoker.java:1383)
at org.testng.internal.Invoker.createParameters(Invoker.java:1075)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1180)
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.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

@wreed since you answered me in the past a couple of times, any suggestions please?

Also if it helps…I tried running the test by not using any TestNG annotation…and just used a public static void main (string[] args). It ran just fine that time and executed the tests on the device.

also I am NOT using any xml files to run the tests.

So far it seems that it MAY have something to do with the setup of my jar files or env variables ? (or something along the lines). I’m on a MAC by the way.

My TestNG is pretty rusty as we don’t use it at my current job. I’m not sure why @Test doesn’t get the parameters for LoginFlow. Maybe try a TestNG forum?

https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=testng%20forum

You are creating a new instance of Appium Driver here.
Just assign Class Level driver object here.

Ex : driver = new AndroidDriver<MobileElement>(serveraddress, capabilities);

driver object here is from the

Unless you assign it while creating driver as mentioned above, it will be null. Hence your tests will throw NPE.

I am also not sure why you want your class “driver” variable, static.

Check with below code-

public class forAndDemo {

private AndroidDriver driver;

@BeforeTest
public void pehlatest() throws MalformedURLException, InterruptedException
{

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("automationName", "Appium"); 
capabilities.setCapability("appium-version", "1.4.0"); 
capabilities.setCapability("platformName", "Android"); 
capabilities.setCapability("platformVersion", "4.4.4"); 
//capabilities.setCapability("platformVersion", "5.1");
capabilities.setCapability("deviceName", "SGH-M919");
//capabilities.setCapability("deviceName", "Nexus_5_API_22_x86");
capabilities.setCapability("app", "/Users/sbhatt/Desktop/android-master/app/build/outputs/apk/app-debug.apk"); 
capabilities.setCapability("appPackage", "com.tigertext.beta"); 
capabilities.setCapability("appActivity", "com.tigertext.onboarding.activities.OnboardingWelcomeActivity");
URL serveraddress = new URL("http://127.0.0.1:4723/wd/hub");  
driver = new AndroidDriver<MobileElement>(serveraddress, capabilities);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
}

@RamS thanks Ramdas, I was able to make it run. However, when I type ‘private AndroidDriver driver;’ I get a warning next to it that says "AndroidDriver is a raw type. References to generic type AndroidDrivershould be parameterized.

Could you also please tell me why I need here. I never figured why we need that mobileElement anywhere.
driver = new AndroidDriver(serveraddress, capabilities);

Also, do I need any parameters inside my @Test method, or does it need to say just ‘public void LoginFlow()’

This is the change introduced in Java client version 3.0 and above.
Details @ Java-client 3.0.0 released

No need of any parameters, unless you are passing test data to your test case using the Dataprovider.

@RamS thanks Ramdas. So we always need MobileElement parameter listed after AndroidDriver ?

Please see if you can answer this:
I already pasted a copy of my desired capabilities for you previously. The issue I have is right now: the app is on my desktop but in future I will have to run from Jenkins box which will pick up the tests from GitHub. Can you paste pice of code that I will need to have to accomplish that? This would mean that the app path will not be my desktop but something else.

@Styris
In my project, we build application using a Jenkins job. Also ,we build and run tests using different Jenkins job.

But we do have a internal framework which takes all capability values in the JSON format and builds respective driver for our test.

Similarly u can have a properties file to list all the desired capabilities and in your test class pull these values and build driver.

If u want to dynamically change any capability, you may want to have a script which overrides these values in properties file, before you trigger test compile and run.

@RamS I think I may have not put my question correctly…sorry about that. What I meant to say is that usually I Build the application using Xcode or Android Studio. I have it where the Build file is generated in a Build folder on my Desktop…hence in my capabilities the app path is directed to my desktop location.

How should I make that path or put that in the capabilities so that if a Jenkins box is running the Appium test it will pick up the path? (as I cannot keep it as my local desktop path). Also we check in stuff on GitHub.

You can not point to a desktop path from Jenkins Job.
But, you can refer to a server URL [ex: Another Jenkins job which has the application apk] in the capabilities.
Either you push the APK to a shared location which can be used by an URL or create a separate job to build your application apk and refer to that location.

I have never seen a test annotation decorating the main point :smile:

@RamS so can you give me a one-line code exameple please (from the code I pasted above?)

I will be checking in the code into our repo on GitHub. Of course that will contain the src folder in there as well. Do you mean to say that i need to put the app in that folder ? In that case what will my code look like ?

Thanks

@RamS did you get my last comment by any chance ?

@brentlavelle @Mario please ? this may be related to the other Post.

I’ve never used testng and I’ve moved on to Calabash for mobile testing now.