|
| 1 | +package io.appium.java_client.pagefactory_tests; |
| 2 | + |
| 3 | +import io.appium.java_client.pagefactory.AppiumFieldDecorator; |
| 4 | + |
| 5 | +import java.util.Calendar; |
| 6 | +import java.util.List; |
| 7 | +import java.util.concurrent.TimeUnit; |
| 8 | + |
| 9 | +import org.junit.After; |
| 10 | +import org.junit.Assert; |
| 11 | +import org.junit.Before; |
| 12 | +import org.junit.Test; |
| 13 | +import org.openqa.selenium.WebDriver; |
| 14 | +import org.openqa.selenium.WebElement; |
| 15 | +import org.openqa.selenium.firefox.FirefoxDriver; |
| 16 | +import org.openqa.selenium.support.FindBy; |
| 17 | +import org.openqa.selenium.support.PageFactory; |
| 18 | + |
| 19 | +public class TimeOutResetTest { |
| 20 | + private WebDriver driver; |
| 21 | + private final static long ACCEPTABLE_DELTA_MILLS = 500; |
| 22 | + |
| 23 | + /** |
| 24 | + * Default time out parameters |
| 25 | + */ |
| 26 | + |
| 27 | + private static long DEFAULT_IMPLICITLY_WAIT_TIMEOUT = 1; |
| 28 | + private static TimeUnit DEFAULT_TIMEUNIT = TimeUnit.SECONDS; |
| 29 | + |
| 30 | + @FindBy(className = "ClassWhichDoesNotExist") |
| 31 | + private List<WebElement> stubElements; |
| 32 | + private AppiumFieldDecorator afd; |
| 33 | + |
| 34 | + @Before |
| 35 | + public void setUp() throws Exception { |
| 36 | + driver = new FirefoxDriver(); |
| 37 | + afd = new AppiumFieldDecorator(driver); |
| 38 | + |
| 39 | + PageFactory.initElements(afd, this); |
| 40 | + } |
| 41 | + |
| 42 | + @After |
| 43 | + public void tearDown() throws Exception { |
| 44 | + driver.quit(); |
| 45 | + } |
| 46 | + |
| 47 | + private static void checkTimeDifference(long etalonTime, |
| 48 | + TimeUnit etalonTimeUnit, long currentMillis) { |
| 49 | + long etalonMillis = TimeUnit.MILLISECONDS.convert(etalonTime, |
| 50 | + etalonTimeUnit); |
| 51 | + try{ |
| 52 | + Assert.assertEquals(true, |
| 53 | + ((currentMillis - etalonMillis) < ACCEPTABLE_DELTA_MILLS) |
| 54 | + && ((currentMillis - etalonMillis) >= 0)); |
| 55 | + } |
| 56 | + catch (Error e){ |
| 57 | + String message = String.valueOf(etalonTime) + " " + etalonTimeUnit.toString() + " current duration in millis " + |
| 58 | + String.valueOf(currentMillis) + " Failed"; |
| 59 | + throw new RuntimeException(message, e); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private long getBenchMark() { |
| 64 | + long startMark = Calendar.getInstance().getTimeInMillis(); |
| 65 | + stubElements.size(); |
| 66 | + long endMark = Calendar.getInstance().getTimeInMillis(); |
| 67 | + return endMark - startMark; |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void test() { |
| 72 | + checkTimeDifference(DEFAULT_IMPLICITLY_WAIT_TIMEOUT, DEFAULT_TIMEUNIT, |
| 73 | + getBenchMark()); |
| 74 | + System.out.println(String.valueOf(DEFAULT_IMPLICITLY_WAIT_TIMEOUT) |
| 75 | + + " " + DEFAULT_TIMEUNIT.toString() + ": Fine"); |
| 76 | + |
| 77 | + afd.resetImplicitlyWaitTimeOut(15500000, TimeUnit.MICROSECONDS); |
| 78 | + checkTimeDifference(15500000, TimeUnit.MICROSECONDS, getBenchMark()); |
| 79 | + System.out.println("Change time: " + String.valueOf(15500000) + " " |
| 80 | + + TimeUnit.MICROSECONDS.toString() + ": Fine"); |
| 81 | + |
| 82 | + afd.resetImplicitlyWaitTimeOut(3, TimeUnit.SECONDS); |
| 83 | + checkTimeDifference(3, TimeUnit.SECONDS, getBenchMark()); |
| 84 | + System.out.println("Change time: " + String.valueOf(3) + " " |
| 85 | + + TimeUnit.SECONDS.toString() + ": Fine"); |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments