1

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.

Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66
Mitul Parekh
  • 151
  • 1
  • 8
  • Please check if [this](http://www.qaautomated.com/2016/03/appium-setup-in-android-studio-with.html) link helps. – Krishnan Mahadevan Sep 12 '17 at 14:55
  • I all ready used this link, but not get any solution. and my program is not run succesfully – Mitul Parekh Sep 13 '17 at 06:57
  • Can you please help elaborate as to what happens ? Do you see any errors ? are there any logs that you can share ? – Krishnan Mahadevan Sep 13 '17 at 06:58
  • This Type of Error is show in Console: Error:Gradle: Lambda coming from jar file need their interfaces on the classpath to be compiled, unknown interfaces are java.util.function.Function Error:Gradle: Lambda coming from jar file need their interfaces on the classpath to be compiled, unknown interfaces are java.util.function.Predicate Error:Gradle: Lambda coming from jar file need their interfaces on the classpath to be compiled, unknown interfaces are java.util.function.Predicate – Mitul Parekh Sep 13 '17 at 07:19
  • Error:Gradle: Lambda coming from jar file need their interfaces on the classpath to be compiled, unknown interfaces are java.util.function.Consumer Error:Gradle: Lambda coming from jar file need their interfaces on the classpath to be compiled, unknown interfaces are java.util.function.Supplier Error:Gradle: Lambda coming from jar file need their interfaces on the classpath to be compiled, unknown interfaces are java.util.function.BiConsumer – Mitul Parekh Sep 13 '17 at 07:20
  • Error:Gradle: Lambda coming from jar file need their interfaces on the classpath to be compiled, unknown interfaces are java.util.function.BiConsumer Error:Gradle: Lambda coming from jar file need their interfaces on the classpath to be compiled, unknown interfaces are java.util.function.BiConsumer Error:Gradle: Lambda coming from jar file need their interfaces on the classpath to be compiled, unknown interfaces are java.util.function.Function Error:Gradle: Lambda coming from jar file need their interfaces on the classpath to be compiled, unknown interfaces are java.util.function.Predicate – Mitul Parekh Sep 13 '17 at 07:21
  • You are experiencing incompatibilities with JDK8. I think you may have to update your build.gradle to use atleast Android SDK 25 or above. Your `minSdkVersion` is defined as `19`. Try updating that to `26` and see if that helps (Selenium now requires JDK8). For more information please refer to [this](https://stackoverflow.com/questions/41735756/cannot-use-lambda-functions-in-android-with-java-1-8) SO thread and see if that helps – Krishnan Mahadevan Sep 13 '17 at 07:56
  • If i use minSdkVersion is 26 that time same issue are show and also gradle build take more time. – Mitul Parekh Sep 13 '17 at 10:04
  • I don't think the problem involves TestNG anymore. You would need to figure out how to get your project to build with JDK8. – Krishnan Mahadevan Sep 13 '17 at 10:08

1 Answers1

0

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

dmle
  • 3,498
  • 1
  • 14
  • 22