首先遇到的问题是app启动后,引导页三张,最后一张有立即体验,思路:
1、app启动
2、启动后进入引导页,抓取引导页元素进行滑动,但是主要是如何抓取引导页的元素,我做的是,直到发现“立即体验”,滑动才会停止。
3、遇到的问题
①定位指定的内容,在当前屏幕内容没有要找的元素,需要向上滑动屏幕才能找到指定元素,困惑了好久用了swipe也不行,因,最后有人指导得到如下方法
//滑动到含有活动标题:0410只签到活动 activityTitle = driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView(text(\"0410只签到活动\"))"); activityTitle.click();
②、我用了new touchation, press, moveto, release ,最后没有realse掉,原来是自己没有理解方法的本质,new touchation的press, moveto, release都是add,必须perform才执行,所以把实现代码mark一下
public void lauch() { // 1. 进入等待页面即开始循环 // 2. 判断是否存在 btnAccess 按钮 // 2.1. Y - 跳出循环 // 2.2. N - 继续下一次循环 // 1. 进入等待页面即开始循环 while (true) { // 1- 等待后,继续循环 try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } // 2. 滑动 this.swipeLeft(); // 3. 判断是否存在 btnAccess 按钮 boolean exists = (null != this.findElementBtnAccess()); // 3.1. Y - 跳出循环 if (exists) break; } AndroidElement showClose = driver.findElement(By.id("com.huitiyu.app:id/btnAccess")); showClose.click(); }
private void swipeLeft() { int screenWidth = driver.manage().window().getSize().width; //获取屏幕最大宽度1080 int screenHeight = driver.manage().window().getSize().height; //获取屏幕最大高度1920 Point fromPoint = new Point(screenWidth - 50, screenHeight / 2); Point toPoint = new Point(10, screenHeight / 2); int offsetX = -screenWidth * 2 / 3; int offsetY = 0; /* System.out.println(String.format("W:%d, H:%d", screenWidth, screenHeight)); System.out.println(String.format("From: p1(%d,%d), To: p2(%d,%d)", fromPoint.getX(), fromPoint.getY(), toPoint.getX(), toPoint.getY()));*/ AndroidElement imageView = new AndroidDriverWait(driver, 3) .until(new AndroidExpectedCondition<AndroidElement>() { @Override public AndroidElement apply(@NullableDecl AndroidDriver androidDriver) { return (AndroidElement) androidDriver.findElement(By.id("com.huitiyu.app:id/imageView")); } }); TouchAction touchAction; touchAction = (new TouchAction(driver)).press(fromPoint.getX(), fromPoint.getY()); touchAction = touchAction.moveTo(offsetX, offsetY); touchAction = touchAction.release(); touchAction = touchAction.waitAction(); touchAction = touchAction.perform(); System.out.println("滑动 " + new Date().getTime()); }