gpt4 book ai didi

android - 简单 Espresso 测试 "Looped for x iterations over 60sec"错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:24:45 24 4
gpt4 key购买 nike

我实际上尝试用 Espresso 设置一些单元测试,经过几个小时的研究,该应用程序只执行单击并通过 EditText 获得焦点,但之后什么都没有

Caused by: android.support.test.espresso.AppNotIdleException: Looped for 1996 iterations over 60 SECONDS. The following Idle Conditions failed .

我删除了所有动画和 SwipeRefreshLayout,因为我看到 swiperefresh 有一个错误

我实际上使用了一些回调来替换 Activity 中的当前 fragment

如果有人有一些提示,我会在搜索 u_u 4 小时后离开

谢谢你:)


我的 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());
}
}

这是我的 Activity :

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();
}
}

还有我的登录 fragment :

    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();
}
});
}

最佳答案

我找到了解决方案,循环迭代就是由此造成的

<ProgressBar
android:id="@+id/loading_spinner"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:indeterminateTintMode="src_atop"
android:indeterminateTint="@color/ganjify"
android:alpha="0"
android:layout_gravity="center" />

因为我没有在不使用它时将可见性设置为 GONE!

loading.setVisibility(View.GONE);

关于android - 简单 Espresso 测试 "Looped for x iterations over 60sec"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36942451/

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