安卓Espresso无法正常工作

3

我已经花了一个多星期的时间使用Espresso进行Android测试。 但是我无法使空闲资源在我的项目中正常工作(集成到项目中)。 这是我添加到build.gradle的内容:

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

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

测试类中包含哪些内容:

    SetUp:
countingResource = new CountingIdlingResource("HelloWorldServerCalls");
Espresso.registerIdlingResources(countingResource);    

在测试方法中:

Espresso.onView(ViewMatchers.withId(R.id.btnLogIn)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
        Log.d(TAG, "We are in WalkthroughActivity, user is not logged in");

//press button  - walk to next activity
Espresso.onView(ViewMatchers.withId(R.id.btnLogIn)).perform(ViewActions.click());

//register MyUserHelperV2 - this is Server decorator
 final LoginActivity act = (LoginActivity) getCurrentActivity();
        LoginActivity.Server aHelper = act.getUserHelper();
        MyUserHelperV2 helper = new MyUserHelperV2(aHelper, countingResource);
        act.setUserHelper(helper);

    //set password and email
  Espresso.onView(ViewMatchers.withId(R.id.email)).perform(ViewActions.typeText("test@mail.ru"));
    Espresso.onView(ViewMatchers.withId(R.id.password)).perform(ViewActions.typeText("password111"));
         //Check if button R.id.btnLogInApp exists: 
Espresso.onView(ViewMatchers.withId(R.id.btnLogInApp)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));

Espresso.closeSoftKeyboard();
    Espresso.onView(ViewMatchers.withId(R.id.btnLogInApp)).perform(ViewActions.click());
    //in last line we have PerformException - can not find R.id.btnLogInApp, 

但是这个按钮肯定存在 - 我可以模拟非空闲(同步)调用,所有东西都正常工作。 我认为我可能在设置方面犯了一个错误 - 在Android 5上所有的工作都正常,在其他小于Android 5的版本上则不行。

我已经仔细查看了很多示例,并且完美地实现了Idle res。但是我的gradle系统太差了。 请帮帮我,我对espresso和gradle感到绝望,如果需要,我可以附加更多的规范代码。


1
请问您能否发布您遇到的错误信息? - Testing Singh
1个回答

0

这是一个 build.gradle 的示例文件

apply plugin: 'com.android.application'

android {
  compileSdkVersion 22
  buildToolsVersion "22"

defaultConfig {
    applicationId "com.my.awesome.app"
    minSdkVersion 10
    targetSdkVersion 22.0.1
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  }
}

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

// Testing-only dependencies
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
}

一个测试类的示例

Android Studio默认在src/androidTest/java/com.example.package/中创建测试

@RunWith(AndroidJUnit4.class)
@LargeTest
 public class HelloWorldEspressoTest {

@Rule
 public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule(MainActivity.class);

@Test
 public void listGoesOverTheFold() {
    onView(withText("Hello world!")).check(matches(isDisplayed()));
 }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接