我实际上试图设置一些单元测试与咖啡和经过几个小时的研究,应用程序只做点击,并获得焦点的编辑文本,但之后没有什么
Caused by: android.support.test.espresso.AppNotIdleException: Looped for 1996 iterations over 60 SECONDS. The following Idle Conditions failed .
我已经删除了所有的动画和SwipeRefreshLayout,因为我看到有一个与swipefresh错误
我实际上使用了一些回调函数来替换Activity中的当前片段
如果有人有一些提示,我在搜索u_u 4小时后就出来了。
Thank you:)
我的Gradle依赖项:
// App dependencies
compile 'com.android.support:support-annotations:23.3.0'
compile 'com.google.guava:guava:18.0'
// Testing-only dependencies
// Force usage of support annotations in the test app, since it is internally used by the runner module.
androidTestCompile 'com.android.support:support-annotations:23.3.0'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.2'
我在defaultConfig中添加了以下内容:
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
这是我的测试:
@RunWith(AndroidJUnit4.class)
@LargeTest
public class HelloWorldEspressoTest {
public static final String USERNAME = "do_f";
@Rule
public ActivityTestRule<LoginActivity> mActivityRule = new ActivityTestRule(LoginActivity.class);
private LoginActivity mActivity = null;
@Before
public void setActivity() {
mActivity = mActivityRule.getActivity();
}
@Test
public void login_LoginActivity() {
onView(withId(R.id.menu_login)).perform(click());
onView(withId(R.id.login_username))
.perform(typeText(USERNAME), closeSoftKeyboard());
}
}
我的活动:
public class LoginActivity extends AppCompatActivity
implements MenuFragment.OnFragmentInteractionListener {
private FragmentManager fm;
public static void newActivity(Activity activity)
{
Intent i = new Intent(activity, LoginActivity.class);
activity.startActivity(i);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
SharedPreferences sp = getSharedPreferences(Utils.SP, Context.MODE_PRIVATE);
if (!sp.getString(Utils.TOKEN, "null").equals("null"))
{
MainActivity.newActivity(this);
finish();
}
fm = getFragmentManager();
fm.beginTransaction()
.replace(R.id.container, MenuFragment.newInstance())
.addToBackStack(null)
.commit();
}
@Override
public void onBackPressed()
{
if (getFragmentManager().getBackStackEntryCount() > 1)
getFragmentManager().popBackStack();
else
super.onBackPressed();
}
@Override
public void showLogin() {
fm.beginTransaction()
.replace(R.id.container, LoginFragment.newInstance())
.addToBackStack(null)
.commit();
}
@Override
public void showRegister() {
fm.beginTransaction()
.replace(R.id.container, RegisterFragment.newInstance())
.addToBackStack(null)
.commit();
}
}
我的LoginFragment:
private static final String TAG = "LoginFragment";
@Bind(R.id.loading_spinner)
ProgressBar loading;
@Bind(R.id.content)
LinearLayout content;
@Bind(R.id.login_username)
EditText username;
@Bind(R.id.login_password)
EditText password;
public LoginFragment() {
}
public static LoginFragment newInstance() {
return new LoginFragment();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.login_fragment_login, container, false);
ButterKnife.bind(this, v);
return v;
}
@Override
public void onActivityCreated(Bundle saveInstanceState) {
super.onActivityCreated(saveInstanceState);
}
@OnClick(R.id.login_submit)
public void onSubmit(View v)
{
if (username.getText().length() == 0
|| password.getText().length() == 0)
{
Snackbar.make(getView(), "blabla", Snackbar.LENGTH_SHORT).show();
return ;
}
//hideContent();
LoginPost p = new LoginPost(username.getText().toString(), password.getText().toString());
Call<LoginResponse> call = RestClient.get().login(p);
call.enqueue(new Callback<LoginResponse>() {
@Override
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
if (response.body() == null) {
String msg = getResources().getString(R.string.login_error_bad_credentials);
Snackbar.make(getView(), msg, Snackbar.LENGTH_SHORT).show();
//showContent();
} else {
SharedPreferences sp = getActivity().getSharedPreferences(Utils.SP, Context.MODE_PRIVATE);
sp.edit().putString(Utils.TOKEN, response.body().getToken()).apply();
sp.edit().putString(Utils.USERNAME, username.getText().toString()).apply();
MainActivity.newActivity(getActivity());
getActivity().finish();
}
}
@Override
public void onFailure(Call<LoginResponse> call, Throwable t) {
Snackbar.make(getView(), "onFailure", Snackbar.LENGTH_SHORT).show();
//showContent();
}
});
}
3条答案
按热度按时间4zcjmb1e1#
我找到了解决方案,循环迭代就是由此引起的
因为我没有设置的可见性去当我不使用它!
1aaf6o9v2#
Espresso被设计为等到应用程序中没有挂起的UI动画或AsyncTasks时才继续测试。从the docs,强调我的:
Espresso提供了一组复杂的同步功能。然而,框架的这一特性仅适用于在MessageQueue上发布消息的操作,例如在屏幕上绘制其内容的View子类。
android.support.test.espresso.AppNotIdleException
最可能的原因是Espresso正在等待完成正在进行的UI动画或AsyncTask。例如,如果您的布局中有一个ProgressBar元素,则需要设置可见性
loading.setVisibility(View.GONE);
,因为Espresso将该元素解释为正在进行的UI动画,并且将等待它消失以继续测试。1cosmwyk3#
在我的案例中,此问题的根本原因是MotionLayout具有无限动画。如果你有一些循环的动画,永远你的应用程序将不会达到空闲状态,测试可能会停留在一个随机的ViewInteraction,而一些EspressoView正在等待空闲状态。