|
| 1 | +package com.saucelabs.appium; |
| 2 | + |
| 3 | +import com.saucelabs.appium.page_object.PageObjectWithCustomizedTimeOuts; |
| 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(classpathRoot, "../../../apps/ApiDemos/bin"); |
| 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, |
| 62 | + TimeUnit timeUnit, long currentMillis) { |
| 63 | + long expectedMillis = TimeUnit.MILLISECONDS.convert(expectedTime, |
| 64 | + timeUnit); |
| 65 | + try { |
| 66 | + assertEquals(true, |
| 67 | + ((currentMillis - expectedMillis) < ACCEPTABLE_DELTA_MILLS) |
| 68 | + && ((currentMillis - expectedMillis) >= 0)); |
| 69 | + } |
| 70 | + catch (Error e){ |
| 71 | + String message = String.valueOf(expectedTime) + " " + timeUnit.toString() + " current duration in millis " + |
| 72 | + String.valueOf(currentMillis) + " Failed"; |
| 73 | + throw new RuntimeException(message, e); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private long getBenchMark(List<MobileElement> stubElements) { |
| 78 | + long startMark = Calendar.getInstance().getTimeInMillis(); |
| 79 | + stubElements.size(); |
| 80 | + long endMark = Calendar.getInstance().getTimeInMillis(); |
| 81 | + return endMark - startMark; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Please read about Page Object design pattern here: |
| 86 | + * https://code.google.com/p/selenium/wiki/PageObjects |
| 87 | + */ |
| 88 | + /** |
| 89 | + * Page Object best practice is to describe interactions with target |
| 90 | + * elements by methods. These methods describe business logic of the page/screen. |
| 91 | + * Here test interacts with lazy instantiated elements directly. |
| 92 | + * It was done so just for obviousness |
| 93 | + */ |
| 94 | + |
| 95 | + @Test |
| 96 | + public void test() { |
| 97 | + checkTimeDifference(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT, AppiumFieldDecorator.DEFAULT_TIMEUNIT, |
| 98 | + getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements)); |
| 99 | + System.out.println(String.valueOf(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT) |
| 100 | + + " " + AppiumFieldDecorator.DEFAULT_TIMEUNIT.toString() + ": Fine"); |
| 101 | + |
| 102 | + timeOutDuration.setTime(15500000, TimeUnit.MICROSECONDS); |
| 103 | + checkTimeDifference(15500000, TimeUnit.MICROSECONDS, getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements)); |
| 104 | + System.out.println("Change time: " + String.valueOf(15500000) + " " |
| 105 | + + TimeUnit.MICROSECONDS.toString() + ": Fine"); |
| 106 | + |
| 107 | + timeOutDuration.setTime(3, TimeUnit.SECONDS); |
| 108 | + checkTimeDifference(3, TimeUnit.SECONDS, getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements)); |
| 109 | + System.out.println("Change time: " + String.valueOf(3) + " " |
| 110 | + + TimeUnit.SECONDS.toString() + ": Fine"); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void test2() { |
| 115 | + checkTimeDifference(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT, AppiumFieldDecorator.DEFAULT_TIMEUNIT, |
| 116 | + getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements)); |
| 117 | + System.out.println(String.valueOf(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT) |
| 118 | + + " " + AppiumFieldDecorator.DEFAULT_TIMEUNIT.toString() + ": Fine"); |
| 119 | + |
| 120 | + checkTimeDifference(5, TimeUnit.SECONDS, |
| 121 | + getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements2)); |
| 122 | + System.out.println(String.valueOf(5) |
| 123 | + + " " + TimeUnit.SECONDS.toString() + ": Fine"); |
| 124 | + |
| 125 | + |
| 126 | + timeOutDuration.setTime(15500000, TimeUnit.MICROSECONDS); |
| 127 | + checkTimeDifference(15500000, TimeUnit.MICROSECONDS, getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements)); |
| 128 | + System.out.println("Change time: " + String.valueOf(15500000) + " " |
| 129 | + + TimeUnit.MICROSECONDS.toString() + ": Fine"); |
| 130 | + |
| 131 | + checkTimeDifference(5, TimeUnit.SECONDS, |
| 132 | + getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements2)); |
| 133 | + System.out.println(String.valueOf(5) |
| 134 | + + " " + TimeUnit.SECONDS.toString() + ": Fine"); |
| 135 | + |
| 136 | + } |
| 137 | +} |
0 commit comments