【问题标题】:Android Espresso wrong set up, or work not stable?Android Espresso 设置错误,或工作不稳定?
【发布时间】:2015-03-16 23:34:34
【问题描述】:

我已经实施了一周的 android espresso 测试。什么是真实的 - 正在实现服务器调用并用浓缩咖啡等待它。这称为空闲资源调用,我们必须遵循非常简单的规则。实际上我找到了解决方案,但结果令人震惊 - 只有当我做评论行时我才成功

Espresso.onView(ViewMatchers.withId(R.id.email)).perform(ViewActions.typeText("some shit"));
Espresso.onView(ViewMatchers.withId(R.id.password)).perform(ViewActions.typeText("123"));

并将它们替换为“huck”:

final EditText email = (EditText) act.findViewById(R.id.email);
        final EditText password = (EditText) act.findViewById(R.id.password);
        getInstrumentation().runOnMainSync(new Runnable() {
            public void run() {
                email.setText("Engineer");
                password.setText("2342");
            }
        });

..在模拟调用服务器后单击启动新活动的按钮之前。 这是我的全部文件: build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "shoppinglist.kizema.anton.testappespresso"
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    packagingOptions {
        exclude 'LICENSE.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/ASL2.0'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/DEPENDENCIES'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'

    // App's dependencies, including test
    compile 'com.android.support:support-annotations:21.0.3'

    // Testing-only dependencies
    androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
}

LoginActivity(第一个活动):

//set up initial listener
private void initLoginHelper(){
    loginHelper = new Server() {
        @Override
        public void login(String email, String code, String phone, String password, boolean loginByPhoneNumber) {
            //ask server
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            //done
            Intent intent = new Intent(LoginActivity.this, SecondActivity.class);
            startActivityForResult(intent, 0);
        }
    };
}

//onButtonClick handler
public void btnLogInSuka(View v) {
    performLogin();
}
void performLogin() {
    new Thread(new Runnable() {
        @Override
        public void run() {
                loginHelper.login(emailParam,codeParam,phoneParam,passwordParam,false);
        }
    }).start();
}

和 AplicationTest.java(浓缩咖啡测试): @大测试 公共类 ApplicationTest 扩展 ActivityInstrumentationTestCase2 {

public ApplicationTest() {
    super(LoginActivity.class);
}

CountingIdlingResource idleRes;

@Override
public void setUp() throws Exception {
    super.setUp();
    getActivity();

    idleRes = new CountingIdlingResource("server");
    Espresso.registerIdlingResources(idleRes);
}

public void testSample(){
    final LoginActivity act = (LoginActivity) getCurrentActivity();
    Server aHelper = act.getUserHelper();

    MyUserHelperExternalIdleRes helper2 = new MyUserHelperExternalIdleRes(idleRes, aHelper);
    act.setUserHelper(helper2);


    //if comment this and uncomment next two lines we receive PerformException
    final EditText email = (EditText) act.findViewById(R.id.email);
    final EditText password = (EditText) act.findViewById(R.id.password);
    getInstrumentation().runOnMainSync(new Runnable() {
        public void run() {
            email.setText("Engineer");
            password.setText("2342");
        }
    });

//        Espresso.onView(ViewMatchers.withId(R.id.email)).perform(ViewActions.typeText("some shit"));
//        Espresso.onView(ViewMatchers.withId(R.id.password)).perform(ViewActions.typeText("123"));

    Espresso.closeSoftKeyboard();
    Espresso.onView(ViewMatchers.withId(R.id.btnLogIn)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));

    Espresso.onView(ViewMatchers.withId(R.id.btnLogIn)).perform(ViewActions.click());
    Espresso.onView(ViewMatchers.withId(R.id.secondActivityOpened)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    Espresso.pressBack();

    Espresso.closeSoftKeyboard();
    Espresso.onView(ViewMatchers.withId(R.id.btnLogIn)).perform(ViewActions.click());
}

Activity getCurrentActivity() {
    getInstrumentation().waitForIdleSync();
    final Activity[] activity = new Activity[1];
    try {
        runTestOnUiThread(new Runnable() {
            @Override
            public void run() {
                java.util.Collection<Activity> activites = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED);
                activity[0] = Iterables.getOnlyElement(activites);
            }});
    } catch (Throwable throwable) {
        throwable.printStackTrace();
    }
    return activity[0];
}

class MyUserHelperExternalIdleRes implements Server {
    private Server aHelper;
    private CountingIdlingResource udleRes;

    public MyUserHelperExternalIdleRes(CountingIdlingResource udleRes, Server aHelper) {
        this.aHelper = aHelper;
        this.udleRes = udleRes;
    }

    @Override
    public void login(String email, String code, String phone, String password, boolean loginByPhoneNumber) {

        udleRes.increment();
        try {
            aHelper.login(email,code, phone,password,loginByPhoneNumber);
        } finally {
            udleRes.decrement();
        }
    }
}

}

所以,如果我们实际进行基本的 espresso 操作: Espresso.onView(ViewMatchers.withId(R.id.password)).perform(ViewActions.typeText ("123"));

我们收到 PerformException : can not find button by id。 如果我们做一个哈克,(在 UI 线程上设置 Runnable)我们就可以成功完成这个简单的演示。 在我的主应用程序中,espresso 还有其他错误(上面写的“huck”不起作用,我们收到同样的错误)。当然我有一些非常棘手的错误,我想项目(espresso)设置是错误的 - 我对 gradle 太糟糕了。 请帮我解决这个问题,或者提供一个带有 espresso 测试的 android studio 示例应用程序的链接(我发现没有,所有应用程序都配置错误(没有 gradle),并且在将它们导入到 android studio 后,我无法使用

gradlew connectedAndroidTest

【问题讨论】:

标签: android android-espresso


【解决方案1】:

我也遇到了同样的问题,我认为主要问题是,按钮会打开一个新的缩进,但似乎仅此一项并不会导致错误,这很奇怪,只有当你在调用 click() 之前在 EditText 上键入文本。

P.S:我设法解决了这个问题,似乎我遇到了一些双重依赖问题,一旦我解决了这个问题,测试就没有任何问题,也不需要做任何工作。

我的 espresso 的 build.gradle 部分是这样结束的:

dependencies {
    repositories {
        mavenCentral()
    }
    // Espresso
    compile 'org.apache.commons:commons-lang3:3.1'
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.0') {
        exclude group: 'com.google.guava'
        exclude module: 'espresso-idling-resource'
    }
    androidTestCompile('com.android.support.test:testing-support-lib:0.1') {
        exclude group: 'com.google.guava'
        exclude module: 'espresso-idling-resource'
    }
}

我还在依赖项之前添加了这个:

configurations {
  androidTestCompile.exclude group: 'com.android.support'
  androidTestCompile.exclude module: 'support-v4'
  androidTestCompile.exclude module: 'appcompat-v7'
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 2022-08-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多