自動化測試系列:將經常使用的Android adb shell 命令行封裝爲C#靜態函數

更多原創測試技術文章同步更新到微信公衆號 :三國測,敬請掃碼關注我的的微信號,感謝!shell

 

簡介: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" );
        }
 
感謝閱讀,做者原創技術文章,轉載請註明出處
相關文章
相關標籤/搜索