gpt4 book ai didi

Android Espresso 无法正常工作

转载 作者:太空狗 更新时间:2023-10-29 14:59:47 25 4
gpt4 key购买 nike

我花了一个多星期的时间来寻找一个带有 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,

但是这个按钮肯定存在——我可以模拟 not-idle(sync) 调用并且一切正常。我在想我可能会在设置中犯错误 - 在 android 5 上一切正常,在其他 android 上 <5 - 没有。

我已经研究了很多示例,并且可以肯定地完美地实现了空闲资源。但是我对gradle系统太糟糕了。请帮忙,我对 espresso 和 gradle 很绝望,如果需要,我可以附上更多规范代码。

最佳答案

这是一个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()));
}
}

关于Android Espresso 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28015885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com