使用UIautomator获取手机QQ登录验证码

本文介绍如何使用UIautomator实现QQ应用的自动化测试,包括启动应用、模拟账号密码输入及点击登录操作,同时还涉及了验证码的截取与识别过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.启动QQ

public void testDemo() throws UiObjectNotFoundException {
        device = UiDevice.getInstance(getInstrumentation());
        device.pressHome();
        device.waitForWindowUpdate("", 2000);

        startQQ();
        device.waitForWindowUpdate("", 3000);

        startLogin();
        device.waitForWindowUpdate("", 2000);
    }
/**
     * 运行QQ
     */
    public void startQQ(){
        Context context = InstrumentationRegistry.getInstrumentation().getContext();
        //sets the intent to start your app
        Intent intent = context.getPackageManager().getLaunchIntentForPackage("com.tencent.mobileqq");
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        //starts the app
        context.startActivity(intent);
    }

2.使用UIautomator模拟账号密码输入并点击登陆

   //点击登陆按钮,进入登陆界面
        UiObject ui_loginBtn= new UiObject(new UiSelector().resourceId("com.tencent.mobileqq:id/btn_login"));
        try {
            ui_loginBtn.click();
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
        device.waitForWindowUpdate("", 2000);

        //输入账号
        UiObject usernameEt=new UiObject(new UiSelector().className("android.widget.EditText"));
        try {
            usernameEt.click();
            usernameEt.setText("");//填写测试用QQ
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
        device.waitForWindowUpdate("", 2000);

        //输入密码
        UiObject pwEt = new UiObject(new UiSelector().resourceId("com.tencent.mobileqq:id/password"));
        try {
            pwEt.click();
            pwEt.setText("");//填写测试用QQ密码
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
        device.waitForWindowUpdate("", 2000);

        //点击登陆
        UiObject btnLogin = new UiObject(new UiSelector().resourceId("com.tencent.mobileqq:id/login"));
        try {
            btnLogin.click();
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
        device.waitForWindowUpdate("", 2000);

3.获取验证码
在某些情况下登陆QQ会要求填写验证码,如异地登录或者多次输入错误的密码等情况。
主要思路:将验证码控件截取保存为图片,保存到本地文件夹。
3.1 根据类名查找到验证码控件,验证码界面只有一个ImageView。

//UiSelector代表一个搜索UI控件的条件。如果发现多个满足条件的控件则会返回第一个控件。
UiObject ivCode = new UiObject(new UiSelector().className("android.widget.ImageView"));

3.2 先截屏整个界面

 String path = "/sdcard/.system/test.png";
 File file = new File(path);
 device.waitForWindowUpdate("", 2000);
 UiDevice.getInstance().takeScreenshot(file,1.0f,0);

3.3 获取到ImageView坐标,根据坐标截取验证码图片

 Rect rect;
 try {
      rect = ivCode.getBounds();
      device.waitForWindowUpdate("", 2000);
      cutBitmap(rect,path,file);
      } catch (UiObjectNotFoundException e) {
           e.printStackTrace();
      } catch (IOException e) {
           e.printStackTrace();
      }

3.4 截图保存到本地

 public void cutBitmap(Rect rect,String path,File file) throws IOException {

        Bitmap b = BitmapFactory.decodeFile(path);
        b = b.createBitmap(b,rect.left,rect.top,rect.width(),rect.height());

        FileOutputStream out = new FileOutputStream(file);
        b.compress(Bitmap.CompressFormat.PNG, 90, out);
        out.flush();
        out.close();
    }

保存到本地之后,就可以进行验证码的识别神马的。第一次接触到UIautomator这个框架,随便玩玩。

UIAutomator是一个用于Android自动化测试的工具,它提供了一组API来获取设备的UI组件信息。要使用UIAutomator获取手机应用图标,可以按照以下步骤进行: 1. **设置环境**:确保你的开发环境中已经安装了Android SDK,并且配置好了UIAutomator的环境。 2. **编写测试代码**:使用UIAutomator的API编写测试代码来获取应用的图标。 以下是一个示例代码,展示如何使用UIAutomator获取应用图标: ```java import androidx.test.platform.app.InstrumentationRegistry; import androidx.test.uiautomator.UiDevice; import androidx.test.uiautomator.UiSelector; import androidx.test.uiautomator.UiScrollable; import androidx.test.uiautomator.UiObject; import androidx.test.uiautomator.By; import androidx.test.uiautomator.UiAutomator; import androidx.test.uiautomator.UiCollection; public class GetAppIcon { public static void main(String[] args) { // 获取设备实例 UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); // 获取应用包名 String packageName = "com.example.app"; // 查找应用图标 UiSelector selector = new UiSelector().packageName(packageName); UiObject app = device.findObject(selector); try { // 获取应用图标 UiObject icon = app.getChild(new UiSelector().className("android.widget.ImageView")); if (icon.exists()) { // 获取图标路径 String iconPath = icon.getContentDescription(); System.out.println("App icon path: " + iconPath); } else { System.out.println("Icon not found"); } } catch (Exception e) { e.printStackTrace(); } } } ``` 在这个示例中,我们首先获取设备的实例,然后通过包名找到应用对象。接着,我们尝试获取应用图标,并输出图标的路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值