感觉拖了好久,今天将Wait接口的总结补上,顺便把SearchCondition也捎带结了。
直接上图。
Wait方法含义:查看查询条件是否符合,若符合,则返回结果。若不符合,则继续等待,直至超时。
返回结果可能为UIObject2对象或者UIObject2对象列表,或者布尔类型。
其中,传参SearchCondition是一个抽象类,若想获取其实体对象,需要调用Until工具类。
该类提供了4个接口方法,来返回SearchCondition实体对象。
public static SearchCondition findObject(final BySelector selector)
public static SearchCondition> findObjects(final BySelector selector)
public static SearchCondition hasObject(final BySelector selector)
public static SearchCondition gone(final BySelector selector)
这里着重说一下gone方法(前三个方法,从方法名称便可理解用途)。
该方法与findObject功能相反。若不存在符合查询条件的UI对象,则返回true,否则返回false。
还是通过示例来说明如何使用。核心代码如下。
public class WaitTest extends UIDeviceTest {
private String text_button1 ="OPEN ACTIVITY 2";
private String text_button2 ="OPEN ACTIVITY";
private String text_check="check";
private String mPackageName="com.breakloop.test";
private String mActivityName=".MainActivity";
private long timeout=2000l;
@Before
public void start(){
Utils.startAPP(mDevice,mPackageName,mActivityName);
waitForAMoment();
}
private void waitForAMoment(){
mDevice.waitForWindowUpdate(mPackageName,timeout);
}
@After
public void end(){
Utils.closeAPP(mDevice,mPackageName);
waitForAMoment();
}
@Test
public void Test1(){
UiObject2 ui2=null;
List uiObject2List=null;
boolean result=false;
Log.i(TAG, "Test: start");
ui2=mDevice.wait(Until.findObject(By.text(text_button1)),timeout);
Log.i(TAG, "Test1: "+(ui2!=null?"":"Do Not ")+"find UI with txt "+text_button1);
ui2=mDevice.wait(Until.findObject(By.text(text_button2)),timeout);
Log.i(TAG, "Test2: "+(ui2!=null?"":"Do Not ")+"find UI with txt "+text_button2);
result=mDevice.wait(Until.hasObject(By.text(text_button1)),timeout);
Log.i(TAG, "Test3: "+(result?"":"Do Not ")+"find UI with txt "+text_button1);
result=mDevice.wait(Until.hasObject(By.text(text_button2)),timeout);
Log.i(TAG, "Test4: "+(result?"":"Do Not ")+"find UI with txt "+text_button2);
result=mDevice.wait(Until.gone(By.text(text_button1)),timeout);
Log.i(TAG, "Test5: "+(result?"Do Not ":"")+"find UI with txt "+text_button1);
result=mDevice.wait(Until.gone(By.text(text_button2)),timeout);
Log.i(TAG, "Test6: "+(result?"Do Not ":"")+"find UI with txt "+text_button2);
uiObject2List=mDevice.wait(Until.findObjects(By.textStartsWith(text_check)),timeout);
Log.i(TAG, "Test7: "+(uiObject2List!=null?"":"Do Not ")+"find UI with txt "+text_check);
if(uiObject2List!=null){
for (UiObject2 item :
uiObject2List) {
item.click();
waitForAMoment();
}
}
uiObject2List=mDevice.wait(Until.findObjects(By.text(text_check)),timeout);
Log.i(TAG, "Test8: "+(uiObject2List!=null?"":"Do Not ")+"find UI with txt "+text_check);
if(uiObject2List!=null){
for (UiObject2 item :
uiObject2List) {
item.click();
waitForAMoment();
}
}
Log.i(TAG, "Test: end");
}
}
执行效果如图
执行结果如下
15:48:58.700 I/com.breakloop.u2demo.uidevice.WaitTest: Test: start
15:48:58.748 I/com.breakloop.u2demo.uidevice.WaitTest: Test1: find UI with txt OPEN ACTIVITY 2
15:49:00.876 I/com.breakloop.u2demo.uidevice.WaitTest: Test2: Do Not find UI with txt OPEN ACTIVITY
15:49:00.939 I/com.breakloop.u2demo.uidevice.WaitTest: Test3: find UI with txt OPEN ACTIVITY 2
15:49:03.078 I/com.breakloop.u2demo.uidevice.WaitTest: Test4: Do Not find UI with txt OPEN ACTIVITY
15:49:05.209 I/com.breakloop.u2demo.uidevice.WaitTest: Test5: find UI with txt OPEN ACTIVITY 2
15:49:05.249 I/com.breakloop.u2demo.uidevice.WaitTest: Test6: Do Not find UI with txt OPEN ACTIVITY
15:49:05.290 I/com.breakloop.u2demo.uidevice.WaitTest: Test7: find UI with txt check
15:49:11.620 I/com.breakloop.u2demo.uidevice.WaitTest: Test8: Do Not find UI with txt check
15:49:11.620 I/com.breakloop.u2demo.uidevice.WaitTest: Test: end
由执行结果可见:
对于Until.findObject,Until.findObjects,Until.hasObject三个方法,若找到符合查询条件的UI,则Wait结束,否则继续Wait,直至超时。
对于Until.gone方法,则相反。若找到符合查询条件的UI,则继续Wait,直至超时,否则,Wait结束。