To run your first Appium test, open Eclipse (or any other Java IDE of your choice) and create a new Java Project by following the steps in the picture below. Here, the project name is given as SampleAppiumProject.

Once your project is created successfully, create a package (Right-click on project -> New -> Package). Here, the package name is given as core. You can give any other meaningful name. Now create a class (here, class name is given as AppiumBase) with a suitable class name of your choice starting with uppercase. To create a class, right click on package -> New -> Class -> Give class name -> Finish. Next, we have to set the dependencies in the Project Build Path. Right click on the project -> Build Path -> Configure Build Path -> Libraries tab -> Click on “Add External JARs…” -> Import the Appium java client library jar, the Selenium Java Client library jars and Apache commons-lang3 jar file that you downloaded earlier. Your SampleAppiumProject structure now should look like below:

apk Files

apk (Android Package Kit), sometimes also referred to as Android Application Package, is a file format which is used by the Android Operating System for installation and distribution of mobile apps. Just like exe file on Windows, you can place a .apk file on your Android device to install an app. When you download an app from Google play store, it automatically downloads and installs the apk file for you. Here, in this tutorial we will be using the ApiDemos-debug.apk file. You can download/clone it from the Appium Java-Client Github page. Create a folder (you can name it app or any name of your choice) in your SampleAppiumProject by doing Right-click -> New -> Folder -> Give Folder name -> Finish. Place the ApiDemos-debug.apk file that you cloned under this folder.

DesiredCapabilities

Let’s create a static method (initializeAppiumCapabilities) inside the AppiumBase class whose purpose will be to initialize and set the Appium capabilities to run our Appium tests. The method returns AndroidDriver<AndroidElement>. Inside the method, write the following code:

public static AndroidDriver<AndroidElement> initializeAppiumCapabilities() {
		AndroidDriver<AndroidElement> androidDriver = null;
		try {
			// Set the desired capabilities
			File androidApkfile = new File("./app/ApiDemos-debug.apk");
			DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
			desiredCapabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Nexus_5X_Edited_API_28");
			desiredCapabilities.setCapability(MobileCapabilityType.APP, androidApkfile.getAbsolutePath());
			desiredCapabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "uiautomator2");

First we are initializing the AndroidDriver<AndroidElement> type variable androidDriver to null. Then we are creating a File object and passing the location of the apk file in its constructor. DesiredCapabiltiies are key-value pairs which are sent to the Appium server to inform it about the session, mobile os platform to be used (iOS/ Android/ Windows), device name (the kind of mobile driver or emulator to be used, here emulator name is “Nexus_5X_Edited_API_28“), automation name (which automation engine to use e.g. uiautomator2 for Android, XCUITest for iOS) and the absolute local path or remote http URL to a .apk file (Android), .apks file (Android App Bundle), .ipa file (iOS), .app folder (iOS Simulator) or a .zip file containing one of these. Appium supports many capabilities. We will be exploring some more in our upcoming tutorials.

// Declare the Appium Server url and create AndroidElement object
			URL appiumServerUrl = new URL("http://127.0.0.1:4723/wd/hub");
			androidDriver = new AndroidDriver<AndroidElement>(appiumServerUrl, desiredCapabilities);
			androidDriver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return androidDriver;
	}

Next we create an URL object and define the http url in which our Appium server will be running. Typically the server will be running at port 4723 of localhost. Now, create an object of the AndroidDriver class and pass the appiumServerUrl object and desiredCapabilities object as its Constructor parameters. You can define webdriver implicit wait too. We have set it to 15 seconds. The AppiumBase class will now look like:

package core;

import java.io.File;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.remote.MobileCapabilityType;

public class AppiumBase {

	public static AndroidDriver<AndroidElement> initializeAppiumCapabilities() {
		AndroidDriver<AndroidElement> androidDriver = null;
		try {
			// Set the desired capabilities
			File androidApkfile = new File("./app/ApiDemos-debug.apk");
			DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
			desiredCapabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Nexus_5X_Edited_API_28");
			desiredCapabilities.setCapability(MobileCapabilityType.APP, androidApkfile.getAbsolutePath());
			desiredCapabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "uiautomator2");

			// Declare the Appium Server url and create AndroidElement object
			URL appiumServerUrl = new URL("http://127.0.0.1:4723/wd/hub");
			androidDriver = new AndroidDriver<AndroidElement>(appiumServerUrl, desiredCapabilities);
			androidDriver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return androidDriver;
	}
}

Let us now create a test class which will contain our first Appium Test. Like before, create a new class inside a new package (test) and name it “AppiumBasicTest”. Ensure that the class is having the main method inside it and the class is extending the “AppiumBase” class (Inheritance). Now write the below code:

package test;

import core.AppiumBase;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;

public class AppiumBasicTest extends AppiumBase {

	public static void main(String[] args) {
		AndroidDriver<AndroidElement> androidDriver = initializeAppiumCapabilities();
		AndroidElement preferenceText = androidDriver.findElementByXPath("//*[@text='Preference']");
		preferenceText.click();
		AndroidElement preferenceDependenciesText = androidDriver
				.findElementByXPath("//android.widget.TextView[@text='3. Preference dependencies']");
		preferenceDependenciesText.click();
		AndroidElement wifiCheckbox = androidDriver.findElementById("android:id/checkbox");
		wifiCheckbox.click();
	}
}

To run this test, perform the following steps one by one:

1) Start the Appium server by running the command appium in command prompt. The Appium server should be kept running throughout your Appium test execution. Once it starts running, it will look like image below.

2) To start the emulator (virtual device), open another command prompt and go inside the emulator folder present inside the Android SDK folder and enter the command emulator -avd deviceName (for me the command looks like emulator -avd Nexus_5X_Edited_API_28). Only one virtual device should be running at a time and to verify that, use the command adb devices which should display only one device.

Commands in Windows Command Prompt

Keep your emulator open on the screen and run the code as “Java Application”. You should be able to view the logs getting generated in the Appium Server as the test starts to run. Initially, the test might take some time to run. But if every setup is done correctly, then the ApiDemos-debug app will get installed in your virtual device and the tests will run successfully on that app. Images below.

FIRST APPIUM TEST RUN

CONGRATULATIONS!! You have successfully run your first test using Appium.

NOTE:
You can also invoke an app from the mobile device itself without downloading the apk file. To do that you need to know the package name and the activity name of the app and then use the below code.

DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
		desiredCapabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, MobilePlatform.ANDROID);
		desiredCapabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Nexus_5X_Edited_API_28");
		desiredCapabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "uiautomator2");
		desiredCapabilities.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, "<insert app package name here>");
		desiredCapabilities.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, "<insert app activity name here>");

Credits:
http://appium.io/