UiAutomator 我用 uiautomator2 在调试测试时报 “no test were found”

tony · 2018年07月03日 · 1518 次阅读

麻烦大神帮我看看,在 pc 连接真机执行时,报 no tests were found. 我的安卓手机版本为 7.1.
Running tests
Test running startedTest running failed: No test results
Empty test suite.
Gradle 配置:
apply plugin: 'com.android.application'

android {
compileSdkVersion 21
buildToolsVersion "21.1.2"

defaultConfig {
applicationId "com.pingan.myapp1"
minSdkVersion 18
targetSdkVersion 28
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:21.+'
compile files('libs/espresso-contrib-2.2-release-no-dep.jar')
compile files('libs/espresso-core-2.2-release-no-dep.jar')
compile files('libs/espresso-idling-resource-2.2-release-no-dep.jar')
compile files('libs/espresso-intents-2.2-release-no-dep.jar')
compile files('libs/espresso-web-2.2-release-no-dep.jar')
compile files('libs/exposed-instrumentation-api-publish-0.3-release-no-dep.jar')
compile files('libs/guava-18.0.jar')
compile files('libs/hamcrest-core-1.3.jar')
compile files('libs/hamcrest-integration-1.3.jar')
compile files('libs/hamcrest-library-1.3.jar')
compile files('libs/javawriter-2.1.1.jar')
compile files('libs/javax.annotation-api-1.2.jar')
compile files('libs/javax.inject-1.jar')
compile files('libs/jsr305-2.0.1.jar')
compile files('libs/junit-4.12.jar')
compile files('libs/rules-0.3-release-no-dep.jar')
compile files('libs/runner-0.3-release-no-dep.jar')
compile files('libs/uiautomator-v18-2.1.1-release-no-dep.jar')
}
测试代码:
package com.pingan.myapp1;

+import android.app.Instrumentation;

/**

  • Created by Administrator on 2018/7/3.
    */
    @RunWith(AndroidJUnit4.class)
    public class PinganAppTest {
    private static Instrumentation mInstrumentation;
    private UiDevice mDevice;
    String toastMessage;
    Long toastOccurTime;
    private final int LAUNCH_TIMEOUT = 5000;

    @Before
    public void setUp() {
    mInstrumentation = InstrumentationRegistry.getInstrumentation();
    mDevice = UiDevice.getInstance(mInstrumentation);
    initToastListener();
    }
    private static void initToastListener() {

    mInstrumentation.getUiAutomation().setOnAccessibilityEventListener(new UiAutomation.OnAccessibilityEventListener() {
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
    Log.i("AAA", "onAccessibilityEvent: " + event.toString());
    //判断是否是通知事件
    if (event.getEventType() != AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
    return;
    }
    //获取消息来源
    String sourcePackageName = (String) event.getPackageName();
    //获取事件具体信息
    Parcelable parcelable = event.getParcelableData();
    //如果不是下拉通知栏消息,则为其它通知信息,包括 Toast
    if (!(parcelable instanceof Notification)) {
    Log.i("AAA",event.getText().toString());
    String toastMessage = (String) event.getText().get(0);
    Long toastOccurTime = event.getEventTime();
    Log.i("AAA", "Latest Toast Message: " + toastMessage + " [Time: " + toastOccurTime + ", Source: " + sourcePackageName + "]");
    }else {
    Log.i("AAA",event.getParcelableData().toString());
    }
    }
    });
    }

    @Test
    public void testLoginFailed() {
    UiObject2 username = mDevice.wait(Until.findObject(By.depth(18).clazz(TextView.class)), LAUNCH_TIMEOUT);
    username.clear();
    username.setText("15888888881");
    UiObject2 password = mDevice.wait(Until.findObject(By.depth(18).text("请输入您的密码")), LAUNCH_TIMEOUT);
    password.clear();
    password.setText("paic@1234");
    UiObject2 smscode = mDevice.wait(Until.findObject(By.depth(19).clazz(TextView.class)), LAUNCH_TIMEOUT);
    smscode.clear();
    smscode.setText("111111");
    UiObject2 loginButton = mDevice.findObject(By.desc("登录"));
    loginButton.click();
    // Verify the test is displayed in the Ui
    final long startTimeMillis = SystemClock.uptimeMillis();
    boolean isSuccessfulCatchToast;
    while (true) {
    long currentTimeMillis = SystemClock.uptimeMillis();
    long elapsedTimeMillis = currentTimeMillis - startTimeMillis;
    if (elapsedTimeMillis > 5000L) {
    Log.i("AAA", "超过 5s 未能捕获到预期 Toast!");
    isSuccessfulCatchToast = false;
    break;
    }
    if (toastOccurTime > startTimeMillis) {

    isSuccessfulCatchToast = "验证码错误".equals(toastMessage);
    break;
    }
    }
    Assert.assertTrue("捕获预期 Toast 失败!", isSuccessfulCatchToast);
    }}

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册