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

 

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

问题

在做自动化测试的时候,我们遇到接电话的case。这个问题的难点在于要自动的实现某一个时刻有电话打进来才能起作用。像我上篇文章写的case,是一个接电话的case,那么要想让该case起作用,我就得启动case后,手动拨一个电话给该手机。这不叫自动化,只能称为半自动化。那么有没有办法能让某个手机在莫个时间准确的打个电话过来。

思路1

 用框架去控制2台手机,告诉其中一台手机给另一台手机打个电话,用我们自研的框架可以实现这个功能。但是问题在于现在不能通过调用API来获取手机号啦,那么如果这么做的话,就得每次手动填写手机号。可能对做APP测试的公司也没太大问题,但是如果是测手机的,手机型号就有很多种,那么sim卡也是很多的,要是每次都填写手机号,是不可行的。所以这个方法在研究了一下午后被我放弃了。

思路2

现在获取手机号的方式有2种,一种发短信给10086,但是不是所有运营商的手机都可以。那么打电话算最靠谱了吧。所以我们需要一个机器来接受来电,然后记录下来电号码,暂且称该手机为总机。只要给总机拨个电话,那么总机就能知道手机号了。如果这个时候,再回拨给该手机,也就是实现了在某个时刻准确的打个电话过来。

所以得在总机上安装个apk,用BroadcastReceiver来处理这种需求。

apk

public class PhoneReceiver extends BroadcastReceiver {

         private static final String TAG = "PhoneReceiver";

         private String lastIncomingNumber;

         private Context context;

         private boolean hasCallBack = false;

         @Override

         public void onReceive(Context context, Intent intent) {

                   Log.d(TAG, "接收到广播 : " + intent.getAction());

                   // 如果是去电

                   this.context = context;

                   if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {

                            String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);

                            Log.d(TAG, "call OUT:" + phoneNumber);

                   } else {

                            TelephonyManager tm = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);

                            tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);

                   }

         }

         PhoneStateListener listener = new PhoneStateListener() {

                   @Override

                   public void onCallStateChanged(int state, String incomingNumber) {

                            Log.d(TAG, "电话状态发生改变" + incomingNumber);

                            switch (state) {

                            case TelephonyManager.CALL_STATE_IDLE:

                                     Log.d(TAG, String.format("电话状态变为空闲"));

                                     if (!hasCallBack && lastIncomingNumber != null && lastIncomingNumber.length() != 0) {

                                               hasCallBack = true;

                                               Log.d(TAG, String.format("已挂断和 %s 的通话", lastIncomingNumber));

                                               Intent phoneIntent = new Intent("android.intent.action.CALL", Uri.parse("tel:" + lastIncomingNumber));

                                               phoneIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                                               context.startActivity(phoneIntent);

                                     }

                                     break;

                            case TelephonyManager.CALL_STATE_OFFHOOK:

                                     break;

                            case TelephonyManager.CALL_STATE_RINGING:

                                     Log.d(TAG, String.format("收到%s的来电", incomingNumber));

                                     hasCallBack = false;

                                     lastIncomingNumber = incomingNumber;

                                     break;

                            }

                   }

         };

}

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

问题

在做自动化测试的时候,我们遇到接电话的case。这个问题的难点在于要自动的实现某一个时刻有电话打进来才能起作用。像我上篇文章写的case,是一个接电话的case,那么要想让该case起作用,我就得启动case后,手动拨一个电话给该手机。这不叫自动化,只能称为半自动化。那么有没有办法能让某个手机在莫个时间准确的打个电话过来。

思路1

 用框架去控制2台手机,告诉其中一台手机给另一台手机打个电话,用我们自研的框架可以实现这个功能。但是问题在于现在不能通过调用API来获取手机号啦,那么如果这么做的话,就得每次手动填写手机号。可能对做APP测试的公司也没太大问题,但是如果是测手机的,手机型号就有很多种,那么sim卡也是很多的,要是每次都填写手机号,是不可行的。所以这个方法在研究了一下午后被我放弃了。

思路2

现在获取手机号的方式有2种,一种发短信给10086,但是不是所有运营商的手机都可以。那么打电话算最靠谱了吧。所以我们需要一个机器来接受来电,然后记录下来电号码,暂且称该手机为总机。只要给总机拨个电话,那么总机就能知道手机号了。如果这个时候,再回拨给该手机,也就是实现了在某个时刻准确的打个电话过来。

所以得在总机上安装个apk,用BroadcastReceiver来处理这种需求。

apk

public class PhoneReceiver extends BroadcastReceiver {

         private static final String TAG = "PhoneReceiver";

         private String lastIncomingNumber;

         private Context context;

         private boolean hasCallBack = false;

         @Override

         public void onReceive(Context context, Intent intent) {

                   Log.d(TAG, "接收到广播 : " + intent.getAction());

                   // 如果是去电

                   this.context = context;

                   if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {

                            String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);

                            Log.d(TAG, "call OUT:" + phoneNumber);

                   } else {

                            TelephonyManager tm = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);

                            tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);

                   }

         }

         PhoneStateListener listener = new PhoneStateListener() {

                   @Override

                   public void onCallStateChanged(int state, String incomingNumber) {

                            Log.d(TAG, "电话状态发生改变" + incomingNumber);

                            switch (state) {

                            case TelephonyManager.CALL_STATE_IDLE:

                                     Log.d(TAG, String.format("电话状态变为空闲"));

                                     if (!hasCallBack && lastIncomingNumber != null && lastIncomingNumber.length() != 0) {

                                               hasCallBack = true;

                                               Log.d(TAG, String.format("已挂断和 %s 的通话", lastIncomingNumber));

                                               Intent phoneIntent = new Intent("android.intent.action.CALL", Uri.parse("tel:" + lastIncomingNumber));

                                               phoneIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                                               context.startActivity(phoneIntent);

                                     }

                                     break;

                            case TelephonyManager.CALL_STATE_OFFHOOK:

                                     break;

                            case TelephonyManager.CALL_STATE_RINGING:

                                     Log.d(TAG, String.format("收到%s的来电", incomingNumber));

                                     hasCallBack = false;

                                     lastIncomingNumber = incomingNumber;

                                     break;

                            }

                   }

         };

}