Preface: Dear Reader, please note that I will be explaining the details in layman terms so as to reach more audience who are very new to Android / iOS Automation.
Prerequisites : In order to run the Code on your device – the phone must be in Developer mode. The Phone is converted into Developer mode by :
- Go to Phone Setting -> system -> About Device -> Click 7 times on Build Details. You will be notified that you are now in Developer mode.
- Once you are in Developer Mode – > go to setting -> System-> devices -> Developer Options – > Enable Stay Awake and USB Debugging.
- Once you complete these steps – > Connect your device to the system-> go to cmd-> type “adb devices” ->Enter -> all the devices connected to the system will be listed. Not the list is empty is the phone is not in developer mode and USB Debugging is not Enabled.
There are 3 important sections that are mandatory to run your automation script on Android Phone.
Your Phone Details :
- Your phone details such as Device Name, Platform Name and Platform Version are the basic requirements.
- To find the above details do the following – Go to Settings on your phone under test -> System -> About Devices
- Device name is usually the Model Number. In my case it is – > SM-N910G
- Platform Name – > Android
- Platform Version -> Android Version. In my case it is 6.0.1
- The below code of java helps in identifying your device.
DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability(“deviceName”, “SM-N910G”); capabilities.setCapability(“platformName”, “Android”); capabilities.setCapability(“platformVersion”, “6.0.1”);
Details of the Application to be automated :
- We will need Application package and launchable Application activity.
- This can be found out in 3 different ways.
- Using Appinfo app installed on your phone – > this lists all the applications and on long pressing the application information you need – > it lists the package and all the activity details of that application.
- using dumpsys tool -> which 70% of the times provides the information needed but not always.
- go to cmd – > enter “adb shell“
- keep the application open on ur phone and type “dumpsys window windows | grep ‘mCurrentFocus’ “
- This provides the phone current active window details which is what we need.
- using aapt tool – > we will see this in the upcoming classes.
- The code for setting application details is as below – in my case I will be using flipboard activity & package.
capabilities.setCapability(“appPackage”, “flipboard.app”); capabilities.setCapability(“appActivity”, “flipboard.activities.LaunchActivity”);
Appium Driver that runs the code on your phone:
- AndroidDriver: This driver class inherits from AppiumDriver, but it adds in additional functions that are useful in the context of a mobile automation test on Android devices through Appium. Only use this driver class if you want to start a test on an Android device or Android emulator. ( Please refer Differences between Drivers for more information)
- So we will use Android Driver to run your code on device this takes Appium Server URL and the capabilities as the arguments to the AndroidDriver Constructor.
- Below is the code for the same. (IP and port number is usually the same in all servers)
AndroidDriver driver=new AndroidDriver(new URL(“http://127.0.0.1:4723/wd/hub “),capabilities);
With this we have now Set our device details, Application details and appium Server details, below is the code combining all the above information. This code will help you identify the device and launch the application.
To Run this code:
- Start cmd prompt [Run this command to start your Appium server]
<“C:Program Files (x86)Appiumnode.exe” “C:Program Files (x86)Appiumnode _modulesappiumbinappium.js” You can also create a Batch file and keep it ready.- Run the code below on Eclipse “Run as Java Application”.
Code:
package Day1;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
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 io.appium.java_client.android.AndroidDriver;
public class Flipboard_01 {
public static void main(String[] args) throws MalformedURLException, InterruptedException {
// TODO Auto-generated method stub
//Launch App
//Device details
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(“deviceName”, “SM-N910G”);
capabilities.setCapability(“platformName”, “Android”);
capabilities.setCapability(“platformVersion”, “6.0.1”);
//App Details
capabilities.setCapability(“appPackage”, “flipboard.app”);
capabilities.setCapability(“appActivity”, “flipboard.activities.LaunchActivity”);
//AppiumServer Details
AndroidDriver driver=new AndroidDriver(new URL(“http://127.0.0.1:4723/wd/hub “),capabilities);
Thread.sleep(3000);
}
}