此文已由作者夏鹏授权网易云社区发布。

欢迎访问网易云社区,了解更多网易技术产品运营经验。

使用WebDriver做Web自动化的时候,org.openqa.selenium.support.ui中提供了非常便捷好用的WebDriverWait类继承FluentWait<WebDriver>,所以可以使用FluentWait类中until方法和ExpectedCondition<T>接口进行显示等待的定义,比如某个元素的可见或者可点击等条件,在规定的时间之内等不到指定条件那么就跳出Exception。最近使用appium做客户端的自动化时发现AppiumDriver无法使用WebDriverWait类,是由于WebDriverWait继承于FluentWait<WebDriver>,而WebDriver接口是没有定义findElementByAccessibilityId()、findElementByIosUIAutomation()、findElementByAndroidUIAutomator()的,所以appium想使用像WebDriverWait的显示等待功能,就必须自己封装造轮子了。

①首先依葫芦(WebDriverWait)画瓢写AppiumDriverWait.java

package com.netease.media.qa.base.util;
import org.openqa.selenium.NotFoundException;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.Clock;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Sleeper;
import org.openqa.selenium.support.ui.SystemClock;
import io.appium.java_client.AppiumDriver;
import java.util.concurrent.TimeUnit;
public class AppiumDriverWait extends FluentWait<AppiumDriver> {
  public final static long DEFAULT_SLEEP_TIMEOUT = 500;
  private final AppiumDriver driver;
  public AppiumDriverWait(AppiumDriver driver, long timeOutInSeconds) {
    this(driver, new SystemClock(), Sleeper.SYSTEM_SLEEPER, timeOutInSeconds, DEFAULT_SLEEP_TIMEOUT);
  }
  public AppiumDriverWait(AppiumDriver driver, long timeOutInSeconds, long sleepInMillis) {
    this(driver, new SystemClock(), Sleeper.SYSTEM_SLEEPER, timeOutInSeconds, sleepInMillis);
  }
  public AppiumDriverWait(AppiumDriver driver, Clock clock, Sleeper sleeper, long timeOutInSeconds,
      long sleepTimeOut) {
    super(driver, clock, sleeper);
    withTimeout(timeOutInSeconds, TimeUnit.SECONDS);
    pollingEvery(sleepTimeOut, TimeUnit.MILLISECONDS);
    ignoring(NotFoundException.class);
    this.driver = driver;
  }
@Override
  protected RuntimeException timeoutException(String message, Throwable lastException) {
    TimeoutException ex = new TimeoutException(message, lastException);
    ex.addInfo(WebDriverException.DRIVER_INFO, driver.getClass().getName());
    if (driver instanceof RemoteWebDriver) {
      RemoteWebDriver remote = (RemoteWebDriver) driver;
      if (remote.getSessionId() != null) {
        ex.addInfo(WebDriverException.SESSION_ID, remote.getSessionId().toString());
      }
      if (remote.getCapabilities() != null) {
        ex.addInfo("Capabilities", remote.getCapabilities().toString());
      }
    }
    throw ex;
  }
}

②然后还要需要修改ExpectedCondition接口,将其WebDriver的类型替换为AppiumDriver

package com.netease.media.qa.base.util;
 import com.google.common.base.Function;
 import io.appium.java_client.AppiumDriver;
 public interface ExpectedConditionForAppium<T> extends Function<AppiumDriver, T>{
 }

③接下来就可以在框架封装的方法类中用appium的显示等待啦

 return  new AppiumDriverWait(driver,DriverBase.stepInterval).until(new  ExpectedConditionForAppium<WebElement>(){
  public WebElement apply(AppiumDriver driver){
   WebElement element =  DriverBase.Andriver.findElementByAndroidUIAutomator("new UiSelector().resourceId(\""+locator+"\")");
   return element.isDisplayed() ? element : null;
  }
 });

网易云免费体验馆,0成本体验20+款云产品!

更多网易技术、产品、运营经验分享请点击

相关文章:
【推荐】 MongoDB复制集成员及状态转换
【推荐】 初识Continuation

appium封装显示等待Wait类和ExpectedCondition接口的更多相关文章

  1. 强制等待&隐士等待&显示等待&元素定位方法封装

    前言 问题 学习selenium的同学估计大多数都遇见过一个问题 明明页面已经精准的定位到了元素,但是执行脚本的时候却经常报错没找到元素.其实原因很简单,就是脚本执行的速度很快,而浏览器加载页面的时候 ...

  2. Selenium(七):截图显示等待

    一.显示等待(有条件等待) 常见问题: 定位明明是对的,为什么运行代码没找到定位. 定位明明是对的,找到定位了,文本信息为什么取到是空的? 分析原因: 没有处理frame 页面渲染速度比自动化测试的代 ...

  3. python selenium基于显示等待封装的一些常用方法

    import os import time from PIL import Image from selenium import webdriver from appium import webdri ...

  4. Python3 Selenium自动化web测试 ==> 第十一节 WebDriver高级应用 -- 显示等待 + 二次封装

    学习目的: 掌握显示等待 掌握二次封装 正式步骤: step1:显示等待的代码示例 # -*- coding:utf-8 -*- from selenium import webdriver from ...

  5. Selenium 定位元素原理,基本API,显示等待,隐式等待,重试机制等等

    Selenium  如何定位动态元素: 测试的时候会遇到元素每次变动的情况,例如: <div id="btn-attention_2030295">...</di ...

  6. selenium中的三种等待方式(显示等待WebDriverWait()、隐式等待implicitly()、强制等待sleep())---基于python

    我们在实际使用selenium或者appium时,等待下个等待定位的元素出现,特别是web端加载的过程,都需要用到等待,而等待方式的设置是保证脚本稳定有效运行的一个非常重要的手段,在selenium中 ...

  7. 9.0 toast定位+WebDriverWait显示等待

    Toast  判断-----基本操作问题 首先基本操作,进入安卓市场的账号密码页面--- from appium import webdriver from selenium.webdriver.su ...

  8. python+selenium显示等待、隐式等待和强制等待的区别

    在实际使用selenium或者appium时,等待下个等待定位的元素出现,特别是web端加载的过程,都需要用到等待,而等待方式的设置是保证脚本稳定有效运行的一个非常重要的手段,在selenium中(a ...

  9. UI自动化学习笔记- Selenium元素等待(强制等待、显示等待、隐式等待)

    一.元素等待 1. 元素等待 1.1 什么是元素等待 概念:在定位页面元素时如果未找到,会在指定时间内一直等待的过程 意思就是:等待指定元素已被加载出来之后,我们才去定位该元素,就不会出现定位失败的现 ...

随机推荐

  1. Codeforces Round #296 (Div. 2) D. Clique Problem [ 贪心 ]

    传送门 D. Clique Problem time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  2. hdu - 5074 Hatsune Miku (简单dp)

    有m种不同的句子要组成一首n个句子的歌,每首歌都有一个美丽值,美丽值是由相邻的句子种类决定的,给出m*m的矩阵map[i][j]表示第i种句子和第j种句子的最大得分,一首歌的美丽值是由sum(map[ ...

  3. IP聚合 ---百度之星(与运算)

    Problem Description 当今世界,网络已经无处不在了,小度熊由于犯了错误,当上了度度公司的网络管理员,他手上有大量的 IP列表,小度熊想知道在某个固定的子网掩码下,有多少个网络地址.网 ...

  4. 《springMVC》学习笔记

    1.SpringMVC框架 1.1 MVC在b/s系统下的应用 用户发送request请求到Controller Controller请求模型(Model)进行处理 Model将处理结果返回到Cont ...

  5. Spring实战Day2

    创建对象之后如何体现对象之间的依赖? Spring容器负责创建Bean和依赖注入,那么Spring是怎么将Bean装配在一起的呢? Spring提供了三种方式装配机制 1.隐式的bean发现机制和自动 ...

  6. linux命名详解及其软件安装实例

    始于cd,ls命令 好啦,步入正题,我使用的linux连接工具为xshell,mRemoteNG,对两款工具不做介绍啦,你可以百度一下,实在不会入左上方群. 进入之后,便是上面的界面黑乎乎一片,对于初 ...

  7. 洛谷 P3811 【模板】乘法逆元

    P3811 [模板]乘法逆元 题目背景 这是一道模板题 题目描述 给定n,p求1~n中所有整数在模p意义下的乘法逆元. 输入输出格式 输入格式: 一行n,p 输出格式: n行,第i行表示i在模p意义下 ...

  8. 扫描仪共享工具(BlindScanner Pro) 3.23 特别版

    http://www.xdowns.com/soft/1/126/2014/Soft_125206.html

  9. zookeeper客户端

    查看具体结点信息 bash zkServer.sh status 查看哪个结点被选作leader或者followerecho stat|nc 127.0.0.1 2181 测试是否启动了该Server ...

  10. Fragment实践之聊天窗体

    前几天刚学了android的fragment,总是停留在简单的demo,也许永远都学不会. 今天,我要动手向我的聊天软件开刀.今天.用Fragment来实现一个例如以下图效果的聊天界面. waterm ...