2

我想執行點擊TextView的(這TextView的是一種線性佈局下),但它提供了以下提到的錯誤。嘗試了Espresso.onData,但它引發了一些其他錯誤。那麼如何在這種情況下使用Espresso.onData?咖啡 - 點擊的TextView(按鈕)

android.support.test.espresso.NoMatchingViewException:在體系中沒有的觀點找到匹配:(id爲:註銷:ID/Profile_login_logout並具有父匹配:id爲:註銷:ID/main_layout並顯示在屏幕給用戶)

如果目標視圖不是視圖層次的一部分,您可能需要使用Espresso.onData加載它從以下

AdapterViews之一:android.support.v7.widget .AppCompatSpinner {3ca0fd9d GFED..C。 ......一世。 0,0-0,0#7f10043a應用:ID/language_profile_edit}

ViewInteraction appCompatTextView5 = onView(
allOf(withId(R.id.Profile_login_logout), 
withParent(withId(R.id.main_layout)), 
isDisplayed())); 
appCompatTextView5.perform(scrollTo(), click()); 

下面是XML文件:

<RelativeLayout 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="@color/dull_gray"> 

<ProgressBar 
    android:id="@+id/progress_profile" 
    style="?android:progressDrawable" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:visibility="gone" /> 

<TextView 
    android:id="@+id/text_loading_profile" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/progress_profile" 
    android:layout_centerHorizontal="true" 
    android:text="@string/loading" 
    android:visibility="gone" /> 

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <LinearLayout 
     android:id="@+id/main_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/profile_bg"> 

      <RelativeLayout 
       android:id="@+id/person_image_profile_containner" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 

       <ImageView 
        android:id="@+id/person_image_profile" 
        android:layout_alignParentLeft="true" 
        android:layout_centerVertical="true" 
        android:padding="@dimen/margin20" /> 
      </RelativeLayout> 

      <ProgressBar 
       android:id="@+id/progress_image_profile" 
       style="?android:progressDrawable" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentLeft="true" 
       android:layout_centerVertical="true" 
       android:visibility="gone" /> 

      <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerVertical="true" 
       android:layout_margin="@dimen/topMarginU2" 
       android:layout_toRightOf="@+id/person_image_profile_containner"> 

       <TextView 
        android:id="@+id/person_name_profile" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentTop="true"/> 

       <EditText 
        android:id="@+id/person_name_profile_edit" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentTop="true" 
        android:hint="@string/enter_name" 
        android:visibility="gone" /> 

       <RelativeLayout 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"> 

        <TextView 
         android:id="@+id/mobile_profile_initial" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_marginTop="@dimen/topMargin" 
         android:textSize="@dimen/txtSizeS" 
         tools:ignore="HardcodedText" /> 

        <EditText 
         android:id="@+id/mobile_profile_edit_initial" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:editable="false" 
         android:layout_alignParentBottom="true" 
         android:visibility="gone" 
         tools:ignore="HardcodedText" /> 

       </RelativeLayout> 


       <EditText 
        android:id="@+id/email_profile_edit" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:visibility="gone" /> 
      </LinearLayout> 
     </RelativeLayout> 

     <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:card_view="http://schemas.android.com/apk/res-auto" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:focusable="true"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 

     <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal"> 

        <TextView 
         android:layout_height="wrap_content" 
         android:paddingLeft="@dimen/topMarginU2" 
         android:text="@string/language"/> 

        <Spinner 
         android:id="@+id/language_profile_edit" 
         android:layout_height="wrap_content"/> 
       </LinearLayout> 

       <LinearLayout 
        android:id="@+id/selected_exams" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal"> 

        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="@string/selected_exams"/> 
       </LinearLayout> 

       <RelativeLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"> 

        <android.support.v7.widget.RecyclerView 
         android:id="@+id/horiz_scroll_exams" 
         android:layout_width="wrap_content" 
         android:layout_centerVertical="true" 
         android:scrollbars="horizontal" /> 
       </RelativeLayout> 

       <LinearLayout 
        android:id="@+id/personal_information" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"> 

        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignParentLeft="true" 
         android:padding="@dimen/topMarginU2" 
         android:text="@string/personal_information"/> 
       </LinearLayout> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal"> 

        <TextView 
         android:layout_height="wrap_content" 
         android:layout_alignParentLeft="true" 
         android:text="@string/date_of_birth" 
         android:textSize="@dimen/textSize16" /> 

       </LinearLayout> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal"> 

        <TextView 
         android:layout_width="0dp" 
         android:layout_height="wrap_content" 
         android:text="@string/qualification" 
         android:textSize="@dimen/textSize16" /> 

        <Spinner 
         android:id="@+id/qualification_profile_edit" 
         android:layout_height="wrap_content" 
         android:layout_marginLeft="@dimen/topMargin" 
         android:padding="@dimen/topMarginU2" 
         android:textSize="@dimen/textSize16" 
         android:visibility="gone" /> 
       </LinearLayout> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal"> 

        <TextView 
         android:id="@+id/Personal_address" 
         android:layout_height="wrap_content" 
         android:text="@string/address" 
         android:textSize="@dimen/textSize16" /> 

        <LinearLayout 
         android:layout_height="wrap_content" 
         android:orientation="vertical"> 

         <TextView 
          android:id="@+id/address_basic" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:paddingLeft="@dimen/topMarginU2" 
          tools:ignore="HardcodedText" /> 

         <EditText 
          android:id="@+id/edit_address_basic" 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content" 
          android:layout_marginLeft="@dimen/topMarginU2" 
          android:layout_marginTop="@dimen/topMarginU1" 
          android:hint="@string/enter_address" 
          android:visibility="gone" /> 


         <Spinner 
          android:id="@+id/edit_address_state" 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content" 
          android:layout_marginLeft="@dimen/topMargin" 
          android:textSize="@dimen/txtSizeS" 
          android:visibility="gone" /> 
        </LinearLayout> 

       </LinearLayout> 


       <View 
        android:layout_width="match_parent" 
        android:background="@color/dull_gray" /> 

       <RelativeLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal"> 

        <TextView 
         android:id="@+id/title5" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignParentLeft="true" 
         android:layout_centerVertical="true" 
         android:textSize="@dimen/txtSize" /> 

        <ImageView 
         android:id="@+id/dot1" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_centerInParent="true" 
         android:src="@drawable/dot" /> 

       </RelativeLayout> 



      </LinearLayout> 
     </android.support.v7.widget.CardView> 


     <TextView 
      android:id="@+id/Profile_login_logout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_above="@+id/signup_splash_tv" 
      android:layout_centerHorizontal="true" /> 
    </LinearLayout> 
</ScrollView> 

+0

你是什麼意思,在「線性佈局下」? 「TextView」是「LinearLayout」的子項還是「LinearLayout」後面的「TextView」(使用屏幕上的相同區域)?發佈您的佈局。 – thaussma

+0

TextView是Linear layout的子版本 –

+0

請發佈您的xml –

回答

0

鑑於佈局如何堆積如山的是,我懷疑allOf()匹配器在isDisplayed()失敗。當視圖當前沒有顯示在屏幕上時失敗。

我真的不明白爲什麼你需要的所有三個匹配器唯一標識視圖。 Profile_login_logout似乎是一個唯一的ID,它不需要除了匹配器之外的其他東西。

如果你想,這個觀點顯示你需要先滾動到它的斷言。但scrollTo()將檢查作爲perform()呼叫的一部分滾動後是否顯示視圖。此外click()也有isDisplayingAtLeast(90)限制烘烤。

在你正在處理一個ViewPager情況下(我希望不是這樣),或者類似的東西,你可以看一下視圖通過其父這樣allOf(withId(R.id.Profile_login_logout), hasParent(allOf(withId(R.id.main_layout), isDisplayed()))

onData()不需要爲這個測試,你不要除了spinner以外,沒有任何適配器視圖,它不屬於當前正在測試的部分。

+0

xml文件有點長,這就是爲什麼我刪除了一些行。這是原因,佈局看起來不錯。那麼我想如何將這個解決方案應用於我的代碼呢? ,我對此很陌生。 –

+0

如果我原來的假設是正確的,失敗的原因是視圖不在屏幕上,那麼你的ViewInteraction應該看起來像:'withId(R.id.Profile_login_logout)',並且它應該能夠唯一地識別視圖您發佈的佈局。 –

+0

我曾試過但沒有運氣 –