Search code examples
android-studioappium

How to configure Appium and Android Studio?


Hi am very new in appium , I have run my first test case (Java with TestNG) using eclipse but now i want switch to Android Studio. Please provide me step by step process to add jar's and other things into Android Studio


Solution

  • I recommend you to use IntelliJ IDEA. You can create project with gradle where you can insert dependencies, but you can include easly .jars by File -> Project structure -> Libraries -> (+) -> Java and then select your .jar file.

    IntelliJ IDEA got testNG already installed so you don't have to install it.

    For testing all you need are gson-2.2.2, java-client-4.0.0, selenium-java-2.53.1 jars.

    To connect with your device use class

    public class Setup {
        private final String DEVICE_NAME = "deviceName";
        private final String PLATFORM_NAME = "platformName";
        private final String PLATFORM_VERSION = "platformVersion";
        private final String APP_PACKAGE = "appPackage";
        private final String APP_ACTIVITY = "appActivity";
    
        private String deviceName = "Android SDK built for x86"; //device name can be found in device settings
        private String platformName = "Android"; 
        private String platformVersion = "6.0"; //version of your android
        private String port = "4723"; //port from Appium server
        private String url; 
    
        private String getIp() throws UnknownHostException {
            InetAddress ip = InetAddress.getLocalHost();
            return ip.getHostAddress();
        }
    
        public AndroidDriver establishConnection() throws MalformedURLException {
            try {
                url = String.format("http://%s:%s/wd/hub", getIp(), port);
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
            DesiredCapabilities capability = new DesiredCapabilities();
            capability.setCapability(DEVICE_NAME, deviceName);
            capability.setCapability(PLATFORM_NAME, platformName);
            capability.setCapability(PLATFORM_VERSION, platformVersion);
    
            capability.setCapability(APP_PACKAGE, "my.app.package");
            capability.setCapability(APP_ACTIVITY, "my.app.activity");
    
            return new AndroidDriver(new URL(url), capability);
        }
    }
    

    After this you can create new class with @BeforeClass where you can create object of Setup class, call establishConnection(); and initialize driver and test your app UI with @Test methods. Don't forget to install .apk first on your device :)