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 ...
随机推荐
- MinGW 使用和创建 DLL 应注意的问题
MinGW 是 GCC 的 Windows 版本,稳定版已经到了 4.5.2,功能和性能上很好,感觉不比 Microsoft 自家的 VC 差啊.但是 MinGW 下使用和创建 DLL 倒是要特别注意 ...
- IOS 使用DSYM文件定位Bug 的具体位置
在项目的开发中,我们通常需要排查和修改测试中和发布后线上的一些bug,现在有一些第三方的bug分享和查找工具SDK,如腾讯的Bugly和听云等,包括苹果的开发工具xcode也自带 bug查找工具.那么 ...
- Microsoft SQL Server 2005技术内幕:存储引擎笔记
http://www.cnblogs.com/lyhabc/articles/3942053.html
- SQL SERVER 内存
http://www.cnblogs.com/CareySon/archive/2012/08/16/HowSQLServerManageMemory.html
- Android Studio apk 打包流程(转)http://blog.chinaunix.net/uid-26000296-id-5567890.html
1.Build -> Generate Signed APK...,打开如下窗口 2.假设这里没有打过apk包,点击Create new,窗口如下 这里只要输入几个必要项 Key store p ...
- Boost.Asio c++ 网络编程翻译(18)
同步服务端 同步服务端也相当简单.它须要两个线程,一个负责接收新的client.另外一个负责处理已经存在的client. 它不能使用单线程:等带一个新的client是一个堵塞操作,所以我们须要另外一个 ...
- 第八讲_图像问答Image Question Answering
第八讲_图像问答Image Question Answering 课程结构 图像问答的描述 具备一系列AI能力:细分识别,物体检测,动作识别,常识推理,知识库推理..... 先要根据问题,判断什么任务 ...
- python得到最长递增子串长度及起始位置【转】
def incSeq(seq): start = 0 for i in xrange(1, len(seq)): if seq[i] < seq[i-1]: yield start, i - s ...
- win7自带照片查看器
win10如何找回自带的照片查看器 方法/步骤 1 首先,我们打开一个记事本,可以点击win+r打开运行框,然后在运行框中输入notepad.或者在桌面右键点击里面的新建,然后在新建中找到文本 ...
- FastDFS的配置、部署与API使用解读(8)FastDFS多种文件上传接口详解(转)
1.StorageClient与StorageClient1的区别 相信使用happy_fish的FastDFS的童鞋们,一定都熟悉StorageClient了,或者你熟悉的是StorageClien ...