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 ...
随机推荐
- html5(拖拽1)
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- ecshop二次开发系统缓存优化之扩展数据缓存的必要性与方法
1.扩展数据缓存的必要性 大家都知道ecshop系统使用的是静态模板缓存,在后台可以设置静态模板的缓存时间,只要缓存不过期,用户访问页面就相当于访问静态页面,速度可想而知,看似非常完美,但是ecsho ...
- vs2017秘钥
VS2017专业版和企业版激活密钥 需要的请自取- Enterprise: NJVYC-BMHX2-G77MM-4XJMR-6Q8QF Professional: KBJFW-NXHK6-W4WJM- ...
- JavaScript实现弹幕效果
效果如下 <html> <head> <title></title> <script src="https://cdn.staticfi ...
- C#深入学习:泛型修饰符in,out、逆变委托类型和协变委托类型
在C#中,存在两个泛型修饰符:in和out,他们分别对应逆变委托和协变委托. 我们知道,在C#中要想将一个泛型对象转换为另一个泛型对象时,必须要将一个泛型对象拆箱,对元素进行显式或隐式转换后重新装箱. ...
- jenkins执行单元测试,会产生大量临时文件,要及时删除,不然会把inode耗尽
jenkins的build命令:clean test -U findbugs:findbugs pmd:pmd sonar:sonar -Djava.io.tmpdir=/tmp/ -Dsonar.p ...
- c# Dictionary拓展2个key得到1个value
using System.Collections.Generic; using System.Collections; Dictionary<Tuple<int, int>, int ...
- 百科知识 hta文件如何打开
后缀名为hta是什么文件,谢谢? 2006-10-11 21:36 提问者: tanhailong2006 | 浏览次数:2092次 我来帮他解答 输入内容已经达到长度限制 还能输入 9999 字 插 ...
- Vue 字面量语法 vs 动态语法
初学者常犯的一个错误是使用字面量语法传递数值: <!-- 传递了一个字符串 "1" --> <comp some-prop="1">&l ...
- Unity3D游戏开发之简单的碰撞检測
在"Project"面板中单击"Create"旁边的小三角,选择"javascript"创建一个名为"collision" ...