我一直在实施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(espresso测试): @LargeTest 公共类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.onView(ViewMatchers.withId(R.id.password))。执行(ViewActions.typeText ( “123”));
我们收到PerformException:无法按ID找到按钮。 如果我们这样做(通过在UI线程上设置Runnable),我们就可以通过这个简单的演示来完成。 在我的主应用程序中还有其他的浓咖啡漏洞(上面写的“哈克”不起作用,我们收到同样的错误)。当然,我有一些非常棘手的错误,我认为项目(浓缩咖啡)设置有问题 - 我对gradle太糟糕了。 请帮我这个,矿石提供链接到Android工作室示例应用程序与espresso测试(我发现没有,所有的应用程序都配置不当(没有gradle),并导入到Android工作室后,我无法用
gradlew connectedAndroidTest
答案 0 :(得分:1)
我有同样的问题,我认为主要的问题是,按钮,打开一个新的缩进,但似乎这不会导致错误,这很奇怪,它只有在调用click()之前在EditText上键入Text时才会发生。
P.S:我设法解决了这个问题,似乎我遇到了一些双重的依赖问题,一旦我解决了这个问题,测试就没有任何问题,而且无需做任何解决。我的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'
}