appium实现截图和清空EditText
前些日子,配置好了appium测试环境,至于环境怎么搭建,参考:http://www.cnblogs.com/tobecrazy/p/4562199.html
知乎Android客户端登陆:http://www.cnblogs.com/tobecrazy/p/4579631.html
在使用appium的过程中,发现一些appium的坑(后边会详说)。
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 命令
appium实现截图
由于我有webdriver 的基础,理解起来比较easy,appium截图实际是继承webdriver的
selenium 中使用的是TakesScreenShot接口getScreenShotAs方法,具体实现如下
/**
* 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");
}
}
调用: 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 |
实现代码如下:
/**
* 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
执行修改知乎的个人介绍
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实现截图和清空EditText的更多相关文章
- appium实现adb命令 截图和清空EditText
原文地址http://www.cnblogs.com/tobecrazy/p/4592405.html 原文地址http://www.cnblogs.com/tobecrazy/ 该博主有很多干货,可 ...
- Appium清空EditText
在使用appium过程中,发现sendkeys和clear方法并不太好使,封装模拟手工一个一个删除 这里用到keyEvent,具体内容请参考api http://appium.github.io/ja ...
- appium的截图
在实际自动化项目运行过程中,很多时候App可以会出现各种异常,为了更好的定位问题,除了捕捉日志我们还需要对运行时的设备状态来进行截屏.从而达到一种“有图有真相”的效果. 截图方法 方法1 save_s ...
- Appium + java截图方法
public static void takeScreenShot(AndroidDriver<WebElement> driver) { File screenShotFile = dr ...
- appium 滑动
前些日子,配置好了appium测试环境,至于环境怎么搭建,参考:http://www.cnblogs.com/tobecrazy/p/4562199.html 知乎Android客户端登陆:htt ...
- Appium 三种wait方法(appium 学习之改造轮子)
前些日子,配置好了appium测试环境,至于环境怎么搭建,参考:http://www.cnblogs.com/tobecrazy/p/4562199.html 知乎Android客户端登陆:htt ...
- Appium for iOS setup
windows下appium设置 之前研究了一段时间的appium for native app 相应的总结如下: ...
- appium for hybrid app 处理webview
之前研究了一段时间的appium for native app 相应的总结如下: appium测试环境搭建 :ht ...
- appium 九宫格解锁招商银行手机客户端app
之前研究了一段时间的appium for native app 相应的总结如下: appium测试环境搭建 :ht ...
随机推荐
- C#中两个Form窗口之间的传值(父->子)(子->父)
//首先定义两个Form,一个为Form1,一个为Form2,其中Form1作为父窗口,Form2作为子窗口 //1.父窗口传值给子窗口 //Form1中代码: public Form1() { In ...
- IO多路复用概念性
sellect.poll.epoll三者的区别 先来了解一下什么是进程切换 为了控制进程的执行,内核必须有能力挂起正在CPU上运行的进程,并恢复以前挂起的某个进程的执行,这种行为为进程的切换,任务切换 ...
- php代码小实例
php多图上传 <html> <meta http-equiv="Content-Type" content="text/html; charset=u ...
- <form:select>的使用
最近在学习springMVC,用到了<form:select>标签,使用发过程中遇到了些问题,现在记录下,以防忘记. 我jsp页面是这样的: <%@ page language=&q ...
- python三大类型数据筛选
如何在列表,字典,集合中根据条件刷选数据 说明: 本文分析的类型: 列表 字典 集合 结合每种类型筛选数据的方法的不同,区分出方法间的差异. 一.列表案例 需求:过滤掉列表中的负数. li = [1, ...
- vmware Centos6.6安装64位
Centos6.6安装64位 必须开启BIOS中的虚拟化技术 首先开机进入BIOS,一般机器是按F2,我的T420是按F1,然后进入Security,Virtualization,选择Enable即可 ...
- nginx服务器http重定向到https的正确写法
http重定向到https使用了nginx的重定向命令.那么应该如何写重定向?之前老版本的nginx可能使用了以下类似的格式. rewrite ^/(.*)$ http://domain.com/$1 ...
- loss function
什么是loss? loss: loss是我们用来对模型满意程度的指标.loss设计的原则是:模型越好loss越低,模型越差loss越高,但也有过拟合的情况. loss function: 在分 ...
- oracle 序列中cache 有什么用途
create sequence name increment by x //x为增长间隔 start with x //x为初始值 maxvalue x //x为最大值 minvalue x //x为 ...
- SCI英文论文写作- Latex 进阶
SCI英文论文写作- Latex 进阶 1.设置行间距的方法: %\setlength{\baselineskip}{15pt} \renewcommand{\baselinestretch}{1 ...