A. *_*and 6 android android-espresso
我想在现有项目上首次测试浓缩咖啡。在我的HomeFragment包含的中HomeActivity,我在显示另一个视图的按钮上设置了 OnClickListener,我在模拟器中运行该应用程序,一切正常。然后我想用浓缩咖啡测试它,但我的 OnClickListener 似乎从未被调用。我在开发者选项中删除了模拟器中的动画选项,但 espresso 测试仍然失败,并且日志确认 onClick 从未被调用。
android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError:“在屏幕上向用户显示”与所选视图不匹配。
预期:显示在屏幕上给用户
得到:“FilterPopupView {id = 2131558573,res-name = filter_view,可见性= GONE,宽度= 0,高度= 0,has-focus = false,has-focusable = false,has-window-focus = true,可点击=假、 is-enabled = true、 is-focused = false、 is-focusable = false、 is-layout-requested = true、 is-selected = false、 root-is-layout-requested = false、 has-input-connection =假,x=0.0,y=0.0,子计数=1}”
在HomeActivityTest
public class HomeActivityTest
extends ActivityInstrumentationTestCase2<HomeActivity> {
public HomeActivityTest() {
super(HomeActivity.class);
}
@Before
public void setUp() throws Exception {
super.setUp();
injectInstrumentation(InstrumentationRegistry.getInstrumentation());
getActivity();
}
public void testOpenFilter() {
Log.e("Tests", "Check button_filter is clickable");
onView(withId(R.id.button_filter)).check(matches(isClickable()));
Log.e("Tests", "Click on button_filter");
onView(withId(R.id.button_filter)).perform(click());
Log.e("Tests", "Check filter_view visibility");
onView(withId(R.id.filter_view)).check(matches(isDisplayed()));
}
}
Run Code Online (Sandbox Code Playgroud)
在build.gradle
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
versionCode 130
versionName '3.0-v2'
multiDexEnabled true
renderscriptTargetApi 20
renderscriptSupportModeEnabled true
vectorDrawables.useSupportLibrary = true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
...
dependencies {
// Espresso core
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2')
{
exclude group: 'com.android.support'
}
// Android JUnit Runner
androidTestCompile('com.android.support.test:runner:0.5')
{
exclude group: 'com.android.support'
}
// JUnit4 Rules
androidTestCompile('com.android.support.test:rules:0.5')
{
exclude group: 'com.android.support'
}
...
Run Code Online (Sandbox Code Playgroud)
}
在HomeFragment
void initFilterButton(View view) {
mFilterButton = (Button) view.findViewById(R.id.button_filter);
Log.e("Tests", "initFilterButton");
mFilterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("Tests", "FilterButton onClick"); // <---- Never shown in logcat
mFilterPopupView.setVisibility(View.VISIBLE);
}
});
}
Run Code Online (Sandbox Code Playgroud)
在我的布局“home_fragment.xml”中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button_filter"
style="@style/FilterAccess"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:clickable="true"
android:text="@string/filter_access" />
<fr.citylity.citylity.v2.ui.views.FilterPopupView
android:id="@+id/filter_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="8dp"
android:visibility="gone" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1356 次 |
| 最近记录: |