老李分享:接电话之uiautomator 1

时间:2023-03-08 22:33:11

老李分享:接电话之uiautomator

poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标。如果对课程感兴趣,请大家咨询qq:908821478,咨询电话010-84505200。

1.UiWatcher

该接口中只有一个方法

public abstract boolean checkForCondition ()

当我们通过UiSelector匹配控件的时候,如果失败后,会进入该方法进行验证。意思只要我们的控件找不到,都会进入该方法中,那么我们就可以在该方法中判断是否有电话打进来。首先我们去查看有电话打进来的界面,找到一个可以确定是接听界面就行。我们以拒绝接听按钮为标识(不缺钱的可以用接听电话为标识)。

case的写法

public class InCallTest extends UiAutomatorTestCase {
    private static final long TIME_OUT = 10 * 60 * 1000;
    private long currentTime;
    private boolean flag = true;

long eslcape = 0;

public void test_Call() throws InterruptedException,
            UiObjectNotFoundException {
        UiWatcher inComingWatcher = new MyWatcher();
        getUiDevice().registerWatcher("来电", inComingWatcher);
       
        getUiDevice().pressBack();
        UiObject dialButton = new UiObject(
                new UiSelector()
                        .resourceId("com.android.sprdlauncher1:id/workspace"));
        System.out.println("waiting for incoming");
        currentTime = System.currentTimeMillis();
        while (flag && eslcape < TIME_OUT) {
            dialButton.clickAndWaitForNewWindow();
            eslcape = System.currentTimeMillis() - currentTime;
        }
        assertFalse("没有收到来电", flag);
    }

class MyWatcher implements UiWatcher {

@Override
        public boolean checkForCondition() {
            UiObject inCall = new UiObject(
                    new UiSelector()
                            .resourceId("com.android.dialer:id/IncomingCallRejectButton"));
           
            while (eslcape < TIME_OUT) {
                if (inCall.exists()) {
                    System.out.println("you have a call");
                    try {
                        inCall.clickAndWaitForNewWindow();
                        flag = false;
                        return true;
                    } catch (UiObjectNotFoundException e) {
                        e.printStackTrace();
                    }
                }
                eslcape = System.currentTimeMillis() - currentTime;
            }
            return false;
        }
    }
}