码迷,mamicode.com
首页 > 移动开发 > 详细

AndroidPageObjectTest_TimeOutManagement.java

时间:2016-12-07 16:33:03      阅读:334      评论:0      收藏:0      [点我收藏+]

标签:find   drive   auc   ldd   get   for   finish   list   runtime   

以下代码使用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 
 30     private WebDriver driver;
 31     private PageObjectWithCustomizedTimeOuts pageObjectWithCustomizedTimeOuts;
 32     private TimeOutDuration timeOutDuration;
 33     private final static long ACCEPTABLE_DELTA_MILLS = 1500; //Android UIAutomator sometimes
 34     //is very slow
 35 
 36     @Before
 37     public void setUp() throws Exception {
 38         //File classpathRoot = new File(System.getProperty("user.dir"));
 39         File appDir = new File("E:/package");
 40         File app = new File(appDir, "ApiDemos-debug.apk");
 41         DesiredCapabilities capabilities = new DesiredCapabilities();
 42         capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
 43         capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
 44         driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
 45         timeOutDuration = new TimeOutDuration(1, TimeUnit.SECONDS); /* This object can be passed through
 46         the constructor of AppiumFieldDecorator. It allows users to set general timeout
 47         of the waiting for elements conveniently and change it when it is needed.
 48         */
 49 
 50         pageObjectWithCustomizedTimeOuts = new PageObjectWithCustomizedTimeOuts();//一个页面类的实例。
 51         //This time out is set because test can be run on slow Android SDK emulator
 52         PageFactory.initElements(new AppiumFieldDecorator(driver, timeOutDuration),
 53                 pageObjectWithCustomizedTimeOuts);
 54     }
 55 
 56     @After
 57     public void tearDown() throws Exception {
 58         driver.quit();
 59     }
 60 
 61     private static void checkTimeDifference(long expectedTime,//定位元素时的期待延迟时间,也就是timeout设定的时间。
 62                                             TimeUnit timeUnit, long currentMillis) {//定位元素的实际所用时间
 63         System.out.println("expectedTime: "+expectedTime);
 64         System.out.println("timeUnit: "+timeUnit);
 65         System.out.println("currentMills: "+currentMillis);
 66         long expectedMillis = TimeUnit.MILLISECONDS.convert(expectedTime,
 67                 timeUnit);
 68         try {
 69             assertEquals(true,
 70                     ((currentMillis - expectedMillis) < ACCEPTABLE_DELTA_MILLS)//ACCEPTABLE_DELTA_MILLS是不加任何额外延迟的超时时间?
 71                             && ((currentMillis - expectedMillis) >= 0));//若这个断言成立,则说明:使用timeOutDuration设置的额外延迟生效了,且定位元素所用时间与额外延迟时间的差值小于默认的超时时间。
 72         }
 73         catch (Error e){
 74             String message = String.valueOf(expectedTime) + " "  + timeUnit.toString() + " current duration in millis " +
 75                     String.valueOf(currentMillis) + " Failed";
 76             throw new RuntimeException(message, e);
 77         }
 78     }
 79 
 80     private long getBenchMark(List<MobileElement> stubElements) {
 81         long startMark = Calendar.getInstance().getTimeInMillis();
 82         stubElements.size();//在PageFactory模式下,使用页面类中的元素,会触发查找元素。所以getBenchMark函数的作用是,测量定位元素的所用时间。
 83         long endMark = Calendar.getInstance().getTimeInMillis();
 84         return 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
 99     public void test() {
100         checkTimeDifference(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT, AppiumFieldDecorator.DEFAULT_TIMEUNIT,
101                 getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements));
102         System.out.println(String.valueOf(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT)
103                 + " " + AppiumFieldDecorator.DEFAULT_TIMEUNIT.toString() + ": Fine");
104 
105         timeOutDuration.setTime(15500000, TimeUnit.MICROSECONDS);//更改定位元素的超时时间。
106         checkTimeDifference(15500000, TimeUnit.MICROSECONDS, getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements));
107         System.out.println("Change time: " + String.valueOf(15500000) + " "
108                 + TimeUnit.MICROSECONDS.toString() + ": Fine");
109 
110         timeOutDuration.setTime(3, TimeUnit.SECONDS);
111         checkTimeDifference(3, TimeUnit.SECONDS, getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements));
112         System.out.println("Change time: " + String.valueOf(3) + " "
113                 + TimeUnit.SECONDS.toString() + ": Fine");
114     }
115 
116     @Test
117     public void test2() {
118         checkTimeDifference(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT, AppiumFieldDecorator.DEFAULT_TIMEUNIT,
119                 getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements));//查找stubElements。
120         System.out.println(String.valueOf(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT)
121                 + " " + AppiumFieldDecorator.DEFAULT_TIMEUNIT.toString() + ": Fine");
122 
123         checkTimeDifference(5, TimeUnit.SECONDS,
124                 getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements2));
125         System.out.println(String.valueOf(5)
126                 + " " + TimeUnit.SECONDS.toString() + ": Fine");
127 
128 
129         timeOutDuration.setTime(15500000, TimeUnit.MICROSECONDS);
130         checkTimeDifference(15500000, TimeUnit.MICROSECONDS, getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements));
131         System.out.println("Change time: " + String.valueOf(15500000) + " "
132                 + TimeUnit.MICROSECONDS.toString() + ": Fine");
133 
134         checkTimeDifference(5, TimeUnit.SECONDS,
135                 getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements2));
136         System.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: Fine Process 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")
20     public List<MobileElement> stubElements;
21 
22     /*Any timeout of the waiting for element/list of elements
23       can be customized if the general time duration is
24       not suitable. E.g. the element is rendered for a long time
25       or the element is used just for instant checkings/assertions
26      */
27     @WithTimeout(time = 5, unit = TimeUnit.SECONDS)
28     @FindBy(className = "OneAnotherClassWhichDoesNotExist")
29     public List<MobileElement> stubElements2;
30 }

 

 

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

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

 

AndroidPageObjectTest_TimeOutManagement.java

标签:find   drive   auc   ldd   get   for   finish   list   runtime   

原文地址:http://www.cnblogs.com/superbaby11/p/6141647.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!