Run Demo Program using TestNG on Android Studio

998 views Asked by At

I am new to TestNG and I tried to run the below code, but it doesnt seem to work with TestNG. The Below code opens the calculator app using TestNG, but some how it is not working.

How do we get the demo program to run using TestNG in android studio on Appium server?

  1. This is my Java File.
package com.example.apple.appiumapplication;    
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

public class FirstTest {

    WebDriver driver;

    @BeforeMethod
    public void setUp() throws MalformedURLException {
        // Created object of DesiredCapabilities class.
        DesiredCapabilities capabilities = new DesiredCapabilities();

        // Set android deviceName desired capability. Set your device name.
        capabilities.setCapability("deviceName", "Mi phone");

        // Set BROWSER_NAME desired capability. It's Android in our case here.
        capabilities.setCapability(CapabilityType.BROWSER_NAME, "Android");

        // Set android VERSION desired capability. Set your mobile device's OS version.
        capabilities.setCapability(CapabilityType.VERSION, "5.0.2");

        // Set android platformName desired capability. It's Android in our case here.
        capabilities.setCapability("platformName", "Android");

        // Set android appPackage desired capability. It is
        // com.android.calculator2 for calculator application.
        // Set your application's appPackage if you are using any other app.
        capabilities.setCapability("appPackage", "com.miui.calculator");
        // Set android appActivity desired capability. It is
        // com.android.calculator2.Calculator for calculator application.
        // Set your application's appPackage if you are using any other app.      capabilities.setCapability("appActivity","com.miui.calculator.cal.CalculatorActivity");

        // Created object of RemoteWebDriver will all set capabilities.
        // Set appium server address and port number in URL string.
        // It will launch calculator app in android device.
        driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    }

    @Test
    void testFirstCalculator() {

        // Click on DELETE/CLR button to clear result text box before running test.
        //driver.findElements(By.xpath("//android.widget.Button")).get(5).click();
        //driver.findElement(By.id("com.miui.calculator:id/btn_7")).getText();
        driver.findElement(By.xpath("//android.widget.Button[@text='8' and @index='1']")).click();
        // Click on number 2 button.
        //driver.findElement(By.name("7")).click();
        //driver.findElement(By.name("6")).click();

        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @AfterMethod
    public void End() {
        driver.quit();
    }
}
  1. This is my build.gradle file
apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "com.example.apple.appiumapplication"
        minSdkVersion 19
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        enter code here
        jackOptions {
            enabled true
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LGPL2.1'
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:design:26.+'
    testCompile 'org.testng:testng:6.10'
    compile files('libs/cglib-nodep-3.2.4.jar')
    compile files('libs/client-combined-3.5.3-nodeps.jar')
    compile files('libs/commons-codec-1.10.jar')
    compile files('libs/commons-exec-1.3.jar')
    compile files('libs/commons-io-2.5.jar')
    compile files('libs/commons-lang3-3.5.jar')
    compile files('libs/commons-logging-1.2.jar')
    compile files('libs/cssparser-0.9.23.jar')
    compile files('libs/gson-2.8.0.jar')
    compile files('libs/guava-23.0.jar')
    compile files('libs/hamcrest-core-1.3.jar')
    compile files('libs/htmlunit-2.27.jar')
    compile files('libs/htmlunit-core-js-2.27.jar')
    compile files('libs/htmlunit-driver-2.27.jar')
    compile files('libs/httpclient-4.5.3.jar')
    compile files('libs/httpcore-4.4.6.jar')
    compile files('libs/httpmime-4.5.3.jar')
    compile files('libs/java-client-5.0.0-BETA1.jar')
    compile files('libs/javax.servlet-api-3.1.0.jar')
    compile files('libs/jetty-client-9.4.5.v20170502.jar')
    compile files('libs/jetty-http-9.4.5.v20170502.jar')
    compile files('libs/jetty-io-9.4.5.v20170502.jar')
    compile files('libs/jetty-util-9.4.5.v20170502.jar')
    compile files('libs/jna-4.1.0.jar')
    compile files('libs/jna-platform-4.1.0.jar')
    compile files('libs/junit-4.12.jar')
    compile files('libs/neko-htmlunit-2.27.jar')
    compile files('libs/phantomjsdriver-1.4.0.jar')
    compile files('libs/sac-1.3.jar')
    compile files('libs/serializer-2.7.2.jar')
    compile files('libs/websocket-api-9.4.5.v20170502.jar')
    compile files('libs/websocket-client-9.4.5.v20170502.jar')
    compile files('libs/websocket-common-9.4.5.v20170502.jar')
    compile files('libs/xalan-2.7.2.jar')
    compile files('libs/xercesImpl-2.11.0.jar')
    compile files('libs/xml-apis-1.4.01.jar')
}

Thanks.

1

There are 1 answers

0
dmle On

The good way is to create separate project for Appium UI tests and do not mess inside you Android application project.

Thats the main benefit of Appium - test application without affecting it source code, so why not use it.

If you have reasons to write tests inside your Android project, it make sense to do it with Espresso