- 得到當前屏幕的寬、高:
driver.manage().window().getSize().getWidth();
driver.manage().window().getSize().getHeight();
- 得到當前元素控件的寬、高:
element.getSize().getWidth();
element.getSize().getHeight();
- 得到當前元素element起始點和終止點的坐標:
element.getLocation().getX()
element.getLocation().getY()
1、向下滑動
滑動的效果是由兩個因素決定的:1、滑動的距離 2、滑動的時間快慢
public void testSwipe() throws InterruptedException { //向下滑動 TouchAction touchAction = new TouchAction(driver); //press:按壓某一個坐標 moveTo:滑動到某一個點 release:手指的釋放 //滑動的起始點坐標 PointOption pointOption1 = PointOption.point(461,619); //滑動的終止點坐標,向下滑動x軸不變 PointOption pointOption2 = PointOption.point(461,1000); //調整滑動的時間 //ofMillis-毫秒,1000毫秒=1秒 Duration duration = Duration.ofMillis(1000); WaitOptions waitOptions = WaitOptions.waitOptions(duration); //waitAction方法需要傳入waitOption類型的參數 touchAction.press(pointOption1).waitAction(waitOptions).moveTo(pointOption2).release().perform(); }
/* * 實現一個更加通用的向下滑動的方法 */ public void swipeDown(){ //由屏幕的寬和高來決定滑動的起始點和終止點 int width = driver.manage().window().getSize().getWidth(); int height = driver.manage().window().getSize().getHeight(); //向下滑動 TouchAction touchAction = new TouchAction(driver); //press:按壓某一個坐標 moveTo:滑動到某一個點 release:手指的釋放 //滑動的起始點坐標 PointOption pointOption1 = PointOption.point(width/2,height/4); //滑動的終止點坐標 PointOption pointOption2 = PointOption.point(width/2,height*3/4); //waitAction方法需要傳入waitOption類型的參數 Duration duration = Duration.ofMillis(1000); WaitOptions waitOptions = WaitOptions.waitOptions(duration); touchAction.press(pointOption1).waitAction(waitOptions).moveTo(pointOption2).release().perform();
向左、向右、向上滑動只要改變起始終止點坐標即可
2、九宮格屏幕手勢解鎖
public void testUnlock() throws InterruptedException { //得到當前九宮格控件的寬和高 WebElement element = driver.findElement(By.id("com.xxzb.fenwoo:id/lpv_password")); int width = element.getSize().getWidth(); int height = element.getSize().getHeight(); //得到當前九宮格控件起始點的坐標x,y int x = element.getLocation().getX(); int y = element.getLocation().getY(); //九宮格解鎖 //第一個點 x+1/6*width y+1/6*height //第二個點 x+3/6*width y+1/6*height //第三個點 x+5/6*width y+1/6*height //第四個點 x+3/6*width y+3/6*height //第五個點 x+1/6*width y+5/6*height //第六個點 x+3/6*width y+5/6*height //第七個點 x+5/6*width y+5/6*height Thread.sleep(3000); PointOption pointOption1 = PointOption.point(x+width/6,y+height/6); PointOption pointOption2 = PointOption.point(x+3*width/6,y+height/6); PointOption pointOption3 = PointOption.point(x+5*width/6,y+height/6); PointOption pointOption4 = PointOption.point(x+3*width/6,y+3*height/6); PointOption pointOption5 = PointOption.point(x+width/6,y+5*height/6); PointOption pointOption6 = PointOption.point(x+3*width/6,y+5*height/6); PointOption pointOption7 = PointOption.point(x+5*width/6,y+5*height/6); //初始化touchAction對象 TouchAction touchAction = new TouchAction(driver); touchAction.press(pointOption1).moveTo(pointOption2).moveTo(pointOption3).moveTo(pointOption4). moveTo(pointOption5).moveTo(pointOption6).moveTo(pointOption7).release().perform(); }
3、多點觸摸:照片、地圖放大縮小
①創建兩個手勢
②MultiTouchAction——讓這兩個手勢動作同時生效
public void testPinch() throws InterruptedException { //手勢放大的動作 Thread.sleep(25000); //得到屏幕的寬度和高度 int width = driver.manage().window().getSize().getWidth(); int height = driver.manage().window().getSize().getHeight(); //第一個手勢動作 從A-->B PointOption pointOptionA = PointOption.point(4*width/10,4*height/10); PointOption pointOptionB = PointOption.point(2*width/10,2*height/10); //第二個手勢操作 從D-->C PointOption pointOptionD = PointOption.point(6*width/10,6*height/10); PointOption pointOptionC = PointOption.point(8*width/10,8*height/10); TouchAction touchAction1 = new TouchAction(driver); TouchAction touchAction2 = new TouchAction(driver); touchAction1.press(pointOptionB).moveTo(pointOptionA).release(); touchAction2.press(pointOptionC).moveTo(pointOptionD).release(); //必須要讓這兩個手勢動作同時生效 -- MultiTouchAction MultiTouchAction multiTouchAction = new MultiTouchAction(driver); multiTouchAction.add(touchAction1); multiTouchAction.add(touchAction2); //讓里面的兩個滑動的動作同時生效 multiTouchAction.perform(); }