Uiautomator 2.0之BySelector類學習小記


1. BySelector與By靜態類

1.1 BySelector類為指定搜索條件進行匹配UI元素, 通過UiDevice.findObject(BySelector)方式進行使用。

1.2 By類是一個實用程序類,可以以簡潔的方式創建BySelectors對象。主要功能是使用縮短語法,提供靜態工廠方法來構造BySelectors對象。例如:你將使用findObject(By.text("foo")),而不是findObject(new Selector().text("foo"))的方式來查找文本值為“foo”的UI元素。

1.3 通過閱讀BySelector類和By類的源代碼,可以很清晰的知道兩者的關系。 

BySelector的部分源碼片段:

 1 package android.support.test.uiautomator;
 2 
 3 import java.util.Iterator;
 4 import java.util.LinkedList;
 5 import java.util.List;
 6 
 7 public class BySelector {
 8 
 9     BySelector() {
10     }
11 
12 
13     public BySelector clazz(String className) {
14         checkNotNull(className, "className cannot be null");
15         return className.charAt(0) == 46?this.clazz("android.widget", className.substring(1)):this.clazz(Pattern.compile(Pattern.quote(className)));
16     }

By類的部分源碼片段:

 1 package android.support.test.uiautomator;
 2 
 3 import android.support.test.uiautomator.BySelector;
 4 import java.util.regex.Pattern;
 5 
 6 public class By {
 7     private By() {
 8     }
 9 
10     public static BySelector clazz(String className) {
11         return (new BySelector()).clazz(className);
12     }

2. 搜索層級深度控制

2.1. 相關AP介紹

 

返回值 API 說明
BySelector depth(int depth) 通過設定固定層級,進行匹配查找元素。
BySelector depth (int min, int max) 通過設定層級返回,進行匹配查找元素。
BySelector minDepth(int min) 通過設定最小層級限制,進行匹配查找元素。
BySelector maxDepth (int max) 通過設定最大層級限制,進行匹配查找元素。

 

2.2 簡單示例:

2.2.1 通過設定固定層級,層級范圍,最小層級等方式進行查找元素,e.g.查找如下圖的App Permission鏈接,並進行點擊:

布局文件:

用例代碼:

 

 1 @Test
 2     public void testCase03(){
 3         //通過固定層級,查找元素
 4         UiObject2 ui = mDevice.findObject(By.depth(11));
 5         Log.i("testCase03", ui.getText());
 6         ui.click();
 7 
 8 
 9         //通過設定層級范圍,查找元素
10         UiObject2 ui2 = mDevice.findObject(By.clazz(TextView.class).depth(10, 11));
11         Log.i("testCase03", ui2.getText());
12         ui2.click();
13 
14 
15         //通過設定最小層級限制,查找元素
16         UiObject2 ui3 = mDevice.findObject(By.clazz(TextView.class).minDepth(10));
17         Log.i("testCase03", ui3.getText());
18         ui3.click();
19 
20     }

運行結果:

2.2.2 通過設定最大層級條件,進行查找元素,e.g. 查找如下圖的返回按鈕,並且點擊:

布局文件:

用例代碼:

1    @Test
2     public void testCase03(){
3         //通過設定最大層級限制,查找元素
4         UiObject2 ui4 = mDevice.findObject(By.clazz(ImageButton.class).maxDepth(4));
5         ui4.click();
6 
7     }

2.2.3 補充說明:

By類中只提供了固定層級搜索的方法可直接通過By.depth (int exactDepth)的方式進行調用,而層級范圍搜索,最小層級搜索限制,最大層級搜索限制等方法,只能通過BySelector對象進行調用,實際應用中可通過By.clazz(TextView.class).minDepth(10)的方式進行調用,即先通過By.clazz(TextView.class)的方式獲取到一個BySelector對象,再通過BySelector對象就可以進行調用depth (int min, int max),minDepth(int min),maxDepth (int max)這些方法了。

3. 常規屬性搜索,比如:

3.1 通過文本值或者正則表達式,進行查找定位元素,代碼如下:

 1 @Test
 2     public void testCase04(){
 3         //通過Text值,進行匹配查找
 4         UiObject2 ui = mDevice.findObject(By.text("8"));
 5         ui.click();
 6 
 7         //通過正則表達式,進行匹配查找
 8         Pattern p = Pattern.compile("[8-9]");
 9         UiObject2 ui2 = mDevice.findObject(By.text(p));
10         ui2.click();
11     }

3.2 運行結果:

4. 邏輯屬性搜索

4.1 代碼示例

1 @Test
2     public void testCase05(){
3         UiObject2 ui = mDevice.findObject(By.checked(false));
4         ui.click();
5     }

5. 后代搜索

5.1 API介紹

API 說明
hasChild (BySelector childSelector) 添加一個子類匹配的選擇條件
hasDescendant (BySelector descendantSelector, int maxDepth) 添加一個后代匹配的選擇條件,可設定搜索層級
hasDescendant (BySelector descendantSelector) 添加一個后代匹配的選擇條件

 

5.2 代碼示例

1 @Test
2     public void testCase05(){
3        UiObject2 ui = mDevice.findObject(By.clazz(LinearLayout.class).hasChild(By.text("設置")));
4         ui.click();
5     }

 原創:http://blog.csdn.net/swordgirl2011/article/details/50990584


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM