diff --git a/build.gradle b/build.gradle index e8f159e6e..2d9a8d406 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,7 @@ apply plugin: 'signing' apply plugin: 'maven-publish' group 'io.appium' -version '5.0.3' +version '5.0.4' repositories { jcenter() @@ -54,7 +54,7 @@ compileJava { ] } -ext.seleniumVersion = '3.5.3' +ext.seleniumVersion = '3.6.0' dependencies { compile ("org.seleniumhq.selenium:selenium-java:${seleniumVersion}") { @@ -73,14 +73,14 @@ dependencies { compile ("org.seleniumhq.selenium:selenium-api:${seleniumVersion}") { force = true } - compile 'com.google.code.gson:gson:2.8.1' + compile 'com.google.code.gson:gson:2.8.2' compile 'org.apache.httpcomponents:httpclient:4.5.3' compile 'cglib:cglib:3.2.5' compile 'commons-validator:commons-validator:1.6' compile 'org.apache.commons:commons-lang3:3.6' compile 'commons-io:commons-io:2.5' - compile 'org.springframework:spring-context:4.3.10.RELEASE' - compile 'org.aspectj:aspectjweaver:1.8.10' + compile 'org.springframework:spring-context:5.0.0.RELEASE' + compile 'org.aspectj:aspectjweaver:1.8.11' compile 'org.openpnp:opencv:3.2.0-1' testCompile 'junit:junit:4.12' diff --git a/src/main/java/io/appium/java_client/ios/PushesFiles.java b/src/main/java/io/appium/java_client/ios/PushesFiles.java index 961723fa5..462725160 100644 --- a/src/main/java/io/appium/java_client/ios/PushesFiles.java +++ b/src/main/java/io/appium/java_client/ios/PushesFiles.java @@ -16,6 +16,9 @@ package io.appium.java_client.ios; +import static com.google.common.base.Preconditions.checkNotNull; +import static io.appium.java_client.MobileCommand.pushFileCommand; + import io.appium.java_client.CommandExecutionHelper; import io.appium.java_client.ExecutesMethod; import org.apache.commons.codec.binary.Base64; @@ -24,9 +27,6 @@ import java.io.File; import java.io.IOException; -import static com.google.common.base.Preconditions.checkNotNull; -import static io.appium.java_client.MobileCommand.pushFileCommand; - public interface PushesFiles extends ExecutesMethod { /** diff --git a/src/main/java/io/appium/java_client/pagefactory/AppiumElementLocator.java b/src/main/java/io/appium/java_client/pagefactory/AppiumElementLocator.java index 61c526476..a08ffc311 100644 --- a/src/main/java/io/appium/java_client/pagefactory/AppiumElementLocator.java +++ b/src/main/java/io/appium/java_client/pagefactory/AppiumElementLocator.java @@ -28,14 +28,12 @@ import org.openqa.selenium.SearchContext; import org.openqa.selenium.StaleElementReferenceException; import org.openqa.selenium.TimeoutException; -import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebDriverException; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.ui.FluentWait; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.TimeUnit; import java.util.function.Function; import java.util.function.Supplier; @@ -43,9 +41,7 @@ class AppiumElementLocator implements CacheableLocator { private final boolean shouldCache; private final By by; - private final TimeOutDuration timeOutDuration; - private final TimeOutDuration originalTimeOutDuration; - private final WebDriver originalWebDriver; + private final TimeOutDuration duration; private final SearchContext searchContext; private WebElement cachedElement; private List cachedElementList; @@ -60,33 +56,23 @@ class AppiumElementLocator implements CacheableLocator { * @param shouldCache is the flag that signalizes that elements which * are found once should be cached * @param duration is a POJO which contains timeout parameters for the element to be searched - * @param originalDuration is a POJO which contains timeout parameters from page object which contains the element - * @param originalWebDriver is an instance of WebDriver that is going to be - * used by a proxied element */ public AppiumElementLocator(SearchContext searchContext, By by, boolean shouldCache, - TimeOutDuration duration, TimeOutDuration originalDuration, WebDriver originalWebDriver) { + TimeOutDuration duration) { this.searchContext = searchContext; this.shouldCache = shouldCache; - this.timeOutDuration = duration; - this.originalTimeOutDuration = originalDuration; + this.duration = duration; this.by = by; - this.originalWebDriver = originalWebDriver; this.exceptionMessageIfElementNotFound = "Can't locate an element by this strategy: " + by.toString(); } - private void changeImplicitlyWaitTimeOut(long newTimeOut, TimeUnit newTimeUnit) { - originalWebDriver.manage().timeouts().implicitlyWait(newTimeOut, newTimeUnit); - } - - private T waitFor(Supplier supplier) { + private T waitFor(Supplier supplier) { WaitingFunction function = new WaitingFunction<>(); try { - changeImplicitlyWaitTimeOut(0, TimeUnit.SECONDS); FluentWait> wait = new FluentWait<>(supplier) .ignoring(NoSuchElementException.class); - wait.withTimeout(timeOutDuration.getTime(), timeOutDuration.getTimeUnit()); + wait.withTimeout(duration.getTime(), duration.getTimeUnit()); return wait.until(function); } catch (TimeoutException e) { if (function.foundStaleElementReferenceException != null) { @@ -94,8 +80,6 @@ private T waitFor(Supplier supplier) { .class.cast(function.foundStaleElementReferenceException); } throw e; - } finally { - changeImplicitlyWaitTimeOut(originalTimeOutDuration.getTime(), originalTimeOutDuration.getTimeUnit()); } } diff --git a/src/main/java/io/appium/java_client/pagefactory/AppiumElementLocatorFactory.java b/src/main/java/io/appium/java_client/pagefactory/AppiumElementLocatorFactory.java index 819bdb263..48c7ecb59 100644 --- a/src/main/java/io/appium/java_client/pagefactory/AppiumElementLocatorFactory.java +++ b/src/main/java/io/appium/java_client/pagefactory/AppiumElementLocatorFactory.java @@ -16,59 +16,57 @@ package io.appium.java_client.pagefactory; +import static java.util.Optional.ofNullable; + import io.appium.java_client.pagefactory.bys.builder.AppiumByBuilder; import io.appium.java_client.pagefactory.locator.CacheableElementLocatorFactory; import io.appium.java_client.pagefactory.locator.CacheableLocator; import org.openqa.selenium.By; import org.openqa.selenium.SearchContext; -import org.openqa.selenium.WebDriver; import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Field; +import javax.annotation.Nullable; public class AppiumElementLocatorFactory implements CacheableElementLocatorFactory { private final SearchContext searchContext; - private final TimeOutDuration timeOutDuration; - private final WebDriver originalWebDriver; + private final TimeOutDuration duration; private final AppiumByBuilder builder; /** * Creates a new mobile element locator factory. * * @param searchContext The context to use when finding the element - * @param timeOutDuration is a POJO which contains timeout parameters for the element to be searched - * @param originalWebDriver is an instance of WebDriver that is going to be used by a proxied element - * @param builder is handler of Appium-specific page object annotations + * @param duration is a POJO which contains timeout parameters for the element to be searched + * @param builder is handler of Appium-specific page object annotations */ - public AppiumElementLocatorFactory(SearchContext searchContext, TimeOutDuration timeOutDuration, - WebDriver originalWebDriver, AppiumByBuilder builder) { + public AppiumElementLocatorFactory(SearchContext searchContext, TimeOutDuration duration, + AppiumByBuilder builder) { this.searchContext = searchContext; - this.originalWebDriver = originalWebDriver; - this.timeOutDuration = timeOutDuration; + this.duration = duration; this.builder = builder; } - public CacheableLocator createLocator(Field field) { + public @Nullable CacheableLocator createLocator(Field field) { return this.createLocator((AnnotatedElement) field); } - @Override public CacheableLocator createLocator(AnnotatedElement annotatedElement) { + @Override public @Nullable CacheableLocator createLocator(AnnotatedElement annotatedElement) { TimeOutDuration customDuration; if (annotatedElement.isAnnotationPresent(WithTimeout.class)) { WithTimeout withTimeout = annotatedElement.getAnnotation(WithTimeout.class); customDuration = new TimeOutDuration(withTimeout.time(), withTimeout.unit()); } else { - customDuration = timeOutDuration; + customDuration = duration; } builder.setAnnotated(annotatedElement); - By by = builder.buildBy(); - if (by != null) { - return new AppiumElementLocator(searchContext, by, builder.isLookupCached(), - customDuration, timeOutDuration, originalWebDriver); - } - return null; + By byResult = builder.buildBy(); + + return ofNullable(byResult) + .map(by -> new AppiumElementLocator(searchContext, by, builder.isLookupCached(), customDuration)) + .orElse(null); } diff --git a/src/main/java/io/appium/java_client/pagefactory/AppiumFieldDecorator.java b/src/main/java/io/appium/java_client/pagefactory/AppiumFieldDecorator.java index bc866a6aa..9753736e9 100644 --- a/src/main/java/io/appium/java_client/pagefactory/AppiumFieldDecorator.java +++ b/src/main/java/io/appium/java_client/pagefactory/AppiumFieldDecorator.java @@ -62,20 +62,20 @@ public class AppiumFieldDecorator implements FieldDecorator { private static final List> availableElementClasses = ImmutableList.of(WebElement.class, RemoteWebElement.class, MobileElement.class, AndroidElement.class, IOSElement.class, WindowsElement.class); - public static long DEFAULT_IMPLICITLY_WAIT_TIMEOUT = 1; + public static long DEFAULT_TIMEOUT = 1; public static TimeUnit DEFAULT_TIMEUNIT = TimeUnit.SECONDS; private final WebDriver originalDriver; private final DefaultFieldDecorator defaultElementFieldDecoracor; private final AppiumElementLocatorFactory widgetLocatorFactory; private final String platform; private final String automation; - private final TimeOutDuration timeOutDuration; + private final TimeOutDuration duration; private final HasSessionDetails hasSessionDetails; - public AppiumFieldDecorator(SearchContext context, long implicitlyWaitTimeOut, + public AppiumFieldDecorator(SearchContext context, long timeout, TimeUnit timeUnit) { - this(context, new TimeOutDuration(implicitlyWaitTimeOut, timeUnit)); + this(context, new TimeOutDuration(timeout, timeUnit)); } /** @@ -84,9 +84,9 @@ public AppiumFieldDecorator(SearchContext context, long implicitlyWaitTimeOut, * or {@link org.openqa.selenium.WebElement} or * {@link io.appium.java_client.pagefactory.Widget} or some other user's * extension/implementation. - * @param timeOutDuration is a desired duration of the waiting for an element presence. + * @param duration is a desired duration of the waiting for an element presence. */ - public AppiumFieldDecorator(SearchContext context, TimeOutDuration timeOutDuration) { + public AppiumFieldDecorator(SearchContext context, TimeOutDuration duration) { this.originalDriver = unpackWebDriverFromSearchContext(context); if (originalDriver == null || !HasSessionDetails.class.isAssignableFrom(originalDriver.getClass())) { @@ -99,10 +99,10 @@ public AppiumFieldDecorator(SearchContext context, TimeOutDuration timeOutDurati automation = hasSessionDetails.getAutomationName(); } - this.timeOutDuration = timeOutDuration; + this.duration = duration; defaultElementFieldDecoracor = new DefaultFieldDecorator( - new AppiumElementLocatorFactory(context, timeOutDuration, originalDriver, + new AppiumElementLocatorFactory(context, duration, new DefaultElementByBuilder(platform, automation))) { @Override protected WebElement proxyForLocator(ClassLoader ignored, ElementLocator locator) { @@ -139,12 +139,11 @@ protected List proxyForListLocator(ClassLoader ignored, }; widgetLocatorFactory = - new AppiumElementLocatorFactory(context, timeOutDuration, originalDriver, - new WidgetByBuilder(platform, automation)); + new AppiumElementLocatorFactory(context, duration, new WidgetByBuilder(platform, automation)); } public AppiumFieldDecorator(SearchContext context) { - this(context, DEFAULT_IMPLICITLY_WAIT_TIMEOUT, DEFAULT_TIMEUNIT); + this(context, DEFAULT_TIMEOUT, DEFAULT_TIMEUNIT); } /** @@ -204,14 +203,14 @@ private Object decorateWidget(Field field) { if (isAlist) { return getEnhancedProxy(ArrayList.class, new WidgetListInterceptor(locator, originalDriver, map, widgetType, - timeOutDuration)); + duration)); } Constructor constructor = WidgetConstructorUtil.findConvenientConstructor(widgetType); return getEnhancedProxy(widgetType, new Class[] {constructor.getParameterTypes()[0]}, new Object[] {proxyForAnElement(locator)}, - new WidgetInterceptor(locator, originalDriver, null, map, timeOutDuration)); + new WidgetInterceptor(locator, originalDriver, null, map, duration)); } private WebElement proxyForAnElement(ElementLocator locator) { diff --git a/src/test/java/io/appium/java_client/ChromeDriverPathUtil.java b/src/test/java/io/appium/java_client/ChromeDriverPathUtil.java new file mode 100644 index 000000000..272f0da28 --- /dev/null +++ b/src/test/java/io/appium/java_client/ChromeDriverPathUtil.java @@ -0,0 +1,30 @@ +package io.appium.java_client; + +import static java.nio.file.FileSystems.getDefault; +import static org.openqa.selenium.Platform.MAC; +import static org.openqa.selenium.Platform.WINDOWS; +import static org.openqa.selenium.Platform.getCurrent; + +import org.openqa.selenium.Platform; + +import java.io.File; +import java.nio.file.Path; + +public final class ChromeDriverPathUtil { + private static final Path ROOT_TEST_PATH = getDefault().getPath("src") + .resolve("test").resolve("java").resolve("io").resolve("appium").resolve("java_client"); + + /** + * @return the choromedriver file which depends on platform. + */ + public static File getChromeDriver() { + Platform current = getCurrent(); + if (current.is(WINDOWS)) { + return ROOT_TEST_PATH.resolve("chromedriver.exe").toFile(); + } else if (current.is(MAC)) { + return ROOT_TEST_PATH.resolve("chromedriver_mac").toFile(); + } + + return ROOT_TEST_PATH.resolve("chromedriver_linux").toFile(); + } +} diff --git a/src/test/java/io/appium/java_client/chromedriver_linux b/src/test/java/io/appium/java_client/chromedriver_linux new file mode 100644 index 000000000..adba49a6f Binary files /dev/null and b/src/test/java/io/appium/java_client/chromedriver_linux differ diff --git a/src/test/java/io/appium/java_client/pagefactory_tests/chromedriver b/src/test/java/io/appium/java_client/chromedriver_mac old mode 100755 new mode 100644 similarity index 56% rename from src/test/java/io/appium/java_client/pagefactory_tests/chromedriver rename to src/test/java/io/appium/java_client/chromedriver_mac index 2e17cecf2..5428aa4de Binary files a/src/test/java/io/appium/java_client/pagefactory_tests/chromedriver and b/src/test/java/io/appium/java_client/chromedriver_mac differ diff --git a/src/test/java/io/appium/java_client/pagefactory_tests/DesktopBrowserCompatibilityTest.java b/src/test/java/io/appium/java_client/pagefactory_tests/DesktopBrowserCompatibilityTest.java index eb1f563e2..680657df4 100644 --- a/src/test/java/io/appium/java_client/pagefactory_tests/DesktopBrowserCompatibilityTest.java +++ b/src/test/java/io/appium/java_client/pagefactory_tests/DesktopBrowserCompatibilityTest.java @@ -16,7 +16,9 @@ package io.appium.java_client.pagefactory_tests; +import static io.appium.java_client.ChromeDriverPathUtil.getChromeDriver; import static io.appium.java_client.pagefactory.LocatorGroupStrategy.ALL_POSSIBLE; +import static java.lang.System.setProperty; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; @@ -27,7 +29,6 @@ import io.appium.java_client.pagefactory.iOSFindBy; import org.junit.BeforeClass; import org.junit.Test; -import org.openqa.selenium.Platform; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; @@ -42,8 +43,6 @@ public class DesktopBrowserCompatibilityTest { - - private static final Platform current = Platform.getCurrent(); @HowToUseLocators(iOSAutomation = ALL_POSSIBLE) @AndroidFindBy(className = "someClass") @iOSFindBy(xpath = "//selector[1]") @iOSFindBy(xpath = "//someTag") @@ -57,13 +56,8 @@ public class DesktopBrowserCompatibilityTest { * The starting. */ @BeforeClass public static void beforeClass() { - if (current.is(Platform.WINDOWS)) { - System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, - "src/test/java/io/appium/java_client/pagefactory_tests/chromedriver.exe"); - } else { - System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, - "src/test/java/io/appium/java_client/pagefactory_tests/chromedriver"); - } + setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, + getChromeDriver().getAbsolutePath()); } @Test public void chromeTest() { diff --git a/src/test/java/io/appium/java_client/pagefactory_tests/TimeOutResetTest.java b/src/test/java/io/appium/java_client/pagefactory_tests/TimeOutResetTest.java deleted file mode 100644 index c404d91a3..000000000 --- a/src/test/java/io/appium/java_client/pagefactory_tests/TimeOutResetTest.java +++ /dev/null @@ -1,155 +0,0 @@ -/* - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * See the NOTICE file distributed with this work for additional - * information regarding copyright ownership. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.appium.java_client.pagefactory_tests; - -import static org.junit.Assert.assertTrue; - -import io.appium.java_client.pagefactory.AppiumFieldDecorator; -import io.appium.java_client.pagefactory.TimeOutDuration; -import io.appium.java_client.pagefactory.WithTimeout; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.openqa.selenium.By; -import org.openqa.selenium.Platform; -import org.openqa.selenium.WebDriver; -import org.openqa.selenium.WebElement; -import org.openqa.selenium.chrome.ChromeDriver; -import org.openqa.selenium.chrome.ChromeDriverService; -import org.openqa.selenium.support.FindAll; -import org.openqa.selenium.support.FindBy; -import org.openqa.selenium.support.PageFactory; - -import java.util.Calendar; -import java.util.List; -import java.util.concurrent.TimeUnit; - -public class TimeOutResetTest { - private static final long ACCEPTABLE_DELTA_MILLS = 1500; - private WebDriver driver; - @FindAll({@FindBy(className = "ClassWhichDoesNotExist"), - @FindBy(className = "OneAnotherClassWhichDoesNotExist")}) private List - stubElements; - - @WithTimeout(time = 5, unit = TimeUnit.SECONDS) - @FindAll({@FindBy(className = "ClassWhichDoesNotExist"), - @FindBy(className = "OneAnotherClassWhichDoesNotExist")}) private List - stubElements2; - - private TimeOutDuration timeOutDuration; - - private static boolean checkTimeDifference(long expectedTime, TimeUnit expectedTimeUnit, - long currentMillis) { - long expectedMillis = TimeUnit.MILLISECONDS.convert(expectedTime, expectedTimeUnit); - try { - Assert.assertEquals(true, - ((currentMillis - expectedMillis) < ACCEPTABLE_DELTA_MILLS) && ( - (currentMillis - expectedMillis) >= 0)); - } catch (Error e) { - String message = String.valueOf(expectedTime) + " " - + expectedTimeUnit.toString() - + " current duration in millis " - + String.valueOf(currentMillis) + " Failed"; - throw new AssertionError(message, e); - } - return true; - } - - /** - * The setting up. - */ - @Before public void setUp() throws Exception { - if (Platform.getCurrent().is(Platform.WINDOWS)) { - System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, - "src/test/java/io/appium/java_client/pagefactory_tests/chromedriver.exe"); - } else { - System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, - "src/test/java/io/appium/java_client/pagefactory_tests/chromedriver"); - } - driver = new ChromeDriver(); - timeOutDuration = new TimeOutDuration(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT, - AppiumFieldDecorator.DEFAULT_TIMEUNIT); - PageFactory.initElements(new AppiumFieldDecorator(driver, timeOutDuration), this); - } - - /** - * finishing. - */ - @After public void tearDown() throws Exception { - driver.quit(); - } - - private long getBenchMark(List stubElements) { - long startMark = Calendar.getInstance().getTimeInMillis(); - stubElements.size(); - long endMark = Calendar.getInstance().getTimeInMillis(); - return endMark - startMark; - } - - @Test public void test() { - assertTrue(checkTimeDifference(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT, - AppiumFieldDecorator.DEFAULT_TIMEUNIT, getBenchMark(stubElements))); - System.out.println( - String.valueOf(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT) + " " - + AppiumFieldDecorator.DEFAULT_TIMEUNIT.toString() + ": Fine"); - - timeOutDuration.setTime(15500000, TimeUnit.MICROSECONDS); - assertTrue(checkTimeDifference(15500000, TimeUnit.MICROSECONDS, getBenchMark(stubElements))); - System.out.println( - "Change time: " + String.valueOf(15500000) + " " + TimeUnit.MICROSECONDS.toString() - + ": Fine"); - - timeOutDuration.setTime(3, TimeUnit.SECONDS); - assertTrue(checkTimeDifference(3, TimeUnit.SECONDS, getBenchMark(stubElements))); - System.out.println( - "Change time: " + String.valueOf(3) + " " + TimeUnit.SECONDS.toString() + ": Fine"); - - } - - @Test public void test2() { - assertTrue(checkTimeDifference(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT, - AppiumFieldDecorator.DEFAULT_TIMEUNIT, getBenchMark(stubElements))); - System.out.println( - String.valueOf(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT) + " " - + AppiumFieldDecorator.DEFAULT_TIMEUNIT.toString() + ": Fine"); - - assertTrue(checkTimeDifference(5, TimeUnit.SECONDS, getBenchMark(stubElements2))); - System.out.println(String.valueOf(5) + " " + TimeUnit.SECONDS.toString() + ": Fine"); - - - timeOutDuration.setTime(15500000, TimeUnit.MICROSECONDS); - assertTrue(checkTimeDifference(15500000, TimeUnit.MICROSECONDS, getBenchMark(stubElements))); - System.out.println( - "Change time: " + String.valueOf(15500000) + " " + TimeUnit.MICROSECONDS.toString() - + ": Fine"); - - assertTrue(checkTimeDifference(5, TimeUnit.SECONDS, getBenchMark(stubElements2))); - System.out.println(String.valueOf(5) + " " + TimeUnit.SECONDS.toString() + ": Fine"); - } - - @Test public void checkThatCustomizedTimeOutDoesNotChangeGeneralValue() { - stubElements2.size(); //time out differs from the general value there - - long startMark = Calendar.getInstance().getTimeInMillis(); - driver.findElements(By.id("FakeId")); - long endMark = Calendar.getInstance().getTimeInMillis(); - assertTrue(checkTimeDifference(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT, - AppiumFieldDecorator.DEFAULT_TIMEUNIT, endMark - startMark)); - } - -} diff --git a/src/test/java/io/appium/java_client/pagefactory_tests/TimeoutTest.java b/src/test/java/io/appium/java_client/pagefactory_tests/TimeoutTest.java new file mode 100644 index 000000000..286e46607 --- /dev/null +++ b/src/test/java/io/appium/java_client/pagefactory_tests/TimeoutTest.java @@ -0,0 +1,132 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.appium.java_client.pagefactory_tests; + +import static io.appium.java_client.ChromeDriverPathUtil.getChromeDriver; +import static io.appium.java_client.pagefactory.AppiumFieldDecorator.DEFAULT_TIMEOUT; +import static io.appium.java_client.pagefactory.AppiumFieldDecorator.DEFAULT_TIMEUNIT; +import static java.lang.Math.abs; +import static java.lang.String.format; +import static java.lang.System.currentTimeMillis; +import static java.lang.System.setProperty; +import static java.util.concurrent.TimeUnit.MICROSECONDS; +import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.hamcrest.Matchers.lessThanOrEqualTo; +import static org.junit.Assert.assertThat; +import static org.openqa.selenium.support.PageFactory.initElements; + +import io.appium.java_client.pagefactory.AppiumFieldDecorator; +import io.appium.java_client.pagefactory.TimeOutDuration; +import io.appium.java_client.pagefactory.WithTimeout; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.chrome.ChromeDriver; +import org.openqa.selenium.chrome.ChromeDriverService; +import org.openqa.selenium.support.FindAll; +import org.openqa.selenium.support.FindBy; + +import java.util.List; +import java.util.concurrent.TimeUnit; + +public class TimeoutTest { + + private static final long ACCEPTABLE_TIME_DIFF_MS = 1500; + private static final String MESSAGE = "Check difference from the expected waiting duration %s %s"; + + private WebDriver driver; + + @FindAll({ + @FindBy(className = "ClassWhichDoesNotExist"), + @FindBy(className = "OneAnotherClassWhichDoesNotExist")}) + private List stubElements; + + @WithTimeout(time = 5, unit = SECONDS) + @FindAll({@FindBy(className = "ClassWhichDoesNotExist"), + @FindBy(className = "OneAnotherClassWhichDoesNotExist")}) + private List stubElements2; + + private TimeOutDuration timeOutDuration; + + private static long getExpectedMillis(long value, TimeUnit sourceTimeUnit) { + return MILLISECONDS.convert(value, sourceTimeUnit); + } + + private static long getPerformanceDiff(long expectedMs, Runnable runnable) { + long startMark = currentTimeMillis(); + runnable.run(); + long endMark = currentTimeMillis(); + return abs(expectedMs - (endMark - startMark)); + } + + /** + * The setting up. + */ + @Before public void setUp() throws Exception { + setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, + getChromeDriver().getAbsolutePath()); + driver = new ChromeDriver(); + timeOutDuration = new TimeOutDuration(DEFAULT_TIMEOUT, DEFAULT_TIMEUNIT); + initElements(new AppiumFieldDecorator(driver, timeOutDuration), this); + } + + /** + * finishing. + */ + @After public void tearDown() throws Exception { + driver.quit(); + } + + @Test public void defaultTimeOutTest() { + assertThat(format(MESSAGE, DEFAULT_TIMEOUT, DEFAULT_TIMEUNIT), + getPerformanceDiff(getExpectedMillis(DEFAULT_TIMEOUT, DEFAULT_TIMEUNIT), () -> stubElements.size()), + lessThanOrEqualTo(ACCEPTABLE_TIME_DIFF_MS)); + + timeOutDuration.setTime(15500000, MICROSECONDS); + assertThat(format(MESSAGE, 15500000, MICROSECONDS), + getPerformanceDiff(getExpectedMillis(15500000, MICROSECONDS), () -> stubElements.size()), + lessThanOrEqualTo(ACCEPTABLE_TIME_DIFF_MS)); + + timeOutDuration.setTime(3, SECONDS); + assertThat(format(MESSAGE, 3, SECONDS), + getPerformanceDiff(getExpectedMillis(3, SECONDS), () -> stubElements.size()), + lessThanOrEqualTo(ACCEPTABLE_TIME_DIFF_MS)); + } + + @Test public void withCustomizedTimeOutTest() { + assertThat(format(MESSAGE, DEFAULT_TIMEOUT, DEFAULT_TIMEUNIT), + getPerformanceDiff(getExpectedMillis(DEFAULT_TIMEOUT, DEFAULT_TIMEUNIT), () -> stubElements.size()), + lessThanOrEqualTo(ACCEPTABLE_TIME_DIFF_MS)); + + assertThat(format(MESSAGE, 5, SECONDS), + getPerformanceDiff(getExpectedMillis(5, SECONDS), () -> stubElements2.size()), + lessThanOrEqualTo(ACCEPTABLE_TIME_DIFF_MS)); + + timeOutDuration.setTime(15500000, MICROSECONDS); + + assertThat(format(MESSAGE, 15500000, MICROSECONDS), + getPerformanceDiff(getExpectedMillis(15500000, MICROSECONDS), () -> stubElements.size()), + lessThanOrEqualTo(ACCEPTABLE_TIME_DIFF_MS)); + + assertThat(format(MESSAGE, 5, SECONDS), + getPerformanceDiff(getExpectedMillis(5, SECONDS), () -> stubElements2.size()), + lessThanOrEqualTo(ACCEPTABLE_TIME_DIFF_MS)); + } +} diff --git a/src/test/java/io/appium/java_client/pagefactory_tests/chromedriver.exe b/src/test/java/io/appium/java_client/pagefactory_tests/chromedriver.exe deleted file mode 100644 index c312e1bab..000000000 Binary files a/src/test/java/io/appium/java_client/pagefactory_tests/chromedriver.exe and /dev/null differ diff --git a/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java b/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java index ce2999faf..fa2a37db0 100644 --- a/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java +++ b/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java @@ -1,5 +1,6 @@ package io.appium.java_client.service.local; +import static io.appium.java_client.ChromeDriverPathUtil.getChromeDriver; import static io.appium.java_client.remote.AndroidMobileCapabilityType.APP_ACTIVITY; import static io.appium.java_client.remote.AndroidMobileCapabilityType.APP_PACKAGE; import static io.appium.java_client.remote.AndroidMobileCapabilityType.CHROMEDRIVER_EXECUTABLE; @@ -69,7 +70,7 @@ private static String getLocalIP(NetworkInterface intf) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { InetAddressValidator validator = InetAddressValidator.getInstance(); - String calculated = inetAddress.getHostAddress().toString(); + String calculated = inetAddress.getHostAddress(); if (validator.isValid(calculated)) { return calculated; } @@ -153,7 +154,7 @@ public void tearDown() throws Exception { @Test public void checkAbilityToStartServiceUsingCapabilities() throws Exception { File app = ROOT_TEST_PATH.resolve("ApiDemos-debug.apk").toFile(); - File chrome = ROOT_TEST_PATH.resolve("pagefactory_tests").resolve("chromedriver.exe").toFile(); + File chrome = getChromeDriver(); DesiredCapabilities caps = new DesiredCapabilities(); caps.setCapability(PLATFORM_NAME, "Android"); @@ -171,7 +172,7 @@ public void tearDown() throws Exception { @Test public void checkAbilityToStartServiceUsingCapabilitiesAndFlags() throws Exception { File app = ROOT_TEST_PATH.resolve("ApiDemos-debug.apk").toFile(); - File chrome = ROOT_TEST_PATH.resolve("pagefactory_tests").resolve("chromedriver.exe").toFile(); + File chrome = getChromeDriver(); DesiredCapabilities caps = new DesiredCapabilities(); caps.setCapability(PLATFORM_NAME, "Android"); diff --git a/src/test/java/io/appium/java_client/service/local/StartingAppLocallyTest.java b/src/test/java/io/appium/java_client/service/local/StartingAppLocallyTest.java index e8ebdd1ff..33139f0d7 100644 --- a/src/test/java/io/appium/java_client/service/local/StartingAppLocallyTest.java +++ b/src/test/java/io/appium/java_client/service/local/StartingAppLocallyTest.java @@ -16,6 +16,7 @@ package io.appium.java_client.service.local; +import static io.appium.java_client.ChromeDriverPathUtil.getChromeDriver; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; @@ -87,8 +88,7 @@ public class StartingAppLocallyTest { File appDir = new File("src/test/java/io/appium/java_client"); File app = new File(appDir, "ApiDemos-debug.apk"); - File pageFactoryDir = new File("src/test/java/io/appium/java_client/pagefactory_tests"); - File chrome = new File(pageFactoryDir, "chromedriver.exe"); + File chrome = getChromeDriver(); DesiredCapabilities serverCapabilities = new DesiredCapabilities(); serverCapabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");