First Appium test script on Android device

Appium

Written by:

1/ Turn on USB debugging on your android device

Go to Setting –> Developer Options –> Enable USB debugging

 

 

2/ Find appPackage and appActivity name of your app.

In this tutorial I will use calculator app on my phone.

There’re 2 ways to check:

2-1/ Use ‘mCurrentFocus’ or ‘mFocusedApp’ in CMD

  • Unclock your mobile device and connect it to your computer by using USB cable.
  • Open Calculator app
  • Open command prompt. And type 

dumpsys window windows | grep -E ‘mCurrentFocus’

or dumpsys window windows | grep -E ‘mFocusApp’

 

 

2-2/ Use Apk Info app

  • Open Play Store and download.

  • Open Apk Info. It will show all app into your mobile.
  • Select Calculator app.

  • You can see appPackage name at top.
  • Expand “Activities”.

3/ Get your device name.

4/ Get device ID of your phone.

Open CMD and type adb devices

 

5/ Get your phone’s android version

Go to Settings –> About phone –> Software information

6/ Start Appium server

 

7/ Open Eclipse

We will make a simple test with steps:

  • Open Calculator.
  • Do calculation: 7+8.
  • Verify result and show report on by console.

Add code and run.

import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;


public class App 
{
    public static void main( String[] args ) throws InterruptedException, MalformedURLException
    {
    	DesiredCapabilities caps;
    	AppiumDriver<MobileElement> driver;
    	caps = new DesiredCapabilities();
		caps.setCapability("deviceName", "TestingPhone");
		caps.setCapability("udid", "4200620ea8f6941b"); //Give Device ID of your mobile phone
		caps.setCapability("platformName", "Android");
		caps.setCapability("platformVersion", "7.0");
		caps.setCapability("appPackage", "com.google.android.calculator");
		caps.setCapability("appActivity", "com.android.calculator2.Calculator");
		caps.setCapability("noReset", "true");
		driver = new AndroidDriver<MobileElement>(new URL("http://0.0.0.0:4723/wd/hub"), caps);
    			
    	driver.findElement(By.id("com.google.android.calculator:id/digit_7")).click();//press number 7
    	driver.findElement(By.id("com.google.android.calculator:id/op_add")).click();//press +
    	driver.findElement(By.id("com.google.android.calculator:id/digit_8")).click();//press number 8
    	driver.findElement(By.id("com.google.android.calculator:id/eq")).click();//press =
    	
    	Thread.sleep(5000); 
    	//Verify result
    	String resultText = driver.findElement(By.id("com.google.android.calculator:id/result_final")).getText(); 
    	if(resultText.equals("15")) { 
    		System.out.println("Test Passed"); 
    	}else {
    		System.out.println("Test Failed"); 
    	}

    }
}

Congratulations! finally you have completed to launch a mobile app with Appium code. Hope that this tutorial is helpful for you. If there’re any issues, please leave a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *