古人已用三冬足,年少今开万卷余。这篇文章主要讲述AndroidPageObjectTest_TimeOutManagement.java相关的知识,希望能为你提供帮助。
以下代码使用ApiDemos-debug.apk进行测试//这个脚本用于演示PageFactory的功能:设置timeout时间。
1 package com.saucelabs.appium; 2 3 import com.saucelabs.appium.page_object.PageObjectWithCustomizedTimeOuts; //一个页面类,其实例传给PageFactory的进行初始化。 4 import io.appium.java_client.MobileElement; 5 import io.appium.java_client.android.AndroidDriver; 6 import io.appium.java_client.pagefactory.AppiumFieldDecorator; 7 import io.appium.java_client.pagefactory.TimeOutDuration; 8 import io.appium.java_client.remote.MobileCapabilityType; 9 import org.junit.After; 10 import org.junit.Before; 11 import org.junit.Test; 12 import org.openqa.selenium.WebDriver; 13 import org.openqa.selenium.remote.DesiredCapabilities; 14 import org.openqa.selenium.support.PageFactory; 15 16 import java.io.File; 17 import java.net.URL; 18 import java.util.Calendar; 19 import java.util.List; 20 import java.util.concurrent.TimeUnit; 21 22 import static org.junit.Assert.assertEquals; 23 24 /** 25* This sample just demonstrates that the time out customization/changing 26* works fine 27*/ 28 public class AndroidPageObjectTest_TimeOutManagement { 29 30private WebDriver driver; 31private PageObjectWithCustomizedTimeOuts pageObjectWithCustomizedTimeOuts; 32private TimeOutDuration timeOutDuration; 33private final static long ACCEPTABLE_DELTA_MILLS = 1500; //Android UIAutomator sometimes 34//is very slow 35 36@Before 37public void setUp() throws Exception { 38//File classpathRoot = new File(System.getProperty("user.dir")); 39File appDir = new File("E:/package"); 40File app = new File(appDir, "ApiDemos-debug.apk"); 41DesiredCapabilities capabilities = new DesiredCapabilities(); 42capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator"); 43capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath()); 44driver = new AndroidDriver< MobileElement> (new URL("http://127.0.0.1:4723/wd/hub"), capabilities); 45timeOutDuration = new TimeOutDuration(1, TimeUnit.SECONDS); /* This object can be passed through 46the constructor of AppiumFieldDecorator. It allows users to set general timeout 47of the waiting for elements conveniently and change it when it is needed. 48*/ 49 50pageObjectWithCustomizedTimeOuts = new PageObjectWithCustomizedTimeOuts(); //一个页面类的实例。 51//This time out is set because test can be run on slow Android SDK emulator 52PageFactory.initElements(new AppiumFieldDecorator(driver, timeOutDuration), 53pageObjectWithCustomizedTimeOuts); 54} 55 56@After 57public void tearDown() throws Exception { 58driver.quit(); 59} 60 61private static void checkTimeDifference(long expectedTime,//定位元素时的期待延迟时间,也就是timeout设定的时间。 62TimeUnit timeUnit, long currentMillis) {//定位元素的实际所用时间 63System.out.println("expectedTime: "+expectedTime); 64System.out.println("timeUnit: "+timeUnit); 65System.out.println("currentMills: "+currentMillis); 66long expectedMillis = TimeUnit.MILLISECONDS.convert(expectedTime, 67timeUnit); 68try { 69assertEquals(true, 70((currentMillis - expectedMillis) < ACCEPTABLE_DELTA_MILLS)//ACCEPTABLE_DELTA_MILLS是不加任何额外延迟的超时时间? 71& & ((currentMillis - expectedMillis) > = 0)); //若这个断言成立,则说明:使用timeOutDuration设置的额外延迟生效了,且定位元素所用时间与额外延迟时间的差值小于默认的超时时间。 72} 73catch (Error e){ 74String message = String.valueOf(expectedTime) + " "+ timeUnit.toString() + " current duration in millis " + 75String.valueOf(currentMillis) + " Failed"; 76throw new RuntimeException(message, e); 77} 78} 79 80private long getBenchMark(List< MobileElement> stubElements) { 81long startMark = Calendar.getInstance().getTimeInMillis(); 82stubElements.size(); //在PageFactory模式下,使用页面类中的元素,会触发查找元素。所以getBenchMark函数的作用是,测量定位元素的所用时间。 83long endMark = Calendar.getInstance().getTimeInMillis(); 84return endMark - startMark; 85} 86 87/** 88* Please read about Page Object design pattern here: 89*https://code.google.com/p/selenium/wiki/PageObjects 90*/ 91/** 92* Page Object best practice is to describe interactions with target 93* elements by methods. These methods describe business logic of the page/screen. 94* Here test interacts with lazy instantiated elements directly. 95* It was done so just for obviousness 96*/ 97 98@Test 99public void test() { 100checkTimeDifference(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT, AppiumFieldDecorator.DEFAULT_TIMEUNIT, 101getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements)); 102System.out.println(String.valueOf(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT) 103+ " " + AppiumFieldDecorator.DEFAULT_TIMEUNIT.toString() + ": Fine"); 104 105timeOutDuration.setTime(15500000, TimeUnit.MICROSECONDS); //更改定位元素的超时时间。 106checkTimeDifference(15500000, TimeUnit.MICROSECONDS, getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements)); 107System.out.println("Change time: " + String.valueOf(15500000) + " " 108+ TimeUnit.MICROSECONDS.toString() + ": Fine"); 109 110timeOutDuration.setTime(3, TimeUnit.SECONDS); 111checkTimeDifference(3, TimeUnit.SECONDS, getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements)); 112System.out.println("Change time: " + String.valueOf(3) + " " 113+ TimeUnit.SECONDS.toString() + ": Fine"); 114} 115 116@Test 117public void test2() { 118checkTimeDifference(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT, AppiumFieldDecorator.DEFAULT_TIMEUNIT, 119getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements)); //查找stubElements。 120System.out.println(String.valueOf(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT) 121+ " " + AppiumFieldDecorator.DEFAULT_TIMEUNIT.toString() + ": Fine"); 122 123checkTimeDifference(5, TimeUnit.SECONDS, 124getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements2)); 125System.out.println(String.valueOf(5) 126+ " " + TimeUnit.SECONDS.toString() + ": Fine"); 127 128 129timeOutDuration.setTime(15500000, TimeUnit.MICROSECONDS); 130checkTimeDifference(15500000, TimeUnit.MICROSECONDS, getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements)); 131System.out.println("Change time: " + String.valueOf(15500000) + " " 132+ TimeUnit.MICROSECONDS.toString() + ": Fine"); 133 134checkTimeDifference(5, TimeUnit.SECONDS, 135getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements2)); 136System.out.println(String.valueOf(5) 137+ " " + TimeUnit.SECONDS.toString() + ": Fine"); 138 139} 140 }
输出结果为
//下面是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: FineProcess finished with exit code 0
下面为页面类的代码
1 package com.saucelabs.appium.page_object; 2 3 import io.appium.java_client.MobileElement; 4 import io.appium.java_client.pagefactory.WithTimeout; 5 import org.openqa.selenium.support.FindBy; 6 7 import java.util.List; 8 import java.util.concurrent.TimeUnit; 9 10 public class PageObjectWithCustomizedTimeOuts { 11 12/** 13* Page Object best practice is to describe interactions with target 14* elements by methods. This methods describe business logic of the page/screen. 15* Here lazy instantiated elements are public. 16* It was done so just for obviousness 17*/ 18 19@FindBy(className = "OneClassWhichDoesNotExist") 20public List< MobileElement> stubElements; 21 22/*Any timeout of the waiting for element/list of elements 23can be customized if the general time duration is 24not suitable. E.g. the element is rendered for a long time 25or the element is used just for instant checkings/assertions 26*/ 27@WithTimeout(time = 5, unit = TimeUnit.SECONDS) 28@FindBy(className = "OneAnotherClassWhichDoesNotExist") 29public List< MobileElement> stubElements2; 30 }
关于timeOutDuration和timeout注解的总结如下:
- timeOutDuration的设置对元素生效;
- 不同test中的timeOutDuration设置互不干扰;
- 若页面类中的元素有WithTimeout注解,则注解优先级高于test中的timeOutDuration设置。
推荐阅读
- Android6.0动态获取权限
- AndroidStudio使用过程中遇到的bug
- Android Studio Madual作为application的使用以及工作空间和modual的区别
- 在线看Android系统源码,那些相见恨晚的几种方案
- Android学习之如何集成极光短信验证
- Android数据存储之Android 6.0运行时权限下文件存储的思考
- android手机介绍
- android pay介绍
- android watch介绍