以下代码使用ApiDemos-debug.apk进行测试

//这个脚本用于演示PageFactory的功能:设置timeout时间。

 package com.saucelabs.appium;

 import com.saucelabs.appium.page_object.PageObjectWithCustomizedTimeOuts;//一个页面类,其实例传给PageFactory的进行初始化。
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import io.appium.java_client.pagefactory.TimeOutDuration;
import io.appium.java_client.remote.MobileCapabilityType;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.PageFactory; import java.io.File;
import java.net.URL;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.TimeUnit; import static org.junit.Assert.assertEquals; /**
* This sample just demonstrates that the time out customization/changing
* works fine
*/
public class AndroidPageObjectTest_TimeOutManagement { private WebDriver driver;
private PageObjectWithCustomizedTimeOuts pageObjectWithCustomizedTimeOuts;
private TimeOutDuration timeOutDuration;
private final static long ACCEPTABLE_DELTA_MILLS = 1500; //Android UIAutomator sometimes
//is very slow @Before
public void setUp() throws Exception {
//File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File("E:/package");
File app = new File(appDir, "ApiDemos-debug.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
timeOutDuration = new TimeOutDuration(1, TimeUnit.SECONDS); /* This object can be passed through
the constructor of AppiumFieldDecorator. It allows users to set general timeout
of the waiting for elements conveniently and change it when it is needed.
*/ pageObjectWithCustomizedTimeOuts = new PageObjectWithCustomizedTimeOuts();//一个页面类的实例。
//This time out is set because test can be run on slow Android SDK emulator
PageFactory.initElements(new AppiumFieldDecorator(driver, timeOutDuration),
pageObjectWithCustomizedTimeOuts);
} @After
public void tearDown() throws Exception {
driver.quit();
} private static void checkTimeDifference(long expectedTime,//定位元素时的期待延迟时间,也就是timeout设定的时间。
TimeUnit timeUnit, long currentMillis) {//定位元素的实际所用时间
System.out.println("expectedTime: "+expectedTime);
System.out.println("timeUnit: "+timeUnit);
System.out.println("currentMills: "+currentMillis);
long expectedMillis = TimeUnit.MILLISECONDS.convert(expectedTime,
timeUnit);
try {
assertEquals(true,
((currentMillis - expectedMillis) < ACCEPTABLE_DELTA_MILLS)//ACCEPTABLE_DELTA_MILLS是不加任何额外延迟的超时时间?
&& ((currentMillis - expectedMillis) >= 0));//若这个断言成立,则说明:使用timeOutDuration设置的额外延迟生效了,且定位元素所用时间与额外延迟时间的差值小于默认的超时时间。
}
catch (Error e){
String message = String.valueOf(expectedTime) + " " + timeUnit.toString() + " current duration in millis " +
String.valueOf(currentMillis) + " Failed";
throw new RuntimeException(message, e);
}
} private long getBenchMark(List<MobileElement> stubElements) {
long startMark = Calendar.getInstance().getTimeInMillis();
stubElements.size();//在PageFactory模式下,使用页面类中的元素,会触发查找元素。所以getBenchMark函数的作用是,测量定位元素的所用时间。
long endMark = Calendar.getInstance().getTimeInMillis();
return endMark - startMark;
} /**
* Please read about Page Object design pattern here:
* https://code.google.com/p/selenium/wiki/PageObjects
*/
/**
* Page Object best practice is to describe interactions with target
* elements by methods. These methods describe business logic of the page/screen.
* Here test interacts with lazy instantiated elements directly.
* It was done so just for obviousness
*/ @Test
public void test() {
checkTimeDifference(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT, AppiumFieldDecorator.DEFAULT_TIMEUNIT,
getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements));
System.out.println(String.valueOf(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT)
+ " " + AppiumFieldDecorator.DEFAULT_TIMEUNIT.toString() + ": Fine"); timeOutDuration.setTime(15500000, TimeUnit.MICROSECONDS);//更改定位元素的超时时间。
checkTimeDifference(15500000, TimeUnit.MICROSECONDS, getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements));
System.out.println("Change time: " + String.valueOf(15500000) + " "
+ TimeUnit.MICROSECONDS.toString() + ": Fine"); timeOutDuration.setTime(3, TimeUnit.SECONDS);
checkTimeDifference(3, TimeUnit.SECONDS, getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements));
System.out.println("Change time: " + String.valueOf(3) + " "
+ TimeUnit.SECONDS.toString() + ": Fine");
} @Test
public void test2() {
checkTimeDifference(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT, AppiumFieldDecorator.DEFAULT_TIMEUNIT,
getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements));//查找stubElements。
System.out.println(String.valueOf(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT)
+ " " + AppiumFieldDecorator.DEFAULT_TIMEUNIT.toString() + ": Fine"); checkTimeDifference(5, TimeUnit.SECONDS,
getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements2));
System.out.println(String.valueOf(5)
+ " " + TimeUnit.SECONDS.toString() + ": Fine"); timeOutDuration.setTime(15500000, TimeUnit.MICROSECONDS);
checkTimeDifference(15500000, TimeUnit.MICROSECONDS, getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements));
System.out.println("Change time: " + String.valueOf(15500000) + " "
+ TimeUnit.MICROSECONDS.toString() + ": Fine"); checkTimeDifference(5, TimeUnit.SECONDS,
getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements2));
System.out.println(String.valueOf(5)
+ " " + TimeUnit.SECONDS.toString() + ": Fine"); }
}

输出结果为

//下面是test的结果
expectedTime: 1
timeUnit: SECONDS
currentMills: 1343//查找 stubElements 的所用时间,说明在setup中设置的timeOutDuration生效了。
1 SECONDS: Fine
expectedTime: 15500000
timeUnit: MICROSECONDS
currentMills: 15559//查找 stubElements 的所用时间,说明在test中设置的timeOutDuration生效了。
Change time: 15500000 MICROSECONDS: Fine
expectedTime: 3
timeUnit: SECONDS
currentMills: 3602//查找 stubElements 的所用时间,说明在test中设置的timeOutDuration生效了。
Change time: 3 SECONDS: Fine
//下面是test2的结果
expectedTime: 1
timeUnit: SECONDS
currentMills: 1721//查找 stubElements 的所用时间,说明在setup中的设置生效了,其它test中的设置不影响test2。
1 SECONDS: Fine
expectedTime: 5 //查找 stubElements2 的所用时间,说明页面类中的WithTimeout注解生效了,优先级高于setup中的设置。
timeUnit: SECONDS
currentMills: 5482
5 SECONDS: Fine
expectedTime: 15500000
timeUnit: MICROSECONDS
currentMills: 15543//查找 stubElements 的所用时间,说明在test2中设置的timeOutDuration生效了。
Change time: 15500000 MICROSECONDS: Fine
expectedTime: 5
timeUnit: SECONDS
currentMills: 6269//查找 stubElements2 的所用时间,说明页面类中的WithTimeout注解生效了,优先级高于test2的timeOutDuration设置。
5 SECONDS: Fine Process finished with exit code 0

下面为页面类的代码

 package com.saucelabs.appium.page_object;

 import io.appium.java_client.MobileElement;
import io.appium.java_client.pagefactory.WithTimeout;
import org.openqa.selenium.support.FindBy; import java.util.List;
import java.util.concurrent.TimeUnit; public class PageObjectWithCustomizedTimeOuts { /**
* Page Object best practice is to describe interactions with target
* elements by methods. This methods describe business logic of the page/screen.
* Here lazy instantiated elements are public.
* It was done so just for obviousness
*/ @FindBy(className = "OneClassWhichDoesNotExist")
public List<MobileElement> stubElements; /*Any timeout of the waiting for element/list of elements
can be customized if the general time duration is
not suitable. E.g. the element is rendered for a long time
or the element is used just for instant checkings/assertions
*/
@WithTimeout(time = 5, unit = TimeUnit.SECONDS)
@FindBy(className = "OneAnotherClassWhichDoesNotExist")
public List<MobileElement> stubElements2;
}

关于timeOutDuration和timeout注解的总结如下:

  • timeOutDuration的设置对元素生效;
  • 不同test中的timeOutDuration设置互不干扰;
  • 若页面类中的元素有WithTimeout注解,则注解优先级高于test中的timeOutDuration设置。

AndroidPageObjectTest_TimeOutManagement.java的更多相关文章

  1. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  2. 故障重现(内存篇2),JAVA内存不足导致频繁回收和swap引起的性能问题

    背景起因: 记起以前的另一次也是关于内存的调优分享下   有个系统平时运行非常稳定运行(没经历过大并发考验),然而在一次活动后,人数并发一上来后,系统开始卡. 我按经验开始调优,在每个关键步骤的加入如 ...

  3. Elasticsearch之java的基本操作一

    摘要   接触ElasticSearch已经有一段了.在这期间,遇到很多问题,但在最后自己的不断探索下解决了这些问题.看到网上或多或少的都有一些介绍ElasticSearch相关知识的文档,但个人觉得 ...

  4. 论:开发者信仰之“天下IT是一家“(Java .NET篇)

    比尔盖茨公认的IT界领军人物,打造了辉煌一时的PC时代. 2008年,史蒂夫鲍尔默接替了盖茨的工作,成为微软公司的总裁. 2013年他与微软做了最后的道别. 2013年以后,我才真正看到了微软的变化. ...

  5. 故障重现, JAVA进程内存不够时突然挂掉模拟

    背景,服务器上的一个JAVA服务进程突然挂掉,查看产生了崩溃日志,如下: # Set larger code cache with -XX:ReservedCodeCacheSize= # This ...

  6. 死磕内存篇 --- JAVA进程和linux内存间的大小关系

    运行个JAVA 用sleep去hold住 package org.hjb.test; public class TestOnly { public static void main(String[] ...

  7. 【小程序分享篇 一 】开发了个JAVA小程序, 用于清除内存卡或者U盘里的垃圾文件非常有用

    有一种场景, 手机内存卡空间被用光了,但又不知道哪个文件占用了太大,一个个文件夹去找又太麻烦,所以我开发了个小程序把手机所有文件(包括路径下所有层次子文件夹下的文件)进行一个排序,这样你就可以找出哪个 ...

  8. Java多线程基础学习(二)

    9. 线程安全/共享变量——同步 当多个线程用到同一个变量时,在修改值时存在同时修改的可能性,而此时该变量只能被赋值一次.这就会导致出现“线程安全”问题,这个被多个线程共用的变量称之为“共享变量”. ...

  9. Java多线程基础学习(一)

    1. 创建线程    1.1 通过构造函数:public Thread(Runnable target, String name){}  或:public Thread(Runnable target ...

随机推荐

  1. Web Cache

    我们都知道,网站对于一些常用数据做缓存,会加速网站访问,像下面这样: public string GetFoo() { if ( cache.get("Foo") == null ...

  2. cookies-判断用户是否是第一次进入页面

    在郑州美莱的活动项目中客户当时有提到该需求.尽管最后去掉了该需求,但是还是花了我不少时间研究,因为之前的项目我前端没有用到过cookies,吓死宝宝了 $(document).ready(functi ...

  3. 牛客网 Wannafly挑战赛5 B.可编程拖拉机比赛-ceil()函数+floor()函数

    可编程拖拉机比赛 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 65536K,其他语言131072K64bit IO Format: %lld 题目描述 “这个比赛,归根结底就是控制一 ...

  4. [原创][FPGA][IP-Core]altlvds_tx & altlvds_rx

    1. 概述 Alter公司的QuartusII软件提供了LVDS发送和接收的IP核供我们使用,其在本质上可以理解为并行-串行数据的转换器.其在官方文档(见附件)上也这样说过.其中的应用场景有告诉AD/ ...

  5. 再次了解android中屏幕尺寸,单位等

    android设备多种多样,要让你的app能够适配所有的屏幕是一件很痛苦的事情,在做适配之前我们首先需要了解android中dimension的定义 android中dimension是如何定义的? ...

  6. 某考试 T3 Try to find out the wrong in the test

    Discription Hint: 对于 100% 的数据, n<=10^6.

  7. IE使用多彩文档上传数据库报错

    使用多彩文档,用IE浏览器提交表单,双引号里面包含单引号,导致数据库插入不了,而用chrome浏览器不会报错,自动过滤单引号, 解决:content.replace("'", &q ...

  8. IIS下安装memcached管理工具—MemAdmin

    1.先看这篇文章 http://www.cnblogs.com/joylee/archive/2013/01/07/memadmin.html . 2.在IIS下安装的php-cgi.exe程序版本为 ...

  9. Handler处理机制

    handler缺点:如果要运送两种类型的数据(比如一个Bitmap,一个Object)就不能运送,但可以用Bunder来传输  *    使用handler的步骤:  *    1.创建一个handl ...

  10. 【java】RC4加密转16进制获取长度为40的不重复优惠码字符串 【未优化版本】

    需求:需要一串给各机构独有的优惠码 间接需求:固定长度.不重复.没有规律可循 实现思想如下: 1.首先获取一个UUID 2.去除UUID中的“-” 3.小写转大写 4.获取一个固定长度字符串 5.按照 ...