如何将adb 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" );
}
感谢阅读,作者原创技术文章,转载请注明出处
看完点个赞呗,难道想白嫖不成?更多内容请访问微信公众号 :三国测,扫码关注哟!