自动化测试系列:将常用的Android adb shell 命令行封装为C#静态函数

更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢!

自动化测试系列:将常用的Android adb shell 命令行封装为C#静态函数

简介:adb命令是常用的Android命令行,自动化、代码调试、手工排查问题都会用的到,这里将常用的一些命令行封装成为C#函数,在C#的自动化框架中可以直接调用。

详细内容如下:
 
获取当前app页:
adb -s 设备号 shell \dumpsys activity \| findstr mFocusedActivity
查看设备状态:
adb devices
手机截屏保存到电脑:
adb pull /sdcard/screen.jpg screen.jpg
 
唤醒 安卓 Apk:
public static bool WakeupAndroidApk( string activity)
        {
            string output = GetCmdOutput("adb shell \"am start -n " + activity + "\"");
            return output.Contains("Success" );
        }
 
关闭当前 应用: 
         public static bool closeCurrentActivity( string activity)
        {
            string output = GetCmdOutput("adb -s " + udid + " shell \"pm clear " + activity);
            return output.Contains("Success" );
        }
按屏幕坐标点击:
         public static void Tap( int X, int Y)
        {
            GetCmdOutput( String.Format("adb -s {0} shell input tap {1} {2}" , udid, X, Y));
        }
 
检查当前 应用:
         public static bool CheckCurrentActivity( string activity)
        {
            string output = GetCmdOutput("adb -s " + udid + " shell \"dumpsys activity \"");
            string[] lines = output.Split('\n' );
            string target = "" ;
            Regex reg = new Regex( ".*mFocusedActivity.*");
            foreach(string line in lines)
            {
                if (reg.IsMatch(line))
                {
                    target = line;
                    break;
                }
            }
            return target.Contains(activity);
        }
 
 
        /// <summary>
        /// 打开指定浏览器
        /// </summary>
        /// <param name="browserAndroidApk"> 浏览器</param>
        public static void OpenSpecialBrowser( string browserAndroidApk)
        {
            GetCmdOutput( "adb -s " + udid + " shell am start " + browserAndroidApk);
        }
 
        /// <summary>
        /// 按手机屏幕坐标点击
        /// </summary>
        /// <param name="x"> x坐标</param>
        /// <param name="y"> y坐标</param>
        public static void Tap( int x, int y)
        {
            GetCmdOutput( "adb -s " + udid + " shell input tap " + x + " " + y);
        }
 
        public static void Tab()
        {
            GetCmdOutput( "adb -s " + udid + " shell input keyevent 61" );
        }
 
        /// <summary>
        /// 输入keyevent值对应的字符
        /// </summary>
        /// <param name="key"> keyevent值</param>
        public static void Tab( string key)
        {
            GetCmdOutput( "adb -s " + udid + " shell input keyevent " + "key");
        }
 
        /// <summary>
        /// 输入URL地址
        /// </summary>
        /// <param name="URL"> URL地址</param>
        public static void InputURL( string URL)
        {
            GetCmdOutput( "adb -s " + udid + " shell input text " + URL);
        }
 
        public static void Enter()
        {
            GetCmdOutput( "adb -s " + udid + " shell input keyevent 66" );
        }
 
        /// <summary>
        /// 清理指定的浏览器
        /// </summary>
        /// <param name="browserAndroidApk"> 浏览器</param>
        public static void ClearBrowser( string browserAndroidApk)
        {
            GetCmdOutput( "adb -s " + udid + " shell pm clear " + browserAndroidApk);
        }
 
        /// <summary>
        /// 卸载指定的App
        /// </summary>
        /// <param name="APPAndroidApk"> App</param>
        public static void UninstallApp( string APPAndroidApk)
        {
            GetCmdOutput( "adb -s " + udid + " uninstall " + APPAndroidApk);
        }
 
        /// <summary>
        /// 关闭当前进程Activity
        /// </summary>
        /// <param name="activity"> Activity</param>
        /// <returns> 返回清理结果 </returns>
        public static bool closeCurrentActivity( string activity)
        {
            string output = GetCmdOutput("adb -s " + udid + " shell \"pm clear " + activity);
            return output.Contains("Success" );
        }
 
感谢阅读,作者原创技术文章,转载请注明出处
上一篇:java中不同类型的数值占用字节数


下一篇:将常用的Android adb shell 命令行封装为C#静态函数