前些日子,配置好了appium测试环境,至于环境怎么搭建,参考:http://www.cnblogs.com/tobecrazy/p/4562199.html

                    知乎Android客户端登陆:http://www.cnblogs.com/tobecrazy/p/4579631.html

appium实现截图和清空EditText:  http://www.cnblogs.com/tobecrazy/p/4592405.html

学过selenium的都知道,一般等待元素加载有三种办法:

  • sleep                  Thread.sleep(60000) 强制等待60s
  • implicitlyWait      
     driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);  

    隐式等待,全局等待30s不管元素是否已经加载

  • WebDriverWait     显示等待,这个需要增加一定等待时间,显示等待时间可以通过WebDriverWait 和util来决定,比如这个timeOut是60,如果该元素60s以内出现就不在等待
WebDriverWait wait = new WebDriverWait(driver, 60);
WebElement e= wait.until(new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(WebDriver d) {
return d.findElement(By.id("q"));
}
})

以上三种方法中,只用WebDriverWait是selenium所特有,在java-client中也找不到相应,如果想使用这种方法怎么办?

改造轮子,首先添加AndroidDriverWait.java, 其实是将WebDriverWait的类型改成AndroidDriverWait

具体代码如下:

  

 package com.dbyl.core;

 /*
Copyright 2007-2009 Selenium committers Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/ import org.openqa.selenium.NotFoundException;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
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.android.AndroidDriver; import java.util.concurrent.TimeUnit; /**
* A specialization of {@link FluentWait} that uses WebDriver instances.
*/
public class AndroidDriverWait extends FluentWait<AndroidDriver> {
public final static long DEFAULT_SLEEP_TIMEOUT = 500;
private final WebDriver driver; /**
* Wait will ignore instances of NotFoundException that are encountered (thrown) by default in
* the 'until' condition, and immediately propagate all others. You can add more to the ignore
* list by calling ignoring(exceptions to add).
*
* @param driver The WebDriver instance to pass to the expected conditions
* @param timeOutInSeconds The timeout in seconds when an expectation is called
* @see AndroidDriverWait#ignoring(java.lang.Class)
*/
public AndroidDriverWait(AndroidDriver driver, long timeOutInSeconds) {
this(driver, new SystemClock(), Sleeper.SYSTEM_SLEEPER, timeOutInSeconds, DEFAULT_SLEEP_TIMEOUT);
} /**
* Wait will ignore instances of NotFoundException that are encountered (thrown) by default in
* the 'until' condition, and immediately propagate all others. You can add more to the ignore
* list by calling ignoring(exceptions to add).
*
* @param driver The WebDriver instance to pass to the expected conditions
* @param timeOutInSeconds The timeout in seconds when an expectation is called
* @param sleepInMillis The duration in milliseconds to sleep between polls.
* @see AndroidDriverWait#ignoring(java.lang.Class)
*/
public AndroidDriverWait(AndroidDriver driver, long timeOutInSeconds, long sleepInMillis) {
this(driver, new SystemClock(), Sleeper.SYSTEM_SLEEPER, timeOutInSeconds, sleepInMillis);
} /**
* @param driver The WebDriver instance to pass to the expected conditions
* @param clock The clock to use when measuring the timeout
* @param sleeper Object used to make the current thread go to sleep.
* @param timeOutInSeconds The timeout in seconds when an expectation is
* @param sleepTimeOut The timeout used whilst sleeping. Defaults to 500ms called.
*/
public AndroidDriverWait(AndroidDriver 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的类型替换为AndroidDriver

具体代码:

 /*
Copyright 2007-2009 Selenium committers Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/ package com.dbyl.core; import io.appium.java_client.android.AndroidDriver; import com.google.common.base.Function; /**
* Models a condition that might reasonably be expected to eventually evaluate to something that is
* neither null nor false. Examples would include determining if a web page has loaded or that an
* element is visible.
* <p>
* Note that it is expected that ExpectedConditions are idempotent. They will be called in a loop by
* the {@link WebDriverWait} and any modification of the state of the application under test may
* have unexpected side-effects.
*
* @param <T> The return type
*/
public interface ExpectedCondition<T> extends Function<AndroidDriver, T> {}

经过修改之后,就可以在appium中直接使用:

        //wait for 60s if WebElemnt show up less than 60s , then return , until 60s
WebElement showClose = new AndroidDriverWait(driver, 60)
.until(new ExpectedCondition<WebElement>() {
public WebElement apply(AndroidDriver d) {
return d.findElement(By
.id("com.zhihu.android:id/showcase_close"));
} });

个人心得:

做自动化也有4年多,之前做的是server端的测试,并未太多的关注过selenium,现在selenium上面的积累已经一年多,

起初学东西是拿来用,不知其原理,只知道这么用,长期以来,并未有什么进步。最近看到一些大牛分享的学习思路,才发现学东西要循序渐进,

学会使用->熟练使用->了解掌握其原理->分析改造源码->造轮子

经过这一系列的学习和总结,就能成为资深人士。

Appium 三种wait方法(appium 学习之改造轮子)的更多相关文章

  1. (java)selenium webdriver学习---三种等待时间方法:显式等待,隐式等待,强制等待

    selenium webdriver学习---三种等待时间方法:显式等待,隐式等待,强制等待 本例包括窗口最大化,刷新,切换到指定窗口,后退,前进,获取当前窗口url等操作: import java. ...

  2. Jquery中each的三种遍历方法

    Jquery中each的三种遍历方法 $.post("urladdr", { "data" : "data" }, function(dat ...

  3. java数组中的三种排序方法中的冒泡排序方法

    我记得我大学学java的时候,怎么就是搞不明白这三种排序方法,也一直不会,现在我有发过来学习下这三种方法并记录下来. 首先说说冒泡排序方法:冒泡排序方法就是把数组中的每一个元素进行比较,如果第i个元素 ...

  4. 2017.10.25 Java List /ArrayList 三种遍历方法

    java list三种遍历方法性能比较 学习java语言list遍历的三种方法,顺便测试各种遍历方法的性能,测试方法为在ArrayList中插入记录,然后遍历ArrayList,测试代码如下: pac ...

  5. 谈谈vector容器的三种遍历方法

    说明:本文仅供学习交流.转载请标明出处.欢迎转载!          vector容器是最简单的顺序容器,其用法相似于数组.实际上vector的底层实现就是採用动态数组.在编敲代码的过程中.经常会变量 ...

  6. map的三种遍历方法!

    map的三种遍历方法!   集合的一个很重要的操作---遍历,学习了三种遍历方法,三种方法各有优缺点~~ /* * To change this template, choose Tools | Te ...

  7. uni-app开发经验分享一: 多页面传值的三种解决方法

    开发了一年的uni-app,在这里总结一些uni-app开发中的问题,提供几个解决方法,分享给大家: 问题描述:一个主页面,需要联通一到两个子页面,子页面传值到主页面,主页面更新 问题难点: 首先我们 ...

  8. javase-常用三种遍历方法

    javase-常用三种遍历方法 import java.util.ArrayList; import java.util.Iterator; import java.util.List; public ...

  9. JS面向对象(3) -- Object类,静态属性,闭包,私有属性, call和apply的使用,继承的三种实现方法

    相关链接: JS面向对象(1) -- 简介,入门,系统常用类,自定义类,constructor,typeof,instanceof,对象在内存中的表现形式 JS面向对象(2) -- this的使用,对 ...

随机推荐

  1. 使用EXtjs6.2构建web项目

    一.项目简介 众所周知ext是一款非常强大的表格控件,尤其是里边的grid为用户提供了非常多的功能,现在主流的还是用extjs4.0-4.2,但是更高一点的版本更加符合人的审美要求.因此,在今天咱们构 ...

  2. Linux Supervisor 守护进程基本配置

    supervisor:C/S架构的进程控制系统,可使用户在类UNIX系统中监控.管理进程.常用于管理与某个用户或项目相关的进程. 组成部分supervisord:服务守护进程supervisorctl ...

  3. C/C++的基本数据类型

    数据类型决定数据的空间分配, 及能对其做什么操作. C语言中的四种基本数据类型: char,int,float,double. C与C++定义这些类型时都只是限定它们的最大最小值, 而不是它们的siz ...

  4. C#定义类型转化 及 格式化字符串

    operator 关键字 operator 关键字用来重载内置运算符,或提供类/结构声明中的用户定义转换.它可以定义不同类型之间采用何种转化方式和转化的结果. operator用于定义类型转化时可采用 ...

  5. Linux问题处理: stdio.h: No such file or directory linux

    本来打算编译一下<自制编程语言>一书的代码,结果提示错误: 其实说的还是很清楚的,一般出现这种情况都是没有安装相应的库,所以: 再次编译,成功: 测试代码: # comment print ...

  6. struts-validate.xml配置详解demo

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC &quo ...

  7. iis6 服务器做301跳转返回状态码200解决方法。

    倘若你的配置和上图一样的话,在查询返回值是200的情况,你试着把你服务器上的安全狗或者防火墙,还有360网站卫士之类的安全软件停止试试,看是否能正常.

  8. document.documentElement.clientHeight 和 $(window).height() 无法正确获取页面可视区高度

    背景: 弹出层插件(自适应) 实现过程中突然发现在获取可视区高度时,无论document.documentElement.clientHeight 还是 $(window).height()都无法正确 ...

  9. stackoverfow访问 ajax.googleapis.com

    本文实验环境:Fedora 23 先用nslookup 查看stackoverflow.com的ip -vc参数,表示用强制用TCP 查到的stackoverflow的地址似乎没错, 用本地的DNS解 ...

  10. 抓取百万知乎用户信息之HttpHelper的迭代之路

    什么是Httphelper? httpelpers是一个封装好拿来获取网络上资源的工具类.因为是用http协议,故取名httphelper. httphelper出现的背景 使用WebClient可以 ...