Appium - Run First Mobile App Test Case for Android - With Emulator

What do we need?

  • Eclipse IDE Or any IDE of your choice (Say IntelliJ)
  • Android SDK for Android Emulator 
  • App APK which will be tested 
  • Appium Server
(We will not discuss on how to set up for Android Emulator etc. The assumption is, we have Android SDK installed, Emulators are created and running, Appium server is installed and up and running.)


Steps

  • Create a maven project in eclipse and make sure that java-client dependency is added. 

pom.xml 





  4.0.0

  FirstAndroid
  FirstAndroid
  0.0.1-SNAPSHOT
  jar

  FirstAndroid
  http://maven.apache.org

  
    UTF-8
  

  
    
            io.appium
            java-client
            7.3.0
            
  
            org.testng
            testng
            6.8.5
        
  






After building the project, lets create our first test case. We will use classic google calculator as our AUT (Application under test).

FirstAndroidTestCase.java


package FirstAndroid.FirstAndroid;

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

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;

public class FirstAndroidTestCase {
 
 public AndroidDriver driver;
  @Test
  public void f() {
   WebElement two=driver.findElement(By.id("com.android.calculator2:id/digit_2"));
    two.click();
    driver.findElement(By.id("com.android.calculator2:id/op_add")).click();
    driver.findElement(By.id("com.android.calculator2:id/digit_2")).click();
    driver.findElement(By.id("com.android.calculator2:id/eq")).click();   
  }
  @BeforeClass
  public void beforeClass() throws MalformedURLException {
   
   DesiredCapabilities caps = new DesiredCapabilities();
   caps.setCapability(MobileCapabilityType.DEVICE_NAME, "Pixel_2_XL_API_27");
      caps.setCapability(MobileCapabilityType.UDID, "emulator-5554"); //DeviceId
      //caps.setCapability("platformName", "Android");
      //caps.setCapability("platformVersion", "8.1");
      //caps.setCapability("skipUnlock","true");
      caps.setCapability("appPackage", "com.android.calculator2");
      caps.setCapability("appActivity","com.android.calculator2.Calculator");
      caps.setCapability("noReset","false");
   driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), caps);
  }

  @AfterClass
  public void afterClass() {
  }
}

Lets see how to get the Device Name, UDID, AppPackage, AppActivity etc

Go to Android SDK -> Tools -> AVD Manager to see list of AVDs added. Get the Emulator name.

And from the command line we can get the UDID 



In the previous, blogs we have seen on how to get the appPackage and appActivity. You can copy the code and create scripts and run it. 


No comments:

Post a Comment