原文地址http://www.cnblogs.com/tobecrazy/p/4592405.html

原文地址http://www.cnblogs.com/tobecrazy/

该博主有很多干货,可以多去研究研究

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方法,具体实现如下

 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 }

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

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


appium清空EditText(一个坑)

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

这里用到keyEvent,具体内容请参考api http://appium.github.io/java-client/

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

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

实现代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
/**
 * 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

执行修改知乎的个人介绍

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
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(70050010050010);
        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的更多相关文章

  1. appium实现截图和清空EditText

    前些日子,配置好了appium测试环境,至于环境怎么搭建,参考:http://www.cnblogs.com/tobecrazy/p/4562199.html   知乎Android客户端登陆:htt ...

  2. Appium(三):安装appium client、adb命令

    1. 安装appium client appium client是对webdriver原生api的一些扩展和封装.它可以帮助我们更容易的写出用例,写出更好的用例. appium client是配合原生 ...

  3. Appium自动化测试教程-自学网-adb命令

    adb命令: adb ( Android Debug Bridge)是一个通用命令行工具,其允许您与模拟器实例或连接的 Android 设备进行通信.它可为各种设备操作提供便利,如安装和调试应用. T ...

  4. Appium+python自动化(十六)- ADB命令,知否知否,应是必知必会(超详解)

    简介 Android 调试桥(adb)是多种用途的工具,该工具可以帮助你你管理设备或模拟器 的状态. adb ( Android Debug Bridge)是一个通用命令行工具,其允许您与模拟器实例或 ...

  5. app自动化appium使用内置adb命令

    一.Appium-server使用 1.登陆页面 高级设置:可以设置Android 和 IOS 日志级别:dabug非常详尽的日志 记录python代码向他发送的请求以及他在收到请求后做的一系列处理 ...

  6. 『与善仁』Appium基础 — 5、常用ADB命令(二)

    目录 9.查看手机运行日志 (1)Android 日志 (2)按级别过滤日志 (3)按 tag 和级别过滤日志 (4)日志格式 (5)清空日志 10.获取APP的包名和启动名 方式一: 方式二: 11 ...

  7. 『与善仁』Appium基础 — 3、ADB命令介绍

    目录 1.ADB命令简介 2.ADB命令运行原理 3.通过ADB命令连接安卓模拟器 (1)安装安卓模拟器 (2)ADB命令连接安卓模拟器 (3)常用Android模拟器端口号 1.ADB命令简介 AD ...

  8. 『与善仁』Appium基础 — 4、常用ADB命令(一)

    目录 1.启动和关闭ADB服务 2.查看ADB版本 3.指定adb server的网络端口 4.查询已连接设备/模拟器 5.获取安卓系统版本 6.为命令指定目标设备 7.发送文件到手机 8.从手机拉取 ...

  9. [整理]ADB命令行学习笔记

    global driver# 元素定位driver.find_element_by_id("id") # id定位driver.find_element_by_name(" ...

随机推荐

  1. 分析并实现 360 P1路由器上的朋友专享网络 功能

    笔者分析了360 P1路由器上的朋友专享网络功能,发现其主要由如下子功能组成: 1. APP点击“立即开启”,则路由器会多出一个新的SSID:360朋友专享网络-8463.此SSID不加密:同时,原有 ...

  2. TCP/IP各层协议数据格式

    ISO规范里定义了7层网络模型,实际常用的仍为TCPIP四层网络模型. 注:本文章插图均来自<图解TCP/IP>. 数据链路层帧格式 经常说的帧格式为以太网帧格式,由于类型和帧长度字段不重 ...

  3. java 获取用户真实ip

    /** * 获取用户真实ip * @param request * @return */ public static String getIpAddr(HttpServletRequest reque ...

  4. jQuery中bind函数绑定多个事件

    名人名言:道德是真理之花.——雨果 在jQuery中绑定多个事件名称是,使用空格隔开,举例如下: $("#foo").bind("mouseenter mouseleav ...

  5. 获取一个Assembly中的命名空间列表

    通过System.Reflection.Assembly类中提供的方法和属性不能直接获取组件中的命名空间列表.但有方法可以直接获得Assembly中的所有类型,我们便可以通过获取的类型来得到命名空间名 ...

  6. java web学习笔记-Servlet篇

    Servlet基础 1.Servlet概述 JSP的前身就是Servlet.Servlet就是在服务器端运行的一段小程序.一个Servlet就是一个Java类,并且可以通过“请求-响应”编程模型来访问 ...

  7. CH Round #54 - Streaming #5 (NOIP模拟赛Day1)(被虐瞎)

    http://ch.ezoj.tk/contest/CH%20Round%20%2354%20-%20Streaming%20%235%20%28NOIP%E6%A8%A1%E6%8B%9F%E8%B ...

  8. 使用Using的注意事项

    参数传递 C#中有四种参数类型:值类型,Ref参数,Out参数,params参数.默认参数都是以传值方式传递,这意味着方法中的变量会在内存中被分配新的存储空间,并赋值.对于引用类型,这种传值意味着传递 ...

  9. android手机有多个摄像头,打开其中一个

    方法: private Camera openFrontFacingCameraGingerbread() { int cameraCount = 0; Camera cam = null; Came ...

  10. 转(解决GLIBC_2.x找不到的编译问题)

    Linux/CentOS 升级C基本运行库CLIBC的注意事项(当想解决GLIBC_2.x找不到的编译问题) 分类: 开发环境 Linux2014-09-24 10:32 8933人阅读 评论(5)  ...