首页 > 技术文章 > appium手机操作

sunny-sl 2017-03-18 16:07 原文

1、按键操作

pressKeyCode(key, metastate)

key为按键事件,metastate为辅助功能键

举例:

pressKeyCode(AndroidKeyCode.HOME)                 HOME

pressKeyCode(AndroidKeyCode.KEYCODE_A, 1)  A

 

2、锁屏操作

lockScreen(seconds):屏幕几秒后锁屏

isLocked():判断屏幕是否锁屏,返回布尔类型,锁屏为true

 

3、打开通知栏

openNotifications()

 

4、屏幕滚动

scrollTo(text):滚动到某个Text属性为指定的字符串的控件

scrollToExact(text):滚动到某个Text属性包含传入的字符串的控件

 

5、获取网络状态

getNetworkConnection().value :返回一个整型

0none  1:Airplane Mode  2:Wifi only  4:Data only  6:All network on

 

6、设置网络状态

setNetworkConnection(connection)

举例:

setNetworkConnection(new NetworkConnectionSetting(1));//飞行模式

setNetworkConnection(new NetworkConnectionSetting(true, false, false))//飞行模式

 

7、截取屏幕

getScreenshotAs(outputType)

举例:

File screen = driver.getScreenshotAs(OutputType.FILE);

File screenFile = new File("d:\\screen.png");

try {

FileUtils.copyFile(screen, screenFile); //commons-io-2.0.1.jar中的api

} catch (IOException e) {

e.printStackTrace();

}

 

8、横竖屏设置

rotate(orientation):设置屏幕横屏或者竖屏   LANDSCAPE (横向) PORTRAIT (纵向)

getOrientation():获取当前屏幕的方向

举例:

driver.rotate(ScreenOrientation.LANDSCAPE);  设置屏幕为横屏

 

9、上传/下载文件

pullFile(remotePath):上传文件

driver.pullFile(remotePath):下载文件

pullFolder(remotePath):下载文件夹

上传文件举例:

File file = new File("c:\\appium.log");

String content = null;

try {

content = FileUtils.readFileToString(file);

} catch (IOException e) {

e.printStackTrace();

}

byte[] data = Base64.encodeBase64(content.getBytes());

driver.pushFile("sdcard/appium.log", data);

下载文件举例:

byte[] resultDate = driver.pullFile("sdcard/appium.log");

System.out.println(new String(Base64.decodeBase64(resultDate)));

 

下载文件夹举例:

driver.pullFolder("tmp/");  androidtmp目录拷贝到临时文件夹

 

10、屏幕元素点击

tap(fingers, element, duration):点击element控件中心点按下,duration*5毫秒秒后松开,如此重复fingers

 

11、屏幕坐标点击

tap(fingers, x, y, duration):点击xy坐标按下,duration*5毫秒秒后松开,如此重复fingers

元素的x,y坐标如何获取,利用uiautomatorviewer获取元素的bounds值,然后在范围内取值,如下示例:

一个buttonbounds坐标是[152,344][300,514]

x的取值范围是152--300y的取值范围是344--514

 

12、根据坐标滑动

swipe(startx, starty, endx, endy, duration):从(startx,starty)滑到(endx,endy),分duration步滑,每一步用时是5毫秒。

坐标获取方式:

a)手机--开发者选项--指针位置  b)hierarchyviewer  c)uiautomatorviewer

举例:

File screen = driver.getScreenshotAs(OutputType.FILE);

try {

BufferedImage bufferedImage = ImageIO.read(screen);

int width = bufferedImage.getWidth();

int height = bufferedImage.getHeight();

System.out.println("width:"+width+"height:"+height);

driver.swipe(width/2,height*3/4, width/2,height/4, 1000);

} catch (IOException e) {

e.printStackTrace();

}

备注:获取手机屏幕的大小,然后再实现滑动。

推荐阅读