AndroidPageObjectTest_TimeOutManagement.java
以下代码使用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的更多相关文章
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- 故障重现(内存篇2),JAVA内存不足导致频繁回收和swap引起的性能问题
背景起因: 记起以前的另一次也是关于内存的调优分享下 有个系统平时运行非常稳定运行(没经历过大并发考验),然而在一次活动后,人数并发一上来后,系统开始卡. 我按经验开始调优,在每个关键步骤的加入如 ...
- Elasticsearch之java的基本操作一
摘要 接触ElasticSearch已经有一段了.在这期间,遇到很多问题,但在最后自己的不断探索下解决了这些问题.看到网上或多或少的都有一些介绍ElasticSearch相关知识的文档,但个人觉得 ...
- 论:开发者信仰之“天下IT是一家“(Java .NET篇)
比尔盖茨公认的IT界领军人物,打造了辉煌一时的PC时代. 2008年,史蒂夫鲍尔默接替了盖茨的工作,成为微软公司的总裁. 2013年他与微软做了最后的道别. 2013年以后,我才真正看到了微软的变化. ...
- 故障重现, JAVA进程内存不够时突然挂掉模拟
背景,服务器上的一个JAVA服务进程突然挂掉,查看产生了崩溃日志,如下: # Set larger code cache with -XX:ReservedCodeCacheSize= # This ...
- 死磕内存篇 --- JAVA进程和linux内存间的大小关系
运行个JAVA 用sleep去hold住 package org.hjb.test; public class TestOnly { public static void main(String[] ...
- 【小程序分享篇 一 】开发了个JAVA小程序, 用于清除内存卡或者U盘里的垃圾文件非常有用
有一种场景, 手机内存卡空间被用光了,但又不知道哪个文件占用了太大,一个个文件夹去找又太麻烦,所以我开发了个小程序把手机所有文件(包括路径下所有层次子文件夹下的文件)进行一个排序,这样你就可以找出哪个 ...
- Java多线程基础学习(二)
9. 线程安全/共享变量——同步 当多个线程用到同一个变量时,在修改值时存在同时修改的可能性,而此时该变量只能被赋值一次.这就会导致出现“线程安全”问题,这个被多个线程共用的变量称之为“共享变量”. ...
- Java多线程基础学习(一)
1. 创建线程 1.1 通过构造函数:public Thread(Runnable target, String name){} 或:public Thread(Runnable target ...
随机推荐
- win10 升级导致找不到SQL Server配置管理器
1.背景 SQL Server配置管理器可用来管理与SQL Server相关联的服务.配置SQL Server使用的网络协议以及从SQL Server客户端计算机管理网络连接配置.但是win10从17 ...
- 腾讯IM的那些坑
项目中接入腾讯IM,在这里记录下,以便大家解决问题时少走弯路 1.首先讲一下IM返回对象的问题: /** * 消息工厂方法 */ public static Message getMessage(TI ...
- Codeforces Gym 100431D Bubble Sort 水题乱搞
原题链接:http://codeforces.com/gym/100431/attachments/download/2421/20092010-winter-petrozavodsk-camp-an ...
- Spring实战Day3
通过XML创建装配bean 1.装配不存在成员变量的bean <bean id="talent" class="cn.jqzhong.Spring.study.da ...
- PHP平均小数红包算法
<?php function RandMoney( $money,$num ){ $arr = array();//存放金额 $total_money = 0;//红包总金额 $thisMone ...
- mybatis学习网站
http://www.mybatis.org/mybatis-3/zh/index.html
- Android gradle 相关配置
有时候我们需要重命名输出apk文件名,在Android studio 3.0以前我们是这样写的: applicationVariants.all { variant -> variant.out ...
- Hibernate 3 深度解析--苏春波
Hibernate 3 深度解析 Hibernate 作为 Java ORM 模式的优秀开源实现, 当下已经成为一种标准,为饱受 JDBC 折磨的 Java 开发者带来了“福音.快速的版本更新,想 ...
- Unity -- 入门教程二
为了接下来要做的小游戏,在这里我要小小的修改一下移动的代码. public class PlayerMove : MonoBehaviour { //定义移动的速度 public float Move ...
- Linux 设备驱动开发 —— platform设备驱动应用实例解析
前面我们已经学习了platform设备的理论知识Linux 设备驱动开发 —— platform 设备驱动 ,下面将通过一个实例来深入我们的学习. 一.platform 驱动的工作过程 platfor ...