Android Espresso无法正常运行

时间:2015-01-18 23:10:45

标签: android android-espresso

我正在挖一个多星期用于浓缩咖啡的Android测试。 我不能让Idle资源在我的项目中正常工作(集成到项目中)。 这是我在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上所有的工作,在其他机器人< 5 - 没有。

我已经挖掘了很多样本​​,并且确实实现了Idle res。但我对gradle系统太糟糕了。 请帮助,我非常渴望浓咖啡和gradle,如果需要,我可以添加更多规格代码。

1 个答案:

答案 0 :(得分: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()));
 }
}