22
2025
04
10:54:47

UI Automator安卓测试,按键模拟

UI Automator 是一个界面测试框架,适用于整个系统上以及多个已安装应用间的跨应用功能界面测试。类似按键精灵的功能。

android.google.cn%2Ftraining%2Ftesting%2Fui-automator%3Fhl%3Dzh-cn" rel="nofollow noopener noreferrer" target="_blank" data-report-click="{"spm":"3001.9009.1","dest":"https://link.csdn.net/?target=https%3A%2F%2Fdeveloper.android.google.cn%2Ftraining%2Ftesting%2Fui-automator%3Fhl%3Dzh-cn"}" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); outline: none; box-sizing: border-box; background: transparent; color: rgb(10, 108, 255); cursor: pointer; transition: color 0.2s ease 0s; margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; text-decoration-line: none; backface-visibility: hidden;">官方文档

官方示例

1、导入依赖包

androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'

2、打开uiautomatorviewer可视界面

双击SDK\tools\bin目录下uiautomatorviewer.bat

3、测试代码

模拟按下home键,测试代码

package com.app.auto;import androidx.test.platform.app.InstrumentationRegistry;import androidx.test.uiautomator.UiDevice;import org.junit.Test;public class SwipeTest {    @Test
    public void pressHome() throws Exception {        //初始化UiDevice实例
        UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());        // 模拟按键home
        device.pressHome();        //屏幕宽高
        // int h = device.getDisplayHeight();
        // int w = device.getDisplayWidth();
        // 向上滑动屏幕
        // for (int i = 0; i < 5; i++) {
        //     device.swipe(w / 2, h / 2 + 250, w / 2, h / 2 - 250, 5);
        //     Thread.sleep(5000);
        // }
        System.out.println();        //启动QQ
        //当前屏幕有QQ启动图标
        // UiObject qq=device.findObject(new UiSelector().text("QQ"));
        // qq.click();
    }
}

连上真机或者模拟器,运行测试代码。

成功运行后,手机界面会回到home界面。

控制台有一段运行日志(启动测试的adb命令),记录下来后面会用到:

$ adb shell am instrument -w -m    -e debug false -e class 'com.app.auto.SwipeTest' com.app.auto.test/androidx.test.runner.AndroidJUnitRunner

4、界面

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="150dp"
        android:text="Button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


5、Activity

package com.app.auto;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import java.io.BufferedReader;import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;public class MainActivity extends AppCompatActivity {    @Override
    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);        //初始化按钮
        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {            @Override
            public void onClick(View view) {                new Thread(new Runnable() {                    @Override
                    public void run() {                        // adb命令
                        //$ adb shell am instrument -w -m    -e debug false -e class 'com.app.auto.SwipeTest#pressHome' com.app.auto.test/androidx.test.runner.AndroidJUnitRunner

                        //使用Java执行命令
                        execCommand("am instrument -w -m    -e debug false -e " +                                //测试类名和方法名
                                "class com.app.auto.SwipeTest#pressHome " +                                //AndroidJUnitRunner
                                "com.app.auto.test/androidx.test.runner.AndroidJUnitRunner");
                    }
                }).start();
            }
        });
    }    //执行命令
    public void execCommand(String command) {        Process process = null;        OutputStream os = null;        try {            //使用Java的Runtime执行命令
            // process = Runtime.getRuntime().exec("sh"); //普通用户权限
            process = Runtime.getRuntime().exec("su"); //root权限
            os = process.getOutputStream();
            os.write(command.getBytes());
            os.write("\n".getBytes());
            os.flush();            int result = process.waitFor();//等待外部命令执行完毕,然后返回执行的结果,0表示正常
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {            try {                if (os != null) {
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }            if (process != null) {
                process.destroy();
            }
        }
    }
}

最初的想法打包成app发给别人使用,测试后发现需要root权限,

很遗憾需要root权限,瞬间没兴趣研究了。




推荐本站淘宝优惠价购买喜欢的宝贝:

image.png

本文链接:https://hqyman.cn/post/10674.html 非本站原创文章欢迎转载,原创文章需保留本站地址!

分享到:
打赏





休息一下~~


« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

请先 登录 再评论,若不是会员请先 注册

您的IP地址是: