2.App特殊元素操作
一、手势操作-滑动
下拉刷新
//下拉刷新-向下滑动 //按住起始点->滑动到终止点位置上->手指抬起 TouchAction touchAction = new TouchAction(driver); //起始点 PointOption startPoint = PointOption.point(424,632); //终止点 PointOption endPoint = PointOption.point(424,1000); //设置滑动时间-waitOptions类型的参数 WaitOptions waitOptions = WaitOptions.waitOptions(Duration.ofMillis(1000)); touchAction.press(startPoint).waitAction(waitOptions).moveTo(endPoint).release().perform(); /** * 向下滑动的通用封装 * @param swipeTime 滑动时间 */ public static void swipeDown(long swipeTime){ //约定滑动起始点与终止点的位置 //滑动起始点:1/2宽度 1/4高度 滑动终止点:1/2宽度 3/4高度 int screenWidth = driver.manage().window().getSize().getWidth(); int screenHeight = driver.manage().window().getSize().getHeight(); TouchAction touchAction = new TouchAction(driver); //起始点(424,632) PointOption pointOptionA = PointOption.point(screenWidth/2,screenHeight/4); //终止点(424,1000) PointOption pointOptionB = PointOption.point(screenWidth/2,screenHeight*3/4); //设置滑动时间-waitOptions类型的参数 WaitOptions waitOptions = WaitOptions.waitOptions(Duration.ofMillis(swipeTime)); touchAction.press(pointOptionA).waitAction(waitOptions).moveTo(pointOptionB).release().perform(); } /** * 向上滑动的通用封装 * @param swipeTime 滑动时间 */ public static void swipeUp(long swipeTime){ //约定滑动起始点与终止点的位置 //滑动起始点:1/2宽度 1/4高度 滑动终止点:1/2宽度 3/4高度 int screenWidth = driver.manage().window().getSize().getWidth(); int screenHeight = driver.manage().window().getSize().getHeight(); TouchAction touchAction = new TouchAction(driver); //起始点(424,632) PointOption pointOptionA = PointOption.point(screenWidth/2,screenHeight*3/4); //终止点(424,1000) PointOption pointOptionB = PointOption.point(screenWidth/2,screenHeight/4); //设置滑动时间-waitOptions类型的参数 WaitOptions waitOptions = WaitOptions.waitOptions(Duration.ofMillis(swipeTime)); touchAction.press(pointOptionA).waitAction(waitOptions).moveTo(pointOptionB).release().perform(); } /** * 向左滑动的通用封装 * @param swipeTime 滑动时间 */ public static void swipeLeft(long swipeTime){ //约定滑动起始点与终止点的位置 //滑动起始点:1/2宽度 1/4高度 滑动终止点:1/2宽度 3/4高度 int screenWidth = driver.manage().window().getSize().getWidth(); int screenHeight = driver.manage().window().getSize().getHeight(); TouchAction touchAction = new TouchAction(driver); //起始点(424,632) PointOption pointOptionA = PointOption.point(screenWidth*3/4,screenHeight/2); //终止点(424,1000) PointOption pointOptionB = PointOption.point(screenWidth/4,screenHeight/2); //设置滑动时间-waitOptions类型的参数 WaitOptions waitOptions = WaitOptions.waitOptions(Duration.ofMillis(swipeTime)); touchAction.press(pointOptionA).waitAction(waitOptions).moveTo(pointOptionB).release().perform(); } /** * 向右滑动的通用封装 * @param swipeTime 滑动时间 */ public static void swipeRight(long swipeTime){ //约定滑动起始点与终止点的位置 //滑动起始点:1/2宽度 1/4高度 滑动终止点:1/2宽度 3/4高度 int screenWidth = driver.manage().window().getSize().getWidth(); int screenHeight = driver.manage().window().getSize().getHeight(); TouchAction touchAction = new TouchAction(driver); //起始点(424,632) PointOption pointOptionA = PointOption.point(screenWidth/4,screenHeight/2); //终止点(424,1000) PointOption pointOptionB = PointOption.point(screenWidth*3/4,screenHeight/2); //设置滑动时间-waitOptions类型的参数 WaitOptions waitOptions = WaitOptions.waitOptions(Duration.ofMillis(swipeTime)); touchAction.press(pointOptionA).waitAction(waitOptions).moveTo(pointOptionB).release().perform(); } //场景:滑动到页面底部 driver.findElement(By.id("com.lemon.lemonban:id/navigation_tiku")).click(); driver.findElement(By.id("com.lemon.lemonban:id/button_go_login")).click(); driver.findElement(By.id("com.lemon.lemonban:id/et_mobile")).sendKeys("13323234545"); driver.findElement(By.id("com.lemon.lemonban:id/et_password")).sendKeys("234545"); driver.findElement(By.id("com.lemon.lemonban:id/btn_login")).click(); driver.findElement(MobileBy.AndroidUIAutomator("new UiSelector().text(\"软件测试基础\")")).click(); driver.findElement(By.id("com.lemon.lemonban:id/first_level")).click(); Thread.sleep(3000); //循环滑动 while(true){ //滑动前的页面源代码 String beforeSource = driver.getPageSource(); swipeUp(1000); Thread.sleep(1000); //什么时候跳出循环呢? //1、等待【全部加载完成】出来之后? 缺点:(1)必须要保证此元素要立马出来 (2)有些页面没有这个提示 //通过getPageSource进行判断 /*if(driver.getPageSource().contains("全部加载完成")){ break; }*/ //2、getPageSource,通过滑动前后页面的pageSource是不是发生变化 //滑动之后的页面源代码 String afterSource = driver.getPageSource(); if(afterSource.equals(beforeSource)){ //滑动到了页面的底部 break; } }
二、特殊场景元素定位
1.toast元素定位
Android中的Toast是一种简易的消息提示框,当视图显示给用户,在应用程序中显示为浮动和Dialog不一样的是,它永远不会获得焦点,无法被点击
//获取toast元素 文本:手机号码或密码不能为空 //xpath表达式获取 //WebElement webElement = driver.findElement(By.xpath("//*[@text='手机号码或密码不能为空']")); //System.out.println(webElement.getText()); //如果时通过显示等待获取toast元素的时候,那么条件只能够是:等待元素存在presenceOfElementLocated WebDriverWait wait = new WebDriverWait(driver,8); WebElement webElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@text='手机号码或密码不能为空']"))); System.out.println(webElement.getText()); driver.quit(); //Original error: Could not proxy command to remote server. Original error: Error: read ECONNRESET //此错误提示原因:之前的Appium会话没有完全的关闭
2.不定时弹窗
App运行,页面不定时会有弹窗出来,例如评价弹窗、升级弹窗、广告弹窗、提示弹窗等
解决方案:try…catch…异常捕获,正常元素定位超时进入到异常处理机制,在其内部处理掉弹窗再继续后续的流程
//实例化配置对象 DesiredCapabilities desiredCapabilities = new DesiredCapabilities(); //1、测试哪一台设备 desiredCapabilities.setCapability("deviceName","127.0.0.1:62001"); //2、指定测试平台Android or IOS desiredCapabilities.setCapability("platformName","Android"); //3、指定测试App(包名-App唯一身份标识) desiredCapabilities.setCapability("appPackage","tv.danmaku.bili"); //4、App启动配置(App启动入口) desiredCapabilities.setCapability("appActivity","tv.danmaku.bili.ui.splash.SplashActivity"); //noReset配置设置为true:不清除掉App数据 desiredCapabilities.setCapability("noReset",true); URL url = new URL("http://127.0.0.1:4723/wd/hub"); //把配置发送给到Appium服务器(Driver实例化) driver = new AndroidDriver(url,desiredCapabilities); driver.manage().timeouts().implicitlyWait(8, TimeUnit.SECONDS); //点击【登录按钮】 try { driver.findElement(By.id("tv.danmaku.bili:id/drawer_handler")).click(); }catch (Exception e){ //找到对应的元素就进入到弹窗的处理机制中来 //找到弹窗 driver.findElement(MobileBy.AndroidUIAutomator("new UiSelector().text(\"我知道了\")")).click(); Thread.sleep(3000); //点完弹窗之后继续再来点击对应的元素 driver.findElement(By.id("tv.danmaku.bili:id/drawer_handler")).click(); }