adb基本命令总结(Android Debug Bridge)

adb 是PC和设备连接的桥梁,可以通过adb对devices进行相关操作


  • adb devices           列出你的devices
  • adb kill-server         杀掉adb服务(如果设备连接出问题,可尝试)
  • adb start-server      重启adb服务
  • adb shell                进入默认device的Linux shell,可以直接执行Linux命令


  • adb shell screenrecord /sdcard/runCase.mp4  录制视频保存,默认3min,也可以加--time-limit 60限制时间 
  • adb install jd.apk      向设备安装app
  • adb pull /sdcard/runCase.mp4 c:// 把手机中文件copy到电脑
  • adb push C://runCase.mp4 /sdcard/          把电脑中文件放到手机     

 以上就是一些基本的adb 命令

利用adb命令先切换为自己的输入法,按了搜索再切换为appium的输入法

查看当前手机的输入法

cmd执行下面的的代码

adb shell ime list -s

可以看到类似下面的结果,

C:\Users\LITP>adb shell ime list -s
com.baidu.input_mi/.ImeService
com.sohu.inputmethod.sogou.xiaomi/.SogouIME
io.appium.android.ime/.UnicodeIME

C:\Users\LITP>


执行adb命令

先写好一个执行cmd的方法

/**
* 执行adb命令
* @param s 要执行的命令
*/
private void excuteAdbShell(String s) {
Runtime runtime=Runtime.getRuntime();
try{
runtime.exec(s);
}catch(Exception e){
print("执行命令:"+s+"出错");
}
}


在需要搜索的时候执行下面的代码,切换的输入法用自己查看列表的输入法内容,我这里是搜狗输入法

//使用adb shell 切换输入法-更改为搜狗拼音,这个看你本来用的什么输入法
excuteAdbShell("adb shell ime set com.sohu.inputmethod.sogou.xiaomi/.SogouIME");
//再次点击输入框,调取键盘,软键盘被成功调出
clickView(page.getSearch());
//点击右下角的搜索,即ENTER键
pressKeyCode(AndroidKeyCode.ENTER);
//再次切回 输入法键盘为Appium unicodeKeyboard
excuteAdbShell("adb shell ime set io.appium.android.ime/.UnicodeIME");


 appium实现截图

由于我有webdriver 的基础,理解起来比较easy,appium截图实际是继承webdriver的

selenium 中使用的是TakesScreenShot接口getScreenShotAs方法,具体实现如下

appium实现adb命令 截图和清空EditText_appium实现adb命令

1 /**
2 * This Method create for take screenshot
3 *
4 * @author Young
5 * @param drivername
6 * @param filename
7 */
8 public static void snapshot(TakesScreenshot drivername, String filename) {
9 // this method will take screen shot ,require two parameters ,one is
10 // driver name, another is file name
11
12 String currentPath = System.getProperty("user.dir"); // get current work
13 // folder
14 File scrFile = drivername.getScreenshotAs(OutputType.FILE);
15 // Now you can do whatever you need to do with it, for example copy
16 // somewhere
17 try {
18 System.out.println("save snapshot path is:" + currentPath + "/"
19 + filename);
20 FileUtils
21 .copyFile(scrFile, new File(currentPath + "\\" + filename));
22 } catch (IOException e) {
23 System.out.println("Can't save screenshot");
24 e.printStackTrace();
25 } finally {
26 System.out.println("screen shot finished, it's in " + currentPath
27 + " folder");
28 }
29 }

appium实现adb命令 截图和清空EditText_appium实现adb命令

调用: snapshot((TakesScreenshot) driver, "zhihu_showClose.png");

你可以在捕获异常的时候调用,可以实现错误截图,做测试结果分析很有效


appium清空EditText(一个坑)

在使用appium过程中,发现sendkeys和clear方法并不太好使,只好自己封装模拟手工一个一个删除

要删除一段文字,该怎么做:

1. 获取文本长度

2. 移动到文本最后

3. 按下删除按钮,直到和文本一样长度

移动到文本最后: 123删除67

​public static final int​

​BACKSPACE​

​67​

​public static final int​

​DEL​

​67​


​public static final int​

​KEYCODE_MOVE_END​

​123​

 实现代码如下:


123456789101112

​/**​​​​ ​​​​* This method for delete text in textView​​​​ ​​​​*​​​​ ​​​​* @author Young​​​​ ​​​​* @param text​​​​ ​​​​*/​​​​public​​​​void​​​​clearText(String text) {​​​​    ​​​​driver.sendKeyEvent(​​​​123​​​​);​​​​    ​​​​for​​​​(​​​​int​​​​i = ​​​​0​​​​; i < text.length(); i++) {​​​​        ​​​​driver.sendKeyEvent(​​​​67​​​​);​​​​    ​​​​}​​​​}​



整个case的代码贴一下

初始化driver,执行cmd.exe /C adb shell screenrecord /sdcard/runCase.mp4 开始录制测试视频

执行login

执行修改知乎的个人介绍


123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198

​package​​​​com.dbyl.core;​


​import​​​​org.apache.commons.io.FileUtils;​​​​import​​​​org.openqa.selenium.By;​​​​import​​​​org.openqa.selenium.OutputType;​​​​import​​​​org.openqa.selenium.TakesScreenshot;​​​​import​​​​org.openqa.selenium.WebElement;​​​​import​​​​org.openqa.selenium.remote.CapabilityType;​​​​import​​​​org.openqa.selenium.remote.DesiredCapabilities;​​​​import​​​​org.testng.Assert;​​​​import​​​​org.testng.annotations.AfterClass;​​​​import​​​​org.testng.annotations.BeforeClass;​​​​import​​​​org.testng.annotations.Test;​


​import​​​​io.appium.java_client.android.AndroidDriver;​


​import​​​​java.io.File;​​​​import​​​​java.io.IOException;​​​​import​​​​java.net.URL;​​​​import​​​​java.util.List;​​​​import​​​​java.util.concurrent.TimeUnit;​


​public​​​​class​​​​zhiHu {​​​​    ​​​​private​​​​AndroidDriver driver;​


​    ​​​​/**​​​​     ​​​​* @author Young​​​​     ​​​​* @throws IOException​​​​     ​​​​*/​​​​    ​​​​public​​​​void​​​​startRecord() ​​​​throws​​​​IOException {​​​​        ​​​​Runtime rt = Runtime.getRuntime();​​​​        ​​​​// this code for record the screen of your device​​​​        ​​​​rt.exec(​​​​"cmd.exe /C adb shell screenrecord /sdcard/runCase.mp4"​​​​);​


​    ​​​​}​


​    ​​​​@BeforeClass​​​​(alwaysRun = ​​​​true​​​​)​​​​    ​​​​public​​​​void​​​​setUp() ​​​​throws​​​​Exception {​​​​        ​​​​// set up appium​​​​        ​​​​File classpathRoot = ​​​​new​​​​File(System.getProperty(​​​​"user.dir"​​​​));​​​​        ​​​​File appDir = ​​​​new​​​​File(classpathRoot, ​​​​"apps"​​​​);​​​​        ​​​​File app = ​​​​new​​​​File(appDir, ​​​​"zhihu.apk"​​​​);​​​​        ​​​​DesiredCapabilities capabilities = ​​​​new​​​​DesiredCapabilities();​​​​        ​​​​capabilities.setCapability(CapabilityType.BROWSER_NAME, ​​​​""​​​​);​​​​        ​​​​capabilities.setCapability(​​​​"platformName"​​​​, ​​​​"Android"​​​​);​​​​        ​​​​capabilities.setCapability(​​​​"deviceName"​​​​, ​​​​"Android Emulator"​​​​);​​​​        ​​​​capabilities.setCapability(​​​​"platformVersion"​​​​, ​​​​"4.4"​​​​);​​​​        ​​​​// if no need install don't add this​​​​        ​​​​capabilities.setCapability(​​​​"app"​​​​, app.getAbsolutePath());​​​​        ​​​​capabilities.setCapability(​​​​"appPackage"​​​​, ​​​​"com.zhihu.android"​​​​);​​​​        ​​​​// support Chinese​​​​        ​​​​capabilities.setCapability(​​​​"unicodeKeyboard"​​​​, ​​​​"True"​​​​);​​​​        ​​​​capabilities.setCapability(​​​​"resetKeyboard"​​​​, ​​​​"True"​​​​);​​​​        ​​​​// no need sign​​​​        ​​​​capabilities.setCapability(​​​​"noSign"​​​​, ​​​​"True"​​​​);​​​​        ​​​​capabilities.setCapability(​​​​"appActivity"​​​​, ​​​​".ui.activity.GuideActivity"​​​​);​​​​        ​​​​driver = ​​​​new​​​​AndroidDriver(​​​​new​​​​URL(​​​​"http://127.0.0.1:4723/wd/hub"​​​​),​​​​                ​​​​capabilities);​​​​        ​​​​startRecord();​​​​    ​​​​}​


​    ​​​​@Test​​​​(groups = { ​​​​"login"​​​​})​​​​    ​​​​public​​​​void​​​​login() {​​​​        ​​​​// find login button​​​​        ​​​​WebElement loginButton = driver.findElement(By​​​​                ​​​​.id(​​​​"com.zhihu.android:id/login"​​​​));​​​​        ​​​​loginButton.click();​


​        ​​​​// wait for 20s​​​​        ​​​​driver.manage().timeouts().implicitlyWait(​​​​20​​​​, TimeUnit.SECONDS);​


​        ​​​​// find login userName and password editText​​​​        ​​​​List<WebElement> textFieldsList = driver​​​​                ​​​​.findElementsByClassName(​​​​"android.widget.EditText"​​​​);​​​​        ​​​​textFieldsList.get(​​​​0​​​​).sendKeys(​​​​"seleniumcookies@126.com"​​​​);​​​​        ​​​​textFieldsList.get(​​​​1​​​​).sendKeys(​​​​"cookies123"​​​​);​​​​        ​​​​driver.manage().timeouts().implicitlyWait(​​​​20​​​​, TimeUnit.SECONDS);​


​        ​​​​// find ok button byName​​​​        ​​​​driver.findElementById(​​​​"android:id/button1"​​​​).click();​​​​        ​​​​driver.manage().timeouts().implicitlyWait(​​​​90​​​​, TimeUnit.SECONDS);​


​        ​​​​// find keyword 首页 and verify it is display​​​​        ​​​​Assert.assertTrue(driver.findElement(By.name(​​​​"首页"​​​​)).isDisplayed());​


​    ​​​​}​


​    ​​​​@Test​​​​(groups = { ​​​​"profileSetting"​​​​}, dependsOnMethods = ​​​​"login"​​​​)​​​​    ​​​​public​​​​void​​​​profileSetting() {​


​        ​​​​driver.manage().timeouts().implicitlyWait(​​​​30​​​​, TimeUnit.SECONDS);​​​​        ​​​​// find keyword 首页 and verify it is display​​​​        ​​​​Assert.assertTrue(driver.findElement(By.name(​​​​"首页"​​​​)).isDisplayed());​​​​        ​​​​WebElement myButton = driver.findElement(By​​​​                ​​​​.className(​​​​"android.widget.ImageButton"​​​​));​​​​        ​​​​myButton.click();​​​​        ​​​​driver.manage().timeouts().implicitlyWait(​​​​30​​​​, TimeUnit.SECONDS);​​​​        ​​​​driver.swipe(​​​​700​​​​, ​​​​500​​​​, ​​​​100​​​​, ​​​​500​​​​, ​​​​10​​​​);​​​​        ​​​​driver.manage().timeouts().implicitlyWait(​​​​30​​​​, TimeUnit.SECONDS);​​​​        ​​​​List<WebElement> textViews = driver​​​​                ​​​​.findElementsByClassName(​​​​"android.widget.TextView"​​​​);​​​​        ​​​​textViews.get(​​​​0​​​​).click();​​​​        ​​​​driver.manage().timeouts().implicitlyWait(​​​​30​​​​, TimeUnit.SECONDS);​


​        ​​​​driver.findElementById(​​​​"com.zhihu.android:id/name"​​​​).click();​​​​        ​​​​driver.manage().timeouts().implicitlyWait(​​​​30​​​​, TimeUnit.SECONDS);​


​        ​​​​List<WebElement> showClose = driver​​​​                ​​​​.findElementsById(​​​​"com.zhihu.android:id/showcase_close"​​​​);​​​​        ​​​​if​​​​(!showClose.isEmpty()) {​​​​            ​​​​snapshot((TakesScreenshot) driver, ​​​​"zhihu_showClose.png"​​​​);​​​​            ​​​​showClose.get(​​​​0​​​​).click();​​​​        ​​​​}​​​​        ​​​​Assert.assertTrue(driver​​​​                ​​​​.findElementsByClassName(​​​​"android.widget.TextView"​​​​).get(​​​​0​​​​)​​​​                ​​​​.getText().contains(​​​​"selenium"​​​​));​


​        ​​​​driver.findElementById(​​​​"com.zhihu.android:id/menu_people_edit"​​​​).click();​​​​        ​​​​driver.manage().timeouts().implicitlyWait(​​​​30​​​​, TimeUnit.SECONDS);​​​​        ​​​​WebElement intro = driver​​​​                ​​​​.findElementById(​​​​"com.zhihu.android:id/introduction"​​​​);​​​​        ​​​​intro.click();​​​​        ​​​​driver.manage().timeouts().implicitlyWait(​​​​30​​​​, TimeUnit.SECONDS);​​​​        ​​​​WebElement content = driver​​​​                ​​​​.findElementById(​​​​"com.zhihu.android:id/content"​​​​);​​​​        ​​​​String text = content.getAttribute(​​​​"text"​​​​);​​​​        ​​​​content.click();​​​​        ​​​​clearText(text);​​​​        ​​​​content.sendKeys(​​​​"Appium Test. Create By Young"​​​​);​


​        ​​​​driver.findElementById(​​​​"com.zhihu.android:id/menu_question_done"​​​​)​​​​                ​​​​.click();​


​        ​​​​WebElement explanation = driver​​​​                ​​​​.findElementById(​​​​"com.zhihu.android:id/explanation"​​​​);​​​​        ​​​​explanation.click();​​​​        ​​​​driver.manage().timeouts().implicitlyWait(​​​​30​​​​, TimeUnit.SECONDS);​​​​        ​​​​content = driver.findElementById(​​​​"com.zhihu.android:id/content"​​​​);​​​​        ​​​​text = content.getAttribute(​​​​"text"​​​​);​​​​        ​​​​content.click();​​​​        ​​​​clearText(text);​​​​        ​​​​content.sendKeys(​​​​"Appium Test. Create By Young. This is an appium type hahahahah"​​​​);​


​        ​​​​driver.findElementById(​​​​"com.zhihu.android:id/menu_question_done"​​​​)​​​​                ​​​​.click();​​​​        ​​​​snapshot((TakesScreenshot) driver, ​​​​"zhihu.png"​​​​);​


​    ​​​​}​


​    ​​​​/**​​​​     ​​​​* This method for delete text in textView​​​​     ​​​​*​​​​     ​​​​* @author Young​​​​     ​​​​* @param text​​​​     ​​​​*/​​​​    ​​​​public​​​​void​​​​clearText(String text) {​​​​        ​​​​driver.sendKeyEvent(​​​​123​​​​);​​​​        ​​​​for​​​​(​​​​int​​​​i = ​​​​0​​​​; i < text.length(); i++) {​​​​            ​​​​driver.sendKeyEvent(​​​​67​​​​);​​​​        ​​​​}​​​​    ​​​​}​


​    ​​​​@AfterClass​​​​(alwaysRun = ​​​​true​​​​)​​​​    ​​​​public​​​​void​​​​tearDown() ​​​​throws​​​​Exception {​​​​        ​​​​driver.quit();​​​​    ​​​​}​


​    ​​​​/**​​​​     ​​​​* This Method create for take screenshot​​​​     ​​​​*​​​​     ​​​​* @author Young​​​​     ​​​​* @param drivername​​​​     ​​​​* @param filename​​​​     ​​​​*/​​​​    ​​​​public​​​​static​​​​void​​​​snapshot(TakesScreenshot drivername, String filename) {​​​​        ​​​​// this method will take screen shot ,require two parameters ,one is​​​​        ​​​​// driver name, another is file name​


​        ​​​​String currentPath = System.getProperty(​​​​"user.dir"​​​​); ​​​​// get current work​​​​                                                                ​​​​// folder​​​​        ​​​​File scrFile = drivername.getScreenshotAs(OutputType.FILE);​​​​        ​​​​// Now you can do whatever you need to do with it, for example copy​​​​        ​​​​// somewhere​​​​        ​​​​try​​​​{​​​​            ​​​​System.out.println(​​​​"save snapshot path is:"​​​​+ currentPath + ​​​​"/"​​​​                    ​​​​+ filename);​​​​            ​​​​FileUtils​​​​                    ​​​​.copyFile(scrFile, ​​​​new​​​​File(currentPath + ​​​​"\\"​​​​+ filename));​​​​        ​​​​} ​​​​catch​​​​(IOException e) {​​​​            ​​​​System.out.println(​​​​"Can't save screenshot"​​​​);​​​​            ​​​​e.printStackTrace();​​​​        ​​​​} ​​​​finally​​​​{​​​​            ​​​​System.out.println(​​​​"screen shot finished, it's in "​​​​+ currentPath​​​​                    ​​​​+ ​​​​" folder"​​​​);​​​​        ​​​​}​​​​    ​​​​}​


​}​



 这是运行中的两处截图:

appium实现adb命令 截图和清空EditText_输入法_03

appium实现adb命令 截图和清空EditText_android_04