1

我是 TestNG 的新手,我尝试运行以下代码,但它似乎不适用于TestNG。下面的代码使用 TestNG 打开了计算器应用程序,但有些代码无法正常工作。

我们如何让演示程序在Appium 服务器上的android studio中使用TestNG运行?

  1. 这是我的 Java 文件。
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. 这是我的 build.gradle 文件
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')
}

谢谢。

4

1 回答 1

0

好的方法是为 Appium UI 测试创建单独的项目,不要在你的 Android 应用程序项目中搞乱。

这就是 Appium 的主要好处——测试应用程序而不影响它的源代码,所以为什么不使用它。

如果你有理由在你的 Android 项目中编写测试,那么使用Espresso是有意义的

于 2017-11-19T20:04:44.420 回答