Quick Start Guide: Building Your First Appium 2.0 Automation Framework & Script (2024
Kushal Parikh
QA Automation Engineer | SDET | Test Automation Consultant | Selenium | Java | Playwright | SFPC? Certified | Driving Quality Through Automation
Step-by-Step Appium 2.0 Setup Guide
1. Install Node.js
?? - Download the latest Node.js from the [official site] - https://nodejs.org/en
?? - Install it locally on your system.
2. Add Node.js to Environment Variables
?? - Open Environment Variables settings.
?? - Add the Node.js environment variable as NODE_HOME & add path of your Node.js folder in path variable .
3. Install Java (Recommended Version 14 or Above)
?? - Download and install Java JDK 14+ from [Oracle] - https://www.oracle.com/java/technologies/javase-downloads.html
4. Add Java to Environment Variables
?? Add the Java environment variable as JAVA_HOME & add path of your Java folder in path variable .
5. Install Android Studio
?? - Download and install Android Studio from the [official site] - https://developer.android.com/studio.
?? - This provides the Android SDK, essential for running Appium tests.
6. Set Android SDK Path in Environment Variables
?? - In Environment Variables, add paths for:
???? - ANDROID_HOME pointing to the Android SDK path.
???? - Update PATH with platform ,`platform-tools` and tools.
7. Install Appium
?? - Open the command prompt.
?? - Install Appium globally with: npm install -g appium.
8. Create a Maven Project for Appium Tests
?? - Create a Maven project in your IDE (like IntelliJ or Eclipse).
?? - Add necessary Appium dependencies to the pom.xml file:
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.25.0</version>
</dependency>
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>9.3.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.10.2</version>
<scope>test</scope>
</dependency>
</dependencies>
9. Write Your First Appium Script
?? - Create a Java class and copy-paste the sample Appium code:
import io.appium.java_client.AppiumBy;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.options.UiAutomator2Options;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
public class FirstAppiumTest {
public static UiAutomator2Options options;
public static AppiumDriver driver;
public static AppiumDriverLocalService service;
@BeforeTest
public void setup() {
//Setup by programmatically starting Appium server
//startAppiumServer(); - uncomment this line if you want to start Appium server automatically
// Set up Appium driver and initialize necessary elements
// Define the options or Desired Capabilities for the Appium driver
//You can below commended code as well as Desired Capabilities based on your requirements
/*options = new UiAutomator2Options();
options.setAutomationName("UiAutomator2"); // Automation Driver Name
options.setUdid("2ead659b"); // Replace with your device UDID displayed in adb devices command
options.setPlatformName("Android"); // Replace with your platform name
options.setPlatformVersion("12"); // Replace with your device OS version
options.setApp("/Users/kusha/AppiumTestingApps/testproject-demo-app.apk"); // Replace with your app package name*/
//Desired Capabilities
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("appium:automationName", "UiAutomator2");
caps.setCapability("appium:deviceName", "2ead659b");
caps.setCapability("appium:platformName", "Android");
caps.setCapability("appium:platformVersion", "12");
caps.setCapability("appium:app", "/Users/kusha/AppiumTestingApps/testproject-demo-app.apk");
// Initialize the Appium driver
try {
driver = new AndroidDriver(new URL("https://127.0.0.1:4723"), caps);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
@Test
public void testRegistration() {
// Add a wait or interact with elements if needed here
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(15)); // Replace with your desired wait time
WebElement fullName = wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.xpath("https://android.widget.EditText[@resource-id=\"io.testproject.demo:id/name\"]")));
//fullName.click();
fullName.sendKeys("Kushal Parikh");
WebElement password= driver.findElement(AppiumBy.xpath("https://android.widget.EditText[@resource-id=\"io.testproject.demo:id/password\"]"));
password.sendKeys("12345");
WebElement login = driver.findElement(AppiumBy.xpath("https://android.widget.Button[@resource-id=\"io.testproject.demo:id/login\"]"));
login.click();
WebElement country = wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.xpath("https://android.widget.EditText[@resource-id=\"io.testproject.demo:id/country\"]")));
country.sendKeys("India");
WebElement address = driver.findElement(AppiumBy.xpath("https://android.widget.EditText[@resource-id=\"io.testproject.demo:id/address\"]"));
address.sendKeys("Test Address");
WebElement emailAddress = driver.findElement(AppiumBy.xpath("https://android.widget.EditText[@resource-id=\"io.testproject.demo:id/email\"]"));
emailAddress.sendKeys("[email protected]");
WebElement phoneNumber = driver.findElement(AppiumBy.xpath("https://android.widget.EditText[@resource-id=\"io.testproject.demo:id/phone\"]"));
phoneNumber.sendKeys("[email protected]");
WebElement save = driver.findElement(AppiumBy.xpath("https://android.widget.Button[@resource-id=\"io.testproject.demo:id/save\"]"));
save.click();
WebElement profileSaveTest = driver.findElement(AppiumBy.xpath("https://android.widget.TextView[@resource-id=\"io.testproject.demo:id/saved\"]"));
String profileMsg = profileSaveTest.getText();
if (profileMsg!=null && !profileMsg.isEmpty())
{
System.out.println("Profile saved successfully: " + profileMsg);
System.out.println("Appium test passed!");
WebElement logout = driver.findElement(AppiumBy.xpath("https://android.widget.Button[@resource-id=\"io.testproject.demo:id/logout\"]"));
logout.click();
}
}
@AfterTest
public void tearDown() {
// Close the Appium driver
if (driver!= null) {
driver.quit();
}
// Stop Appium server method - Programmatically stop Appium server
//stopAppiumServer(); - uncomment this line if you want to stop Appium server automatically
}
//Start and stop Appium server methods - Programmatically start and stop Appium server
public void startAppiumServer() {
service = new AppiumServiceBuilder()
.withAppiumJS(new File("/Users/kusha/AppData/Roaming/npm/node_modules/appium/build/lib/main.js")) // Specify the path to Appium
.withIPAddress("127.0.0.1")
.usingPort(4723)
.build();
service.start();
System.out.println("Appium Server Started...");
}
// Stop Appium server method - Programmatically stop Appium server
public void stopAppiumServer() {
if (service != null) {
service.stop();
System.out.println("Appium Server Stopped.");
}
}
}
10. Start Appium Server
?? - Start Appium server either:
???? 1) Using Command Prompt: Run appium command.
???? 2) ?Programmatically in your code.
Note: if you’re using appium inspector web -use : “appium --allow-cors”
11. Connect Your Device
?? - Connect your Android device via USB – turn on debugger.
?? - Check for device name using adb devices command.
12. Set Up Appium Inspector - https://inspector.appiumpro.com/? - web use or you can download
?? - Open Appium Inspector.
?? - Add desired capabilities:
???? - platformName, deviceName, app, automationName, udid, etc., based on your device and application.
{
"appium:automationName": "UiAutomator2",
"appium:platformName": "Android",
"appium:platformVersion": "12",
"appium:deviceName": "2ead659b",
"appium:app": "/Users/kusha/AppiumTestingApps/testproject-demo-app.apk"
}
13. Configure Desired Capabilities in test script
?? - Adjust your script capabilities as per your device and APK:
???? - setAutomationName, setUdid, setPlatformName, setPlatformVersion, setApp
DesiredCapabilities caps = new DesiredCapabilities(); caps.setCapability("appium:automationName", "UiAutomator2"); caps.setCapability("appium:deviceName", "2ead659b"); caps.setCapability("appium:platformName", "Android"); caps.setCapability("appium:platformVersion", "12"); caps.setCapability("appium:app", "/Users/kusha/AppiumTestingApps/testproject-demo-app.apk");
14. Run Your Script
?? - With all paths and configurations set, execute your Java code to initiate the Appium test on your Android device.
This setup will give you a working environment to execute Appium tests on Android.
My source code Git repo - https://github.com/kush1107/AppiumAutomationFramework.git
?
For video tutorials - https://youtube.com/playlist?list=PLhW3qG5bs-L8BQaqLpjt5792e8om6IR3k&si=cReD4eynkMAMaw67
Demo Apks for Practices
Here are some sample APK files that we can use for learning test automation?
Can get some sample APK files from appium
This contains both Android (.apk files) and iOS (.app files)
__________________________________________________________
Can search for APK files for apps like Calculator
__________________________________________________________
Bitbar sample APK
__________________________________________________________
Browserstack
__________________________________________________________
SauceLabs demo Apps https://github.com/saucelabs/sample-app-mobile/releases
Includes both android and ios apps
__________________________________________________________
TestProject demo app https://github.com/testproject-io/android-demo-app?
__________________________________________________________
Katalon Studio Sample apk files
Happy Testing !