我試圖驗證一個應用程序的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
我能想出得到價格的方法有兩種:
遍歷列表視圖的孩子並通過指數獲得價格。如下面的代碼
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的(不知道爲什麼)。此外,這僅適用於當前視圖。我無法向下滾動完整列表並獲取每個產品的詳細信息。
二是,讓所有的
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
這些觀點的問題。另外,當滾動時,這會跳過當前視圖中的一兩個產品,但無法解決這個問題。
如果有人可以對此提出建議,那就太好了。
爲什麼不爲每個ListView設置一個OnItemClickListener? – Houcine
這會有幫助嗎?一旦我點擊列表項目,我會得到一個新的層次。對不起,我對android自動化很陌生。 –
添加代碼,您將適配器實例化並將其設置爲您的列表視圖 – Houcine