Write your first Appium Test for Android

krishna chetan
3 min readFeb 5, 2021

We learned how to set up Appium in the last Blog, in this one we will write our first test using Appium for Android and later for an iOS device in the upcoming blog.

Below is Appium Architecture

Required Softwares

Appium Setup

https://krishnachetan.medium.com/setup-appium-on-mac-1e06f1178427

IntelliJ Idea ide

Let's start by creating a new project, we will be using the IntelliJ idea IDE for this project

  1. Select New Project.

2. Enter project Details like Name, GroupId, ArtifactId, Version and click on finish.

3. Project will be created with following project strcuture

4. Add the following dependencies in pom.xml file and rebuild the project

<dependencies><!-- https://mvnrepository.com/artifact/io.appium/java-client -->
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>7.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.3.0</version>
<scope>compile</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.aventstack/extentreports -->
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports</artifactId>
<version>4.0.6</version>
</dependency>

</dependencies>

5. Create a New Java class under main -> java

6. Add your first Test Case

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import io.appium.java_client.remote.MobilePlatform;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
public class AppiumDemo
{
public static URL url;
public static DesiredCapabilities capabilities;
public static AndroidDriver<MobileElement> driver;

@BeforeSuite
public void setupAppium() throws MalformedURLException
{
File rootDirectory = new File(System.getProperty("user.dir"));
File appDir = new File(rootDirectory, "/app/");
File app = new File(appDir, "WIRED.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "9");
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, MobilePlatform.ANDROID);
capabilities.setCapability("deviceName", "ce0317138187a0860c");
capabilities.setCapability("autoAcceptAlerts",true);
capabilities.setCapability("noReset",false);
capabilities.setCapability("autoGrantPermissions",true);
//capabilities.setCapability("appPackage", "kc.example");
//capabilities.setCapability("appWaitActivity","kc.example");
capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
capabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 10000);
driver = new AndroidDriver (new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
}
@AfterSuite
public void uninstallApp() throws InterruptedException {
driver.removeApp("com.example.android.contactmanager");
}
@Test (enabled=true) public void myFirstTest() throws InterruptedException {
driver.findElementById("demo");
}
}

--

--

krishna chetan

Automation Test Engineer by Profession, Traveller by the Weekend