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 :返回一個整型
0:none 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/"); 把android的tmp目錄拷貝到臨時文件夾
10、屏幕元素點擊
tap(fingers, element, duration):點擊element控件中心點按下,duration*5毫秒秒后松開,如此重復fingers次
11、屏幕坐標點擊
tap(fingers, x, y, duration):點擊x,y坐標按下,duration*5毫秒秒后松開,如此重復fingers次
元素的x,y坐標如何獲取,利用uiautomatorviewer獲取元素的bounds值,然后在范圍內取值,如下示例:
一個button的bounds坐標是[152,344][300,514]
x的取值范圍是152--300,y的取值范圍是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();
}
備注:獲取手機屏幕的大小,然后再實現滑動。