我正在编写一个经过检测的Android测试。当我点击按钮sample_btn
时,Task
和DummyTask
开始。当Task
结束时,EditText sample_text
变为可见,我可以在那里输入一些文字。
当我注意到以下事实时,我感到很惊讶。 onView(withId(R.id.sample_btn)).perform(click());
等待Task
和DummyTask
完成。为什么呢?
MainActivity
package com.sample;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import java.lang.ref.WeakReference;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.sample_btn).setOnClickListener(this);
}
@Override
public void onClick(final View v) {
switch (v.getId()) {
case R.id.sample_btn:
Log.d("UnderstandIsDisplayed", "Before sample_btn click IN CODE");
new Task(this).execute();
new DummyTask().execute();
Log.d("UnderstandIsDisplayed", "After sample_btn click IN CODE");
break;
}
}
private static class DummyTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(final Void... params) {
try {
Log.d("UnderstandIsDisplayed", "DummyTask doInBackground");
Thread.sleep(8000);
Log.d("UnderstandIsDisplayed", "DummyTask doInBackground done");
} catch (InterruptedException e) {
}
return null;
}
}
private static class Task extends AsyncTask<Void, Void, Void> {
final WeakReference<MainActivity> mActivityWeakReference;
Task(MainActivity activity) {
mActivityWeakReference = new WeakReference<>(activity);
}
@Override
protected Void doInBackground(final Void... params) {
try {
Log.d("UnderstandIsDisplayed", "doInBackground");
Thread.sleep(4000);
Log.d("UnderstandIsDisplayed", "doInBackground done");
} catch (InterruptedException e) {
}
return null;
}
@Override
protected void onPostExecute(final Void aVoid) {
super.onPostExecute(aVoid);
MainActivity mainActivity = mActivityWeakReference.get();
if (mainActivity != null) {
mainActivity.findViewById(R.id.sample_text).setVisibility(View.VISIBLE);
Log.d("UnderstandIsDisplayed", "sample_text VISIBLE");
}
}
}
}
MainActivityTest
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
private MainActivity mMainActivity;
public MainActivityTest() {
super(MainActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
injectInstrumentation(InstrumentationRegistry.getInstrumentation());
mMainActivity = getActivity();
}
public void testFoo() {
Log.d("UnderstandIsDisplayed", "Before sample_btn click");
onView(withId(R.id.sample_btn)).perform(click());
Log.d("UnderstandIsDisplayed", "After sample_btn click");
// TODO: Wait for sample_text to become visible
onView(withId(R.id.sample_text)).perform(typeText("sample_text"));
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/container"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#336633"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.sample.MainActivity">
<Button
android:id="@+id/sample_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="sample_btn"/>
<EditText
android:id="@+id/sample_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
</LinearLayout>
的build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "24.0.1"
defaultConfig {
applicationId "com.sample"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.0.1'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
}
这是我在运行应用程序时看到的内容:
09-07 12:09:02.623 22356-22356/com.sample D/UnderstandIsDisplayed: Before sample_btn click IN CODE
09-07 12:09:02.629 22356-22660/com.sample D/UnderstandIsDisplayed: doInBackground 1562
09-07 12:09:02.631 22356-22356/com.sample D/UnderstandIsDisplayed: After sample_btn click IN CODE
09-07 12:09:06.670 22356-22660/com.sample D/UnderstandIsDisplayed: doInBackground done
09-07 12:09:06.672 22356-22356/com.sample D/UnderstandIsDisplayed: sample_text VISIBLE
09-07 12:09:06.680 22356-22771/com.sample D/UnderstandIsDisplayed: DummyTask doInBackground 1563
09-07 12:09:14.721 22356-22771/com.sample D/UnderstandIsDisplayed: DummyTask doInBackground done
这是我在运行testFoo
时看到的:
09-07 12:10:24.357 24892-24911/com.sample D/UnderstandIsDisplayed: testFoo = 1555
09-07 12:10:24.357 24892-24911/com.sample D/UnderstandIsDisplayed: Before sample_btn click
09-07 12:10:24.429 24892-24892/com.sample D/UnderstandIsDisplayed: Before sample_btn click IN CODE
09-07 12:10:24.431 24892-24954/com.sample D/UnderstandIsDisplayed: doInBackground 1563
09-07 12:10:24.431 24892-24892/com.sample D/UnderstandIsDisplayed: After sample_btn click IN CODE
09-07 12:10:28.465 24892-24954/com.sample D/UnderstandIsDisplayed: doInBackground done
09-07 12:10:28.469 24892-24892/com.sample D/UnderstandIsDisplayed: sample_text VISIBLE
09-07 12:10:28.472 24892-24936/com.sample D/UnderstandIsDisplayed: DummyTask doInBackground 1558
09-07 12:10:36.483 24892-24936/com.sample D/UnderstandIsDisplayed: DummyTask doInBackground done
09-07 12:10:36.702 24892-24911/com.sample D/UnderstandIsDisplayed: After sample_btn click
答案 0 :(得分:1)
使用Espresso
,您只能在测试环境下的应用内操作。这意味着Espresso用于执行任何操作需要在app的主线程上运行。它检查UI线程何时为idle()
,如果不是,则等待UI线程再次空闲。
检查:http://dev.jimdo.com/2014/05/09/wait-for-it-a-deep-dive-into-espresso-s-idling-resources/
在某些情况下,它会产生Espressso IdlingResources
错误,例如AppNotIdleState
:https://developer.android.com/reference/android/support/test/espresso/AppNotIdleException.html
示例1#:https://github.com/81813780/AVLoadingIndicatorView/issues/16
示例2#:Espresso test fails after successful click and blocking for 60 seconds
要处理此问题,您可以创建自己的IdlingResource
方法,以便在UI线程为Espresso
时为idle
说明。
检查:https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
例如:http://blog.sqisland.com/2015/04/espresso-custom-idling-resource.html
如果无效,请尝试与Espresso
另一个名为uiatomator
的Google测试框架一起使用,这可能会帮助您解决此问题。
就我而言,编写自己的IdlingResources
无效,但将Espresso
与Robotium
混合(不等待空闲的UI线程)和uiautomator
完成了它的工作。
希望它会有所帮助
答案 1 :(得分:1)
AsyncTask
处理已融入UiControllerImpl.loopMainThreadUntilIdle()
中的espresso框架。在大多数情况下,espresso处理一些开箱即用的背景工作非常方便。不幸的是,没有可用的解决方案来关闭任何提供的处理程序(UI线程同步,异步任务)。这使得测试进度指标或测试中断变得非常复杂。
espresso bug tracker中有一个未解决的问题,但它没有引起太多关注。