2014-02-21 71 views
0

我試圖驗證一個應用程序的TextView,該應用程序顯示搜索類別時列出的產品的price基於ID篩選TextViews

我使用Robotium進行自動化。

當我得到當前視圖層次結構(solo.getCurrentViews(ListView.class))是一樣的東西:

ListView 
--> (0)FrameLayout 
--> (1)RelativeLayout 
     --> (0)ImageView 
     --> (1)RelativeLayout 
       -->(0)ImageView 
       -->(1)TextView //id - name 
       -->(2)TextView //id - price 
       -->(3)TextView //id - other 
--> (2)RelativeLayout 
     --> (0)ImageView 
     --> (1)RelativeLayout 
       -->(0)ImageView 
       -->(1)TextView //id - name 
       -->(2)TextView //id - price 
       -->(3)TextView //id - other 

--> (3)RelativeLayout 
     --> (0)ImageView 
     --> (1)RelativeLayout 
       -->(0)ImageView 
       -->(1)TextView //id - name 
       -->(2)TextView //id - price 
       -->(3)TextView //id - other 

--> (4)RelativeLayout 
     --> (0)ImageView 
     --> (1)RelativeLayout 
       -->(0)ImageView 
       -->(1)TextView //id - name 
       -->(2)TextView //id - price 
       -->(3)TextView //id - other 

--> (5)RelativeLayout 
     --> (0)ImageView 
     --> (1)RelativeLayout 
       -->(0)ImageView 
       -->(1)TextView //id - name 
       -->(2)TextView //id - price 
       -->(3)TextView //id - other 

我能想出得到價格的方法有兩種:

  1. 遍歷列表視圖的孩子並通過指數獲得價格。如下面的代碼

    int index = 1; 
    ListView view = null; 
    
    List<ListView> productList = solo.getCurrentViews(ListView.class); 
    view = productList.get(0); 
    for (int i = 1; i < view.getChildCount(); i++) { 
        RelativeLayout relativeLayoutDetails = (RelativeLayout) view.getChildAt(index++); // get the first RelativeLayout 
    
        RelativeLayout productDetails = (RelativeLayout) relativeLayoutDetails.getChildAt(2); //get the inner child RelativeLayout 
        TextView productName = (TextView) productDetails.getChildAt(0); 
        System.out.println("********" + productDetails.getChildCount()); 
        TextView price = (TextView) productDetails.getChildAt(2); 
        System.out.println(productName.getText().toString() + "---"+ price.getText().toString()); 
    } 
    

這段代碼的問題是TextView price = (TextView) hotelDetails.getChildAt(2);不給我price但給我other TextView的(不知道爲什麼)。此外,這僅適用於當前視圖。我無法向下滾動完整列表並獲取每個產品的詳細信息。

  1. 二是,讓所有的TextView S代表當前看法以及基於price ID過濾。像下面這樣:

    ListView l =(ListView)solo.getView(android.R.id.list); int viewShown = l.getChildCount();

     int total = l.getCount(); 
    
         int i; 
         int index=1; 
         if (hotelsShown != 0) { 
          for (i = 0; i < total/viewShown; i++) { 
    
    
            List<TextView> textViews = solo.getCurrentViews(TextView.class, l); //get all the text views 
            //filter for price 
            for(TextView priceView: textViews){ 
             if(priceView.id equals "price-id") 
               //get this TextView. 
            } 
    
           solo.scrollDown(); 
           solo.sleep(500); 
          } 
    

我用這種方法面臨着是我不知道如何過濾基礎上,ID這些觀點的問題。另外,當滾動時,這會跳過當前視圖中的一兩個產品,但無法解決這個問題。

如果有人可以對此提出建議,那就太好了。

+0

爲什麼不爲每個ListView設置一個OnItemClickListener? – Houcine

+0

這會有幫助嗎?一旦我點擊列表項目,我會得到一個新的層次。對不起,我對android自動化很陌生。 –

+0

添加代碼,您將適配器實例化並將其設置爲您的列表視圖 – Houcine

回答

2

我會參考我以前的答案之一,您可以使用它來查看列表的索引here

一旦你有了這個,你可以用一個新的,得到一個視圖的所有子元素的功能相結合:

private List<View> getAllChildren(View v) { 

    if (!(v instanceof ViewGroup)) { 
     ArrayList<View> viewArrayList = new ArrayList<View>(); 
     viewArrayList.add(v); 
     return viewArrayList; 
    } 

    ArrayList<View> result = new ArrayList<View>(); 

    ViewGroup viewGroup = (ViewGroup) v; 
    for (int i = 0; i < viewGroup.getChildCount(); i++) { 

     View child = viewGroup.getChildAt(i); 

     ArrayList<View> viewArrayList = new ArrayList<View>(); 
     viewArrayList.add(v); 
     viewArrayList.addAll(getAllChildren(child)); 

     result.addAll(viewArrayList); 
    } 
    return result; 
} 

,最後,最後一個是由ID

過濾視圖列表
public View(List<View> views, int idToMatch) { 
    for (View view : views) { 
     if (view.getId() == idToMatch) { 
      return view; 
     } 
    } 
    return null; 
} 

你或許可以想出hwo將這些全部組合成一個功能,它可以很好地完成你想要的功能。 (提示一個R.id.price是一個整數,所以只需要將它傳入最後一個函數,只要你有索引的孩子在你想要的索引)

+0

Paul .....您是我的上帝! –