Best io.appium code snippet using io.appium.java_client.android.AuthenticatesByFinger
pureDrivers.java
Source:pureDrivers.java
...1826 // ********************************************************************************************************************************************************1827 // AndroidDriver [147] = public default java.util.List<java.util.List<java.lang.Object>> io.appium.java_client.android.HasSupportedPerformanceDataType.getPerformanceData(java.lang.String,java.lang.String,int) throws java.lang.Exception1828 1829 // ********************************************************************************************************************************************************1830 // AndroidDriver [148] = public default void io.appium.java_client.android.AuthenticatesByFinger.fingerPrint(int)1831 1832 // ********************************************************************************************************************************************************1833 // AndroidDriver [149] = public default boolean io.appium.java_client.HasOnScreenKeyboard.isKeyboardShown()1834 public boolean isKeyboardShown() {1835 pureDriverDetails currentDriver = getCurrentDriverDetails();1836 return (boolean)pureCore.callMethod( currentDriver.mainDriver, currentDriver.mainDriver.getClass(), "isKeyboardShown", (Class<?>)null,1837 currentDriver.mainDriver.getClass().toString(), (Object)null );1838 }1839 1840 // ********************************************************************************************************************************************************1841 // AndroidDriver [150] = public default <T> java.lang.String io.appium.java_client.screenrecording.CanRecordScreen.startRecordingScreen(T)1842 1843 // ********************************************************************************************************************************************************1844 // AndroidDriver [151] = public default java.lang.String io.appium.java_client.screenrecording.CanRecordScreen.startRecordingScreen()...
Locomotive.java
Source:Locomotive.java
...7import io.appium.java_client.AppiumDriver;8import io.appium.java_client.MobileElement;9import io.appium.java_client.TouchAction;10import io.appium.java_client.android.AndroidDriver;11import io.appium.java_client.android.AuthenticatesByFinger;12import io.appium.java_client.ios.IOSDriver;13import io.appium.java_client.ios.PerformsTouchID;14import io.appium.java_client.remote.AndroidMobileCapabilityType;15import io.appium.java_client.remote.MobileCapabilityType;16import io.appium.java_client.service.local.AppiumServiceBuilder;17import io.appium.java_client.service.local.flags.GeneralServerFlag;18import org.apache.commons.lang3.StringUtils;19import org.junit.Assert;20import org.junit.Before;21import org.junit.Rule;22import org.junit.rules.TestName;23import org.junit.rules.TestRule;24import org.openqa.selenium.*;25import org.openqa.selenium.remote.DesiredCapabilities;26import org.openqa.selenium.support.ui.ExpectedCondition;27import org.openqa.selenium.support.ui.ExpectedConditions;28import org.openqa.selenium.support.ui.WebDriverWait;29import org.pmw.tinylog.Logger;30import org.testng.annotations.AfterMethod;31import org.testng.annotations.BeforeMethod;32import org.testng.annotations.Listeners;33import java.lang.reflect.Method;34import java.net.URL;35import java.util.HashMap;36import java.util.List;37import java.util.Map;38import java.util.regex.Matcher;39import java.util.regex.Pattern;40import static io.appium.java_client.touch.WaitOptions.waitOptions;41import static io.appium.java_client.touch.offset.ElementOption.element;42import static io.appium.java_client.touch.offset.PointOption.point;43import static java.time.Duration.ofMillis;44import static org.openqa.selenium.support.ui.ExpectedConditions.*;45/**46 * Created on 8/10/16.47 */48@Listeners({TestListener.class, SauceLabsListener.class})49public class Locomotive extends Watchman implements Conductor<Locomotive>, SauceOnDemandSessionIdProvider, SauceOnDemandAuthenticationProvider {50 private static final float SWIPE_DISTANCE = 0.25f;51 private static final float SWIPE_DISTANCE_LONG = 0.50f;52 private static final float SWIPE_DISTANCE_SUPER_LONG = 1.0f;53 private static final int SWIPE_DURATION_MILLIS = 2000;54 /**55 * ThreadLocal variable which contains the {@link AppiumDriver} instance which is used to perform interactions with.56 */57 private ThreadLocal<AppiumDriver> driver = new ThreadLocal<>();58 /**59 * ThreadLocal variable which contains the Sauce Job Id.60 */61 private ThreadLocal<String> sessionId = new ThreadLocal<>();62 /**63 * ThreadLocal variable which contains the Shutdown Hook instance for this Thread's Driver.64 * <p>65 * Registered just before Device creation, de-registered after 'quit' is called.66 */67 private ThreadLocal<Thread> shutdownHook = new ThreadLocal<>();68 public ConductorConfig configuration;69 private Map<String, String> vars = new HashMap<>();70 private String testMethodName;71 @Rule72 public TestRule watchman = this;73 @Rule74 public TestName testNameRule = new TestName();75 public Locomotive getLocomotive() {76 return this;77 }78 public Locomotive() {79 }80 public AppiumDriver getAppiumDriver() {81 return driver.get();82 }83 public Locomotive setAppiumDriver(AppiumDriver d) {84 registerShutdownHook();85 driver.set(d);86 return this;87 }88 /**89 * Creates the shutdownHook, or returns an existing copy90 */91 private Thread getShutdownHook() {92 if (shutdownHook.get() == null) {93 shutdownHook.set(new Thread(() -> {94 try {95 if (getAppiumDriver() != null) {96 getAppiumDriver().quit();97 }98 } catch (org.openqa.selenium.NoSuchSessionException ignored) {99 } // Don't care if session already closed100 }));101 }102 return shutdownHook.get();103 }104 /**105 * Registers the shutdownHook with the runtime.106 * <p>107 * Ignoring exceptions on registration; They mean the VM is already shutting down and it's too late.108 */109 private void registerShutdownHook() {110 // Register a hook to always close this session. Only works/needed once session is created.111 try {112 Runtime.getRuntime().addShutdownHook(getShutdownHook());113 } catch (IllegalStateException ignored) {114 // Thrown if a hook is added while shutting down; We don't care115 }116 }117 /**118 * De-registers the shutdownHook. This allows the GC to remove the thread and avoids double-quitting.119 * <p>120 * Silently swallows exceptions if the VM is already shutting down; it's too late.121 */122 private void deregisterShutdownHook() {123 if (shutdownHook.get() != null) {124 try {125 Runtime.getRuntime().removeShutdownHook(getShutdownHook());126 } catch (IllegalStateException ignored) {127 // VM already shutting down; Irrelevant128 }129 }130 }131 public Locomotive setConfiguration(ConductorConfig configuration) {132 this.configuration = configuration;133 return this;134 }135 @Before136 public void init() {137 // For jUnit get the method name from a test rule.138 this.testMethodName = testNameRule.getMethodName();139 initialize();140 }141 @BeforeMethod(alwaysRun = true)142 public void init(Method method) {143 // For testNG get the method name from an injected dependency.144 this.testMethodName = method.getName();145 initialize();146 }147 @AfterMethod(alwaysRun = true)148 public void quit() {149 if (getAppiumDriver() != null) {150 try {151 getAppiumDriver().quit();152 } catch (WebDriverException exception) {153 Logger.error(exception, "WebDriverException occurred during quit method");154 }155 }156 deregisterShutdownHook();157 }158 private void initialize() {159 if (this.configuration == null) {160 this.configuration = new ConductorConfig();161 }162 startAppiumSession(1);163 // Set session ID after driver has been initialized164 String id = getAppiumDriver().getSessionId().toString();165 sessionId.set(id);166 }167 void startAppiumSession(int startCounter) {168 if ((getAppiumDriver() != null) && (getAppiumDriver().getSessionId() != null)) {169 // session is already active -> terminal condition170 return;171 }172 if (startCounter > configuration.getStartSessionRetries()) {173 // maximum amount of retries reached174 throw new WebDriverException(175 "Could not start Appium Session with capabilities: " + getCapabilities(configuration).toString());176 }177 // start a new session178 try {179 URL hub = configuration.getHub();180 DesiredCapabilities capabilities = onCapabilitiesCreated(getCapabilities(configuration));181 AppiumServiceBuilder builder = new AppiumServiceBuilder()182 .withArgument(GeneralServerFlag.LOG_LEVEL, "warn");183 switch (configuration.getPlatformName()) {184 case ANDROID:185 setAppiumDriver(configuration.isLocal()186 ? new AndroidDriver(builder, capabilities)187 : new AndroidDriver(hub, capabilities));188 break;189 case IOS:190 setAppiumDriver(configuration.isLocal()191 ? new IOSDriver(builder, capabilities)192 : new IOSDriver(hub, capabilities));193 break;194 default:195 throw new IllegalArgumentException("Unknown platform: " + configuration.getPlatformName());196 }197 } catch (WebDriverException exception) {198 Logger.error(exception, "Received an exception while trying to start Appium session");199 }200 // recursive call to retry if necessary201 startAppiumSession(startCounter + 1);202 }203 protected DesiredCapabilities onCapabilitiesCreated(DesiredCapabilities desiredCapabilities) {204 return desiredCapabilities;205 }206 private DesiredCapabilities getCapabilities(ConductorConfig configuration) {207 DesiredCapabilities capabilities;208 switch (configuration.getPlatformName()) {209 case ANDROID:210 case IOS:211 capabilities = buildCapabilities(configuration);212 break;213 default:214 throw new IllegalArgumentException("Unknown platform: " + configuration.getPlatformName());215 }216 // If deviceName is empty replace it with something217 if (capabilities.getCapability(MobileCapabilityType.DEVICE_NAME).toString().isEmpty()) {218 capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Empty Device Name");219 }220 return capabilities;221 }222 public DesiredCapabilities buildCapabilities(ConductorConfig config) {223 DesiredCapabilities capabilities = new DesiredCapabilities();224 capabilities.setCapability(MobileCapabilityType.UDID, config.getUdid());225 capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, config.getDeviceName());226 capabilities.setCapability(MobileCapabilityType.APP, config.getFullAppPath());227 capabilities.setCapability(MobileCapabilityType.ORIENTATION, config.getOrientation());228 capabilities.setCapability("autoGrantPermissions", config.isAutoGrantPermissions());229 capabilities.setCapability(MobileCapabilityType.FULL_RESET, config.isFullReset());230 capabilities.setCapability(MobileCapabilityType.NO_RESET, config.getNoReset());231 capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, config.getPlatformVersion());232 capabilities.setCapability("xcodeSigningId", config.getXcodeSigningId());233 capabilities.setCapability("xcodeOrgId", config.getXcodeOrgId());234 capabilities.setCapability(AndroidMobileCapabilityType.AVD, config.getAvd());235 capabilities.setCapability(AndroidMobileCapabilityType.AVD_ARGS, config.getAvdArgs());236 capabilities.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, config.getAppActivity());237 capabilities.setCapability(AndroidMobileCapabilityType.APP_WAIT_ACTIVITY, config.getAppWaitActivity());238 capabilities.setCapability(AndroidMobileCapabilityType.INTENT_CATEGORY, config.getIntentCategory());239 capabilities.setCapability("sauceUserName", config.getSauceUserName());240 capabilities.setCapability("sauceAccessKey", config.getSauceAccessKey());241 capabilities.setCapability("waitForQuiescence", config.isWaitForQuiescence());242 capabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, config.getNewCommandTimeout());243 capabilities.setCapability("idleTimeout", config.getIdleTimeout());244 capabilities.setCapability("simpleIsVisibleCheck", config.isSimpleIsVisibleCheck());245 capabilities.setCapability(MobileCapabilityType.APPIUM_VERSION, config.getAppiumVersion());246 if (StringUtils.isNotEmpty(config.getAutomationName())) {247 capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, config.getAutomationName());248 }249 // Set custom capabilities if there are any250 for (String key : config.getCustomCapabilities().keySet()) {251 capabilities.setCapability(key, config.getCustomCapabilities().get(key));252 }253 return capabilities;254 }255 public Locomotive click(String id) {256 return click(PageUtil.buildBy(configuration, id));257 }258 public Locomotive click(By by) {259 TouchAction touchAction = new TouchAction(getAppiumDriver());260 touchAction.tap(element(getAppiumDriver().findElement(by)));261 touchAction.perform();262 return this;263 }264 public Locomotive click(MobileElement mobileElement) {265 try {266 TouchAction touchAction = new TouchAction(getAppiumDriver());267 touchAction.tap(element(mobileElement));268 touchAction.perform();269 return this;270 } catch (NoSuchElementException noSuchElementException) {271 throw new NoSuchElementException("Error: Unable to find element: " + mobileElement.toString() + " in order to click the element", noSuchElementException);272 }273 }274 public Locomotive click(WebElement webElement) {275 try {276 TouchAction touchAction = new TouchAction(getAppiumDriver());277 touchAction.tap(element(webElement));278 touchAction.perform();279 return this;280 } catch (NoSuchElementException noSuchElementException) {281 throw new NoSuchElementException("Error: Unable to find element: " + webElement.toString() + " in order to click the element", noSuchElementException);282 }283 }284 public Locomotive setText(String id, String text) {285 return setText(PageUtil.buildBy(configuration, id), text);286 }287 public Locomotive setText(By by, String text) {288 return setText(getAppiumDriver().findElement(by), text);289 }290 public Locomotive setText(MobileElement mobileElement, String text) {291 try {292 mobileElement.setValue(text);293 return this;294 } catch (NoSuchElementException noSuchElementException) {295 throw new NoSuchElementException("Error: Unable to find element: " + mobileElement.toString() + " in order to set the text of the element", noSuchElementException);296 }297 }298 public Locomotive setText(WebElement webElement, String text) {299 try {300 webElement.sendKeys(text);301 return this;302 } catch (NoSuchElementException noSuchElementException) {303 throw new NoSuchElementException("Error: Unable to find element: " + webElement.toString() + " in order to set the text of the element", noSuchElementException);304 }305 }306 public boolean isPresent(String id) {307 return isPresent(PageUtil.buildBy(configuration, id));308 }309 public boolean isPresent(By by) {310 return getAppiumDriver().findElements(by).size() > 0;311 }312 public boolean isPresent(MobileElement mobileElement) {313 return mobileElement.isDisplayed();314 }315 public boolean isPresentWait(String id) {316 return isPresentWait(PageUtil.buildBy(configuration, id), 5);317 }318 public boolean isPresentWait(By by) {319 return isPresentWait(by, 5);320 }321 public boolean isPresentWait(MobileElement element) {322 return isPresentWait(element, 5);323 }324 public boolean isPresentWait(String id, long timeOutInSeconds) {325 return isPresentWait(PageUtil.buildBy(configuration, id), timeOutInSeconds);326 }327 public boolean isPresentWait(By by, long timeOutInSeconds) {328 try {329 waitForCondition(elementToBeClickable(by), timeOutInSeconds, 200);330 return true;331 } catch (TimeoutException e) {332 return false;333 }334 }335 public boolean isPresentWait(MobileElement mobileElement, long timeOutInSeconds) {336 try {337 waitForCondition(elementToBeClickable(mobileElement), timeOutInSeconds, 200);338 return true;339 } catch (TimeoutException e) {340 return false;341 }342 }343 public String getText(String id) {344 return getText(PageUtil.buildBy(configuration, id));345 }346 public String getText(By by) {347 return getText(getAppiumDriver().findElement(by));348 }349 public String getText(WebElement webElement) {350 try {351 return webElement.getText();352 } catch (NoSuchElementException noSuchElementException) {353 throw new NoSuchElementException("Error: Unable to find element: " + webElement.toString() + " in order to get the text of the element", noSuchElementException);354 }355 }356 public String getText(MobileElement mobileElement) {357 try {358 return mobileElement.getText();359 } catch (NoSuchElementException noSuchElementException) {360 throw new NoSuchElementException("Error: Unable to find element: " + mobileElement.toString() + " in order to get the text of the element", noSuchElementException);361 }362 }363 public String getAttribute(String id, String attribute) {364 return getAttribute(PageUtil.buildBy(configuration, id), attribute);365 }366 public String getAttribute(By by, String attribute) {367 return getAppiumDriver().findElement(by).getAttribute(attribute);368 }369 public String getAttribute(MobileElement mobileElement, String attribute) {370 return getAttribute((WebElement) mobileElement, attribute);371 }372 public String getAttribute(WebElement webElement, String attribute) {373 try {374 return webElement.getAttribute(attribute);375 } catch (NoSuchElementException noSuchElementException) {376 throw new NoSuchElementException("Error: Unable to find element: " + webElement.toString() + " in order to get the attribute of the element", noSuchElementException);377 }378 }379 //region Generic swipes380 public Locomotive swipeCenter(SwipeElementDirection direction) {381 return swipe(direction, /*element=*/null, SWIPE_DISTANCE, 0);382 }383 public Locomotive swipeCenter(SwipeElementDirection direction, int swipeDurationInMillis) {384 return swipe(direction, /*element=*/null, SWIPE_DISTANCE, swipeDurationInMillis);385 }386 public Locomotive swipeCenter(SwipeElementDirection direction, int swipeDurationInMillis, int numberOfSwipes) {387 return repetitiveCenterSwipe(direction, swipeDurationInMillis, numberOfSwipes, SWIPE_DISTANCE);388 }389 public Locomotive swipe(SwipeElementDirection direction, String id, float percentage) {390 return swipe(direction, PageUtil.buildBy(configuration, id), percentage);391 }392 public Locomotive swipe(SwipeElementDirection direction, By by, float percentage) {393 return swipe(direction, (MobileElement) getAppiumDriver().findElement(by), percentage);394 }395 public Locomotive swipe(SwipeElementDirection direction, MobileElement mobileElement, float percentage) {396 return swipe(direction, (WebElement) mobileElement, percentage);397 }398 public Locomotive swipe(SwipeElementDirection direction, WebElement webElement, float percentage) {399 return swipe(direction, webElement, percentage, 0);400 }401 public Locomotive swipe(SwipeElementDirection direction, String id) {402 return swipe(direction, PageUtil.buildBy(configuration, id));403 }404 public Locomotive swipe(SwipeElementDirection direction, By by) {405 return swipe(direction, (MobileElement) getAppiumDriver().findElement(by));406 }407 public Locomotive swipe(SwipeElementDirection direction, MobileElement mobileElement) {408 return swipe(direction, (WebElement) mobileElement);409 }410 public Locomotive swipe(SwipeElementDirection direction, WebElement webElement) {411 return swipe(direction, webElement, SWIPE_DISTANCE, 0);412 }413 public Locomotive swipe(SwipeElementDirection direction, String id, int swipeDurationInMillis) {414 return swipe(direction, PageUtil.buildBy(configuration, id), swipeDurationInMillis);415 }416 public Locomotive swipe(SwipeElementDirection direction, By by, int swipeDurationInMillis) {417 return swipe(direction, (MobileElement) getAppiumDriver().findElement(by), swipeDurationInMillis);418 }419 public Locomotive swipe(SwipeElementDirection direction, MobileElement mobileElement, int swipeDurationInMillis) {420 return swipe(direction, (WebElement) mobileElement, swipeDurationInMillis);421 }422 public Locomotive swipe(SwipeElementDirection direction, WebElement webElement, int swipeDurationInMillis) {423 return swipe(direction, webElement, SWIPE_DISTANCE, swipeDurationInMillis);424 }425 public Locomotive swipeCenterLong(SwipeElementDirection direction) {426 return swipe(direction, /*element=*/null, SWIPE_DISTANCE_LONG, 0);427 }428 public Locomotive swipeCenterLong(SwipeElementDirection direction, int swipeDurationInMillis) {429 return swipe(direction, /*element=*/null, SWIPE_DISTANCE_LONG, swipeDurationInMillis);430 }431 public Locomotive swipeCenterLong(SwipeElementDirection direction, short numberOfSwipes) {432 return swipeCenterLong(direction, 0, numberOfSwipes);433 }434 public Locomotive swipeCenterLong(SwipeElementDirection direction, int swipeDurationInMillis, int numberOfSwipes) {435 return repetitiveCenterSwipe(direction, swipeDurationInMillis, numberOfSwipes, SWIPE_DISTANCE_LONG);436 }437 public Locomotive swipeCenterSuperLong(SwipeElementDirection direction) {438 return swipe(direction, /*element=*/null, SWIPE_DISTANCE_SUPER_LONG, 0);439 }440 public Locomotive swipeCenterSuperLong(SwipeElementDirection direction, int swipeDurationInMillis) {441 return swipe(direction, /*element=*/null, SWIPE_DISTANCE_SUPER_LONG, swipeDurationInMillis);442 }443 public Locomotive swipeCenterSuperLong(SwipeElementDirection direction, int swipeDurationInMillis, int numberOfSwipes) {444 return repetitiveCenterSwipe(direction, swipeDurationInMillis, numberOfSwipes, SWIPE_DISTANCE_SUPER_LONG);445 }446 public Locomotive swipeCornerLong(ScreenCorner corner, SwipeElementDirection direction, int duration) {447 return performCornerSwipe(corner, direction, SWIPE_DISTANCE_LONG, duration);448 }449 public Locomotive swipeCornerSuperLong(ScreenCorner corner, SwipeElementDirection direction, int duration) {450 return performCornerSwipe(corner, direction, SWIPE_DISTANCE_SUPER_LONG, duration);451 }452 public Locomotive swipeLong(SwipeElementDirection direction, String id) {453 return swipe(direction, PageUtil.buildBy(configuration, id));454 }455 public Locomotive swipeLong(SwipeElementDirection direction, By by) {456 return swipe(direction, (MobileElement) getAppiumDriver().findElement(by));457 }458 public Locomotive swipeLong(SwipeElementDirection direction, MobileElement mobileElement) {459 return swipeLong(direction, (WebElement) mobileElement);460 }461 public Locomotive swipeLong(SwipeElementDirection direction, WebElement element) {462 return swipe(direction, element, SWIPE_DISTANCE_LONG, 0);463 }464 public Locomotive swipeLong(SwipeElementDirection direction, String id, int swipeDurationInMillis) {465 return swipe(direction, PageUtil.buildBy(configuration, id), swipeDurationInMillis);466 }467 public Locomotive swipeLong(SwipeElementDirection direction, By by, int swipeDurationInMillis) {468 return swipe(direction, (MobileElement) getAppiumDriver().findElement(by), swipeDurationInMillis);469 }470 public Locomotive swipeLong(SwipeElementDirection direction, MobileElement element, int swipeDurationInMillis) {471 return swipe(direction, (WebElement) element, swipeDurationInMillis);472 }473 public Locomotive swipeLong(SwipeElementDirection direction, WebElement webElement, int swipeDurationInMillis) {474 return swipe(direction, webElement, SWIPE_DISTANCE_LONG, swipeDurationInMillis);475 }476 public Locomotive swipe(SwipeElementDirection direction, MobileElement mobileElement, float percentage, int swipeDurationInMillis) {477 return swipe(direction, (WebElement) mobileElement, percentage, swipeDurationInMillis);478 }479 public Locomotive swipe(SwipeElementDirection direction, WebElement webElement, float percentage, int swipeDurationInMillis) {480 return performSwipe(direction, false, webElement, percentage, swipeDurationInMillis);481 }482 public Locomotive swipe(Point start, Point end, int swipeDurationInMillis) {483 return performSwipe(false, start, end, swipeDurationInMillis);484 }485 public Locomotive swipe(Point start, Point end) {486 return swipe(start, end, 0);487 }488 public Locomotive repetitiveCenterSwipe(SwipeElementDirection direction, int swipeDurationInMillis, int numberOfSwipes, float swipeDistance) {489 int i = 0;490 while (i < numberOfSwipes) {491 swipe(direction, /*element=*/null, swipeDistance, swipeDurationInMillis);492 i++;493 }494 return this;495 }496 //endregion Generic swipes497 //region Directional swipes498 /***499 * @deprecated since 0.20.0, use {@link #scrollDown()} instead500 */501 @Deprecated502 public void swipeDown() {503 swipeCenterLong(SwipeElementDirection.UP);504 }505 /***506 * @deprecated since 0.20.0, use {@link #scrollDown()} instead507 */508 @Deprecated509 public void swipeDown(int times) {510 for (int i = 0; i < times; i++) {511 swipeCenterLong(SwipeElementDirection.UP);512 }513 }514 public void scrollDown() {515 swipeCenterLong(SwipeElementDirection.UP);516 }517 public void scrollDown(int times) {518 for (int i = 0; i < times; i++) {519 swipeCenterLong(SwipeElementDirection.UP);520 }521 }522 /***523 * @deprecated since 0.20.0, use {@link #scrollUp()} instead524 */525 @Deprecated526 public void swipeUp() {527 swipeCenterLong(SwipeElementDirection.DOWN);528 }529 /***530 * @deprecated since 0.20.0, use {@link #scrollUp()} instead531 */532 @Deprecated533 public void swipeUp(int times) {534 for (int i = 0; i < times; i++) {535 swipeCenterLong(SwipeElementDirection.DOWN);536 }537 }538 public void scrollUp() {539 swipeCenterLong(SwipeElementDirection.DOWN);540 }541 public void scrollUp(int times) {542 for (int i = 0; i < times; i++) {543 swipeCenterLong(SwipeElementDirection.DOWN);544 }545 }546 /***547 * @deprecated since 0.20.0, use {@link #scrollRight()} instead548 */549 @Deprecated550 public void swipeRight() {551 swipeCenterLong(SwipeElementDirection.LEFT);552 }553 /***554 * @deprecated since 0.20.0, use {@link #scrollRight()} instead555 */556 @Deprecated557 public void swipeRight(int times) {558 for (int i = 0; i < times; i++) {559 swipeCenterLong(SwipeElementDirection.LEFT);560 }561 }562 public void scrollRight() {563 swipeCenterLong(SwipeElementDirection.LEFT);564 }565 public void scrollRight(int times) {566 for (int i = 0; i < times; i++) {567 swipeCenterLong(SwipeElementDirection.LEFT);568 }569 }570 /***571 * @deprecated since 0.20.0, use {@link #scrollLeft()} instead572 */573 @Deprecated574 public void swipeLeft() {575 swipeCenterLong(SwipeElementDirection.RIGHT);576 }577 /***578 * @deprecated since 0.20.0, use {@link #scrollLeft()} instead579 */580 @Deprecated581 public void swipeLeft(int times) {582 for (int i = 0; i < times; i++) {583 swipeCenterLong(SwipeElementDirection.RIGHT);584 }585 }586 public void scrollLeft() {587 swipeCenterLong(SwipeElementDirection.RIGHT);588 }589 public void scrollLeft(int times) {590 for (int i = 0; i < times; i++) {591 swipeCenterLong(SwipeElementDirection.RIGHT);592 }593 }594 /***595 * Used to trigger iOS' gestural navigation back596 * As of 8/29/2019, Android does not have this feature, but it may in the future597 * More information on Android here: https://developer.android.com/preview/features/gesturalnav598 */599 public Locomotive swipeSystemBack() {600 Dimension screen = getAppiumDriver().manage().window().getSize();601 Point start = new Point(screen.getWidth() - 2, getYCenter());602 Point end = new Point(2, getYCenter());603 return swipe(start, end);604 }605 //endregion Directional swipes606 //region longPress swipes607 public Locomotive longPressSwipeCenter(SwipeElementDirection direction) {608 return longPressSwipe(direction, /*element=*/null, SWIPE_DISTANCE, 0);609 }610 public Locomotive longPressSwipeCenter(SwipeElementDirection direction, int swipeDurationInMillis) {611 return longPressSwipe(direction, /*element=*/null, SWIPE_DISTANCE, swipeDurationInMillis);612 }613 public Locomotive longPressSwipeCenter(SwipeElementDirection direction, int swipeDurationInMillis, short numberOfSwipes) {614 short i = 0;615 while (i < numberOfSwipes) {616 longPressSwipeCenter(direction, swipeDurationInMillis);617 i++;618 }619 return this;620 }621 public Locomotive longPressSwipe(SwipeElementDirection direction, String id) {622 return longPressSwipe(direction, PageUtil.buildBy(configuration, id));623 }624 public Locomotive longPressSwipe(SwipeElementDirection direction, By by) {625 return longPressSwipe(direction, (MobileElement) getAppiumDriver().findElement(by));626 }627 public Locomotive longPressSwipe(SwipeElementDirection direction, MobileElement mobileElement) {628 return longPressSwipe(direction, (WebElement) mobileElement);629 }630 public Locomotive longPressSwipe(SwipeElementDirection direction, WebElement webElement) {631 return longPressSwipe(direction, webElement, SWIPE_DISTANCE, 0);632 }633 public Locomotive longPressSwipe(SwipeElementDirection direction, String id, int swipeDurationInMillis) {634 return longPressSwipe(direction, PageUtil.buildBy(configuration, id), swipeDurationInMillis);635 }636 public Locomotive longPressSwipe(SwipeElementDirection direction, By by, int swipeDurationInMillis) {637 return longPressSwipe(direction, (MobileElement) getAppiumDriver().findElement(by), swipeDurationInMillis);638 }639 public Locomotive longPressSwipe(SwipeElementDirection direction, MobileElement mobileElement, int swipeDurationInMillis) {640 return longPressSwipe(direction, (WebElement) mobileElement, swipeDurationInMillis);641 }642 public Locomotive longPressSwipe(SwipeElementDirection direction, WebElement webElement, int swipeDurationInMillis) {643 return longPressSwipe(direction, webElement, SWIPE_DISTANCE, swipeDurationInMillis);644 }645 public Locomotive longPressSwipeCenterLong(SwipeElementDirection direction) {646 return longPressSwipe(direction, /*element=*/null, SWIPE_DISTANCE_LONG, 0);647 }648 public Locomotive longPressSwipeCenterLong(SwipeElementDirection direction, int swipeDurationInMillis) {649 return longPressSwipe(direction, /*element=*/null, SWIPE_DISTANCE_LONG, swipeDurationInMillis);650 }651 public Locomotive longPressSwipeCenterLong(SwipeElementDirection direction, int swipeDurationInMillis, short numberOfSwipes) {652 short i = 0;653 while (i < numberOfSwipes) {654 longPressSwipeCenterLong(direction, swipeDurationInMillis);655 i++;656 }657 return this;658 }659 public Locomotive longPressSwipeCenterSuperLong(SwipeElementDirection direction) {660 return longPressSwipe(direction, /*element=*/null, SWIPE_DISTANCE_SUPER_LONG, 0);661 }662 public Locomotive longPressSwipeCenterSuperLong(SwipeElementDirection direction, int swipeDurationInMillis) {663 return longPressSwipe(direction, /*element=*/null, SWIPE_DISTANCE_SUPER_LONG, swipeDurationInMillis);664 }665 public Locomotive longPressSwipeCenterSuperLong(SwipeElementDirection direction, int swipeDurationInMillis, short numberOfSwipes) {666 short i = 0;667 while (i < numberOfSwipes) {668 longPressSwipeCenterSuperLong(direction, swipeDurationInMillis);669 i++;670 }671 return this;672 }673 public Locomotive longPressSwipeLong(SwipeElementDirection direction, String id) {674 return longPressSwipe(direction, PageUtil.buildBy(configuration, id));675 }676 public Locomotive longPressSwipeLong(SwipeElementDirection direction, By by) {677 return longPressSwipe(direction, (MobileElement) getAppiumDriver().findElement(by));678 }679 public Locomotive longPressSwipeLong(SwipeElementDirection direction, MobileElement mobileElement) {680 return longPressSwipe(direction, (WebElement) mobileElement);681 }682 /**683 * It is preferred to pass in a MobileElement, hence this is deprecated.684 */685 @Deprecated686 public Locomotive longPressSwipeLong(SwipeElementDirection direction, WebElement webElement) {687 return longPressSwipe(direction, webElement, SWIPE_DISTANCE_LONG, 0);688 }689 public Locomotive longPressSwipeLong(SwipeElementDirection direction, String id, int swipeDurationInMillis) {690 return longPressSwipe(direction, PageUtil.buildBy(configuration, id), swipeDurationInMillis);691 }692 public Locomotive longPressSwipeLong(SwipeElementDirection direction, By by, int swipeDurationInMillis) {693 return longPressSwipe(direction, (MobileElement) getAppiumDriver().findElement(by), swipeDurationInMillis);694 }695 public Locomotive longPressSwipeLong(SwipeElementDirection direction, MobileElement mobileElement, int swipeDurationInMillis) {696 return longPressSwipeLong(direction, (WebElement) mobileElement, swipeDurationInMillis);697 }698 /**699 * It is preferred to pass in a MobileElement, hence this is deprecated.700 */701 @Deprecated702 public Locomotive longPressSwipeLong(SwipeElementDirection direction, WebElement webElement, int swipeDurationInMillis) {703 return longPressSwipe(direction, webElement, SWIPE_DISTANCE_LONG, swipeDurationInMillis);704 }705 public Locomotive longPressSwipe(SwipeElementDirection direction, MobileElement mobileElement, float percentage, int swipeDurationInMillis) {706 return longPressSwipe(direction, (WebElement) mobileElement, percentage, swipeDurationInMillis);707 }708 /**709 * It is preferred to pass in a MobileElement, hence this is deprecated.710 */711 @Deprecated712 public Locomotive longPressSwipe(SwipeElementDirection direction, WebElement webElement, float percentage, int swipeDurationInMillis) {713 return performSwipe(direction, true, webElement, percentage, swipeDurationInMillis);714 }715 public Locomotive longPressSwipe(Point start, Point end, int swipeDurationInMillis) {716 return performSwipe(true, start, end, swipeDurationInMillis);717 }718 public Locomotive longPressSwipe(Point start, Point end) {719 return longPressSwipe(start, end, 0);720 }721 //endregion longPress swipes722 public Locomotive hideKeyboard() {723 try {724 getAppiumDriver().hideKeyboard();725 } catch (WebDriverException e) {726 Logger.error(e, "WARN:" + e.getMessage());727 }728 return this;729 }730 /***731 * Generic Perform Swipe Method732 * Allows for different TouchAction presses: press() and longPress()733 * Can accept different start and end Points, by Appium's MobileElement, By, or Points734 *735 * @param direction - nullable, but required if `to` is null736 * @param isLongPress - determines TouchAction as `press()` or `longPress()`737 * @param from - origin point of swipe, not nullable738 * @param to - destination point of swipe, nullable, but required if `direction` is null739 * @param percentage - modifies destination X and Y depending on `direction`740 * @param swipeDurationInMillis - modifies duration of swipe741 ***/742 private Locomotive performSwipe(SwipeElementDirection direction, boolean isLongPress, Point from, Point to, float percentage, int swipeDurationInMillis) {743 Dimension screen = getAppiumDriver().manage().window().getSize();744 if (direction != null) {745 switch (direction) {746 case UP:747 int toYUp = (int) (from.getY() - (from.getY() * percentage));748 toYUp = toYUp <= 0 ? 1 : toYUp; // toYUp cannot be less than 0749 to = new Point(from.getX(), toYUp);750 break;751 case RIGHT:752 int toXRight = (int) (from.getX() + (screen.getWidth() * percentage));753 toXRight = toXRight >= screen.getWidth() ? screen.getWidth() - 1 : toXRight; // toXRight cannot be longer than screen width754 to = new Point(toXRight, from.getY());755 break;756 case DOWN:757 int toYDown = (int) (from.getY() + (screen.getHeight() * percentage));758 toYDown = toYDown >= screen.getHeight() ? screen.getHeight() - 1 : toYDown; // toYDown cannot be longer than screen height759 to = new Point(from.getX(), toYDown);760 break;761 case LEFT:762 int toXLeft = (int) (from.getX() - (from.getX() * percentage));763 toXLeft = toXLeft <= 0 ? 1 : toXLeft; // toXLeft cannot be less than 0764 to = new Point(toXLeft, from.getY());765 break;766 default:767 throw new IllegalArgumentException("Swipe Direction not specified: " + direction.name());768 }769 } else if (to == null) {770 throw new IllegalArgumentException("Swipe Direction and To Point are not specified.");771 }772 // Appium specifies that TouchAction.moveTo should be relative. iOS implements this correctly, but android773 // does not. As a result we have to check if we're on iOS and perform the relativization manually774 if (configuration.getPlatformName() == Platform.IOS) {775 to = new Point(to.getX() - from.getX(), to.getY() - from.getY());776 }777 int swipeDuration = (swipeDurationInMillis != 0) ? swipeDurationInMillis : SWIPE_DURATION_MILLIS;778 TouchAction swipe;779 if (!isLongPress) {780 swipe = new TouchAction(getAppiumDriver())781 .press(point(from.getX(), from.getY()))782 .waitAction(waitOptions(ofMillis(swipeDuration)))783 .moveTo(point(to.getX(), to.getY()))784 .release();785 } else {786 swipe = new TouchAction(getAppiumDriver())787 .longPress(point(from.getX(), from.getY()))788 .waitAction(waitOptions(ofMillis(swipeDuration)))789 .moveTo(point(to.getX(), to.getY()))790 .release();791 }792 swipe.perform();793 return this;794 }795 //Overload method for using custom Points796 private Locomotive performSwipe(boolean isLongPress, Point from, Point to, int swipeDurationInMillis) {797 return performSwipe(null, isLongPress, from, to, 0, swipeDurationInMillis);798 }799 //Overload method for using MobileElement, By, or String800 private Locomotive performSwipe(SwipeElementDirection direction, boolean isLongPress, MobileElement mobileElement, float percentage, int swipeDurationInMillis) {801 return performSwipe(direction, isLongPress, getCenter((WebElement) mobileElement), null, percentage, swipeDurationInMillis);802 }803 @Deprecated804 private Locomotive performSwipe(SwipeElementDirection direction, boolean isLongPress, WebElement webElement, float percentage, int swipeDurationInMillis) {805 return performSwipe(direction, isLongPress, getCenter(webElement), null, percentage, swipeDurationInMillis);806 }807 private Locomotive performCornerSwipe(ScreenCorner corner, SwipeElementDirection direction, float percentage, int duration) {808 Dimension screen = getAppiumDriver().manage().window().getSize();809 final int SCREEN_MARGIN = 10;810 Point from;811 if (corner != null) {812 switch (corner) {813 case TOP_LEFT:814 from = new Point(SCREEN_MARGIN, SCREEN_MARGIN);815 break;816 case TOP_RIGHT:817 from = new Point(screen.getWidth() - SCREEN_MARGIN, SCREEN_MARGIN);818 break;819 case BOTTOM_LEFT:820 from = new Point(SCREEN_MARGIN, screen.getHeight() - SCREEN_MARGIN);821 break;822 case BOTTOM_RIGHT:823 from = new Point(screen.getWidth() - SCREEN_MARGIN, screen.getHeight() - SCREEN_MARGIN);824 break;825 default:826 throw new IllegalArgumentException("Corner not specified: " + corner.name());827 }828 } else {829 throw new IllegalArgumentException("Corner not specified");830 }831 Point to;832 if (direction != null) {833 switch (direction) {834 case UP:835 int toYUp = (int) (from.getY() - (screen.getHeight() * percentage));836 toYUp = toYUp <= 0 ? 1 : toYUp;837 to = new Point(from.getX(), toYUp);838 break;839 case RIGHT:840 int toXRight = (int) (from.getX() + (screen.getWidth() * percentage));841 toXRight = toXRight >= screen.getWidth() ? screen.getWidth() - 1 : toXRight; // toXRight cannot be longer than screen width;842 to = new Point(toXRight, from.getY());843 break;844 case DOWN:845 int toYDown = (int) (from.getY() + (screen.getWidth() * percentage));846 toYDown = toYDown >= screen.getHeight() ? screen.getHeight() - 1 : toYDown; // toYDown cannot be longer than screen height;847 to = new Point(from.getX(), toYDown);848 break;849 case LEFT:850 int toXLeft = (int) (from.getX() - (screen.getWidth() * percentage));851 toXLeft = toXLeft <= 0 ? 1 : toXLeft; // toXLeft cannot be less than 0852 to = new Point(toXLeft, from.getY());853 break;854 default:855 throw new IllegalArgumentException("Swipe Direction not specified: " + direction.name());856 }857 } else {858 throw new IllegalArgumentException("Swipe Direction not specified");859 }860 // Appium specifies that TouchAction.moveTo should be relative. iOS implements this correctly, but android861 // does not. As a result we have to check if we're on iOS and perform the relativization manually862 if (configuration.getPlatformName() == Platform.IOS) {863 to = new Point(to.getX() - from.getX(), to.getY() - from.getY());864 }865 new TouchAction(getAppiumDriver())866 .press(point(from.getX(), from.getY()))867 .waitAction(waitOptions(ofMillis(duration)))868 .moveTo(point(to.getX(), to.getY()))869 .release()870 .perform();871 return this;872 }873 public MobileElement swipeTo(SwipeElementDirection direction, By by, int attempts) {874 MobileElement mobileElement;875 for (int i = 0; i < attempts; i++) {876 swipeCenterLong(direction);877 try {878 mobileElement = (MobileElement) getAppiumDriver().findElement(by);879 // element was found, check for visibility880 if (mobileElement.isDisplayed()) {881 // element is in view, exit the loop882 return mobileElement;883 }884 // element was not visible, continue scrolling885 } catch (WebDriverException exception) {886 // element could not be found, continue scrolling887 }888 }889 // element could not be found or was not visible, return null890 Logger.warn("Element " + by.toString() + " does not exist!");891 return null;892 }893 public MobileElement swipeTo(By by) {894 SwipeElementDirection s = SwipeElementDirection.UP;895 int attempts = 3;896 return swipeTo(s, by, attempts);897 }898 public MobileElement swipeTo(SwipeElementDirection direction, By by) {899 int attempts = 3;900 return swipeTo(direction, by, attempts);901 }902 public MobileElement swipeTo(SwipeElementDirection direction, String id, int attempts) {903 return swipeTo(direction, By.id(id), attempts);904 }905 /**906 * Get center point of element, if element is null return center of screen907 *908 * @param element The element to get the center point form909 * @return Point centered on the provided element or screen.910 */911 public Point getCenter(MobileElement element) {912 return getCenter((WebElement) element);913 }914 @Deprecated915 public Point getCenter(WebElement element) {916 return new Point(getXCenter(element), getYCenter(element));917 }918 public int getXCenter(MobileElement element) {919 return getXCenter((WebElement) element);920 }921 public int getXCenter() {922 return getXCenter(null);923 }924 @Deprecated925 public int getXCenter(WebElement element) {926 if (element == null) {927 return getAppiumDriver().manage().window().getSize().getWidth() / 2;928 } else {929 return element.getLocation().getX() + (element.getSize().getWidth() / 2);930 }931 }932 public int getYCenter(MobileElement element) {933 return getYCenter((WebElement) element);934 }935 public int getYCenter() {936 return getYCenter(null);937 }938 @Deprecated939 public int getYCenter(WebElement element) {940 if (element == null) {941 return getAppiumDriver().manage().window().getSize().getHeight() / 2;942 } else {943 return element.getLocation().getY() + (element.getSize().getHeight() / 2);944 }945 }946 public List<MobileElement> getElements(String id) {947 return getElements(PageUtil.buildBy(configuration, id));948 }949 public List<MobileElement> getElements(By by) {950 return getAppiumDriver().findElements(by);951 }952 /**953 * Validation Functions for Testing954 */955 public Locomotive validatePresent(String id) {956 return validatePresent(PageUtil.buildBy(configuration, id));957 }958 public Locomotive validatePresent(By by) {959 Assert.assertTrue("Element " + by.toString() + " does not exist!", isPresent(by));960 return this;961 }962 public Locomotive validateNotPresent(String id) {963 return validateNotPresent(PageUtil.buildBy(configuration, id));964 }965 public Locomotive validateNotPresent(By by) {966 Assert.assertFalse("Element " + by.toString() + " exists!", isPresent(by));967 return this;968 }969 public Locomotive validateText(String id, String text) {970 return validateText(PageUtil.buildBy(configuration, id), text);971 }972 public Locomotive validateTextIgnoreCase(String id, String text) {973 return validateTextIgnoreCase(PageUtil.buildBy(configuration, id), text);974 }975 public Locomotive validateTextIgnoreCase(By by, String text) {976 return validateTextIgnoreCase(getAppiumDriver().findElement(by), text);977 }978 public Locomotive validateTextIgnoreCase(MobileElement element, String text) {979 return validateTextIgnoreCase((WebElement) element, text);980 }981 @Deprecated982 public Locomotive validateTextIgnoreCase(WebElement element, String text) {983 String actual = getText(element);984 Assert.assertTrue(String.format("Text does not match! [expected: %s] [actual: %s]", text, actual),985 text.equalsIgnoreCase(actual));986 return this;987 }988 public Locomotive validateText(By by, String expected) {989 return validateText(getAppiumDriver().findElement(by), expected);990 }991 public Locomotive validateText(MobileElement mobileElement, String expected) {992 return validateText((WebElement) mobileElement, expected);993 }994 @Deprecated995 public Locomotive validateText(WebElement webElement, String expected) {996 String actual = getText(webElement);997 Assert.assertEquals(String.format("Text does not match! [expected: %s] [actual: %s]", expected, actual), expected, actual);998 return this;999 }1000 public Locomotive validateTextNot(String id, String text) {1001 return validateTextNot(PageUtil.buildBy(configuration, id), text);1002 }1003 public Locomotive validateTextNotIgnoreCase(String id, String text) {1004 return validateTextNotIgnoreCase(PageUtil.buildBy(configuration, id), text);1005 }1006 public Locomotive validateTextNotIgnoreCase(By by, String text) {1007 return validateTextNotIgnoreCase(getAppiumDriver().findElement(by), text);1008 }1009 public Locomotive validateTextNotIgnoreCase(MobileElement element, String text) {1010 return validateTextNotIgnoreCase((WebElement) element, text);1011 }1012 @Deprecated1013 public Locomotive validateTextNotIgnoreCase(WebElement element, String text) {1014 String actual = getText(element);1015 Assert.assertFalse(String.format("Text matches! [expected: %s] [actual: %s]", text, actual),1016 text.equalsIgnoreCase(actual));1017 return this;1018 }1019 public Locomotive validateTextNot(By by, String text) {1020 return validateTextNot(getAppiumDriver().findElement(by), text);1021 }1022 public Locomotive validateTextNot(MobileElement element, String text) {1023 return validateTextNot((WebElement) element, text);1024 }1025 @Deprecated1026 public Locomotive validateTextNot(WebElement element, String unexpected) {1027 String actual = getText(element);1028 Assert.assertNotEquals(String.format("Text matches! [expected: %s] [actual: %s]", unexpected, actual), unexpected, actual);1029 return this;1030 }1031 public Locomotive validateTextPresent(String text) {1032 Assert.assertTrue(getAppiumDriver().getPageSource().contains(text));1033 return this;1034 }1035 public Locomotive validateTextNotPresent(String text) {1036 Assert.assertFalse(getAppiumDriver().getPageSource().contains(text));1037 return this;1038 }1039 public Locomotive validateAttribute(String id, String attr, String expected) {1040 return validateAttribute(PageUtil.buildBy(configuration, id), attr, expected);1041 }1042 public Locomotive validateAttribute(By by, String attr, String expected) {1043 return validateAttribute(getAppiumDriver().findElement(by), attr, expected);1044 }1045 public Locomotive validateAttribute(MobileElement element, String attr, String expected) {1046 return validateAttribute((WebElement) element, attr, expected);1047 }1048 /**1049 * It is preferred to use a MobileElement, hence this is deprecated.1050 */1051 @Deprecated1052 public Locomotive validateAttribute(WebElement element, String attr, String expected) {1053 String actual = null;1054 try {1055 actual = element.getAttribute(attr);1056 if (actual.equals(expected)) return this; // test passes.1057 } catch (NoSuchElementException e) {1058 Assert.fail("No such element [" + element.toString() + "] exists.");1059 } catch (Exception x) {1060 Assert.fail("Cannot validate an attribute if an element doesn't have it!");1061 }1062 Pattern p = Pattern.compile(expected);1063 Matcher m = p.matcher(actual);1064 Assert.assertTrue(1065 String.format("Attribute doesn't match! [Selector: %s] [Attribute: %s] [Desired value: %s] [Actual value: %s]",1066 element.toString(),1067 attr,1068 expected,1069 actual1070 ),1071 m.find());1072 return this;1073 }1074 public Locomotive validateTrue(boolean condition) {1075 Assert.assertTrue(condition);1076 return this;1077 }1078 public Locomotive validateFalse(boolean condition) {1079 Assert.assertFalse(condition);1080 return this;1081 }1082 public Locomotive store(String key, String value) {1083 vars.put(key, value);1084 return this;1085 }1086 public String get(String key) {1087 return get(key, null);1088 }1089 public String get(String key, String defaultValue) {1090 return Strings.isNullOrEmpty(vars.get(key))1091 ? defaultValue1092 : vars.get(key);1093 }1094 /**1095 * Enroll biometrics. This command is ignored on Android.1096 *1097 * @return The implementing class for fluency1098 */1099 public Locomotive enrollBiometrics(int id) {1100 switch (configuration.getPlatformName()) {1101 case ANDROID:1102 // Don't do anything for now1103 break;1104 case IOS:1105 PerformsTouchID performsTouchID = (PerformsTouchID) driver;1106 performsTouchID.toggleTouchIDEnrollment(true);1107 break;1108 case NONE:1109 break;1110 }1111 return this;1112 }1113 /**1114 * Perform a biometric scan, either forcing a match (iOS) or by supplying the id of an enrolled1115 * fingerprint (Android)1116 *1117 * @param match Whether or not the finger should match. This parameter is ignored on Android1118 * @param id The id of the enrolled finger. This parameter is ignored on iOS1119 * @return The implementing class for fluency1120 */1121 public Locomotive performBiometric(boolean match, int id) {1122 switch (configuration.getPlatformName()) {1123 case ANDROID:1124 //Note: Biometrics are only supported on Android APIs 23 and above (6.0)1125 AuthenticatesByFinger authenticatesByFinger = (AuthenticatesByFinger) getAppiumDriver();1126 authenticatesByFinger.fingerPrint(id);1127 break;1128 case IOS:1129 PerformsTouchID performsTouchID = (PerformsTouchID) getAppiumDriver();1130 performsTouchID.performTouchID(match);1131 break;1132 case NONE:1133 break;1134 }1135 return this;1136 }1137 /**1138 * Wait for a specific condition (polling every 1s, for MAX_TIMEOUT seconds)1139 *...
AndroidDriver.java
Source:AndroidDriver.java
...58 extends AppiumDriver<T>59 implements PressesKey, HasNetworkConnection, PushesFiles, StartsActivity,60 FindsByAndroidUIAutomator<T>, FindsByAndroidViewTag<T>, FindsByAndroidDataMatcher<T>,61 FindsByAndroidViewMatcher<T>, LocksDevice, HasAndroidSettings, HasAndroidDeviceDetails,62 HasSupportedPerformanceDataType, AuthenticatesByFinger, HasOnScreenKeyboard,63 CanRecordScreen, SupportsSpecialEmulatorCommands,64 SupportsNetworkStateManagement, ListensToLogcatMessages, HasAndroidClipboard,65 HasBattery<AndroidBatteryInfo>, ExecuteCDPCommand {66 private static final String ANDROID_PLATFORM = MobilePlatform.ANDROID;67 private StringWebSocketClient logcatClient;68 /**69 * Creates a new instance based on command {@code executor} and {@code capabilities}.70 *71 * @param executor is an instance of {@link HttpCommandExecutor}72 * or class that extends it. Default commands or another vendor-specific73 * commands may be specified there.74 * @param capabilities take a look at {@link Capabilities}75 */76 public AndroidDriver(HttpCommandExecutor executor, Capabilities capabilities) {...
MobileDevice.java
Source:MobileDevice.java
2import io.appium.java_client.AppiumDriver;3import io.appium.java_client.LocksDevice;4import io.appium.java_client.MobileDriver;5import io.appium.java_client.android.AndroidDriver;6import io.appium.java_client.android.AuthenticatesByFinger;7import io.appium.java_client.battery.BatteryInfo;8import io.appium.java_client.ios.IOSDriver;9import io.appium.java_client.ios.PerformsTouchID;10import io.appium.java_client.ios.ShakesDevice;11import org.openqa.selenium.DeviceRotation;12import org.openqa.selenium.ScreenOrientation;13import org.openqa.selenium.WebDriver;14import org.openqa.selenium.html5.Location;15import java.time.Duration;16import java.util.function.Consumer;17import java.util.function.Function;18import static com.epam.jdi.light.common.Exceptions.exception;19import static com.epam.jdi.light.driver.WebDriverFactory.getDriver;20import static com.epam.jdi.light.mobile.MobileUtils.executeDriverMethod;21public class MobileDevice {22 public static void rotate(DeviceRotation rotation) {23 executeDriverMethod(AppiumDriver.class, (AppiumDriver driver) -> driver.rotate(rotation));24 }25 public static DeviceRotation getRotation() {26 return executeDriverMethod(AppiumDriver.class,27 (Function<AppiumDriver, DeviceRotation>) AppiumDriver::rotation);28 }29 public static void rotate(ScreenOrientation orientation) {30 executeDriverMethod(AppiumDriver.class, (AppiumDriver driver) -> driver.rotate(orientation));31 }32 public static ScreenOrientation getOrientation() {33 return executeDriverMethod(AppiumDriver.class,34 (Function<AppiumDriver, ScreenOrientation>) AppiumDriver::getOrientation);35 }36 public static void lockDevice() {37 executeDriverMethod(LocksDevice.class, (Consumer<LocksDevice>) LocksDevice::lockDevice);38 }39 public static void lockDevice(Duration duration) {40 executeDriverMethod(LocksDevice.class, (LocksDevice driver) -> driver.lockDevice(duration));41 }42 public static void unlockDevice() {43 executeDriverMethod(LocksDevice.class, LocksDevice::unlockDevice);44 }45 public static boolean isLocked() {46 return executeDriverMethod(LocksDevice.class, LocksDevice::isDeviceLocked);47 }48 public static BatteryInfo getBatteryInfo() {49 WebDriver driver = getDriver();50 if (driver instanceof IOSDriver) {51 return ((IOSDriver) driver).getBatteryInfo();52 } else if (driver instanceof AndroidDriver) {53 return ((AndroidDriver) driver).getBatteryInfo();54 } else {55 throw exception("This method is not supported by the driver. The driver needs to be the instance of either Ios or Android driver");56 }57 }58 public static Location getLocation() {59 return executeDriverMethod(AppiumDriver.class, (Function<AppiumDriver, Location>) AppiumDriver::location);60 }61 public static void setLocation(Location location) {62 executeDriverMethod(AppiumDriver.class, (AppiumDriver driver) -> driver.setLocation(location));63 }64 public static String getDeviceTime() {65 return executeDriverMethod(MobileDriver.class, (Function<MobileDriver, String>) MobileDriver::getDeviceTime);66 }67 public static String getDeviceTime(String format) {68 return executeDriverMethod(MobileDriver.class, (MobileDriver driver) -> driver.getDeviceTime(format));69 }70 // the next methods are for IOS only71 public static void shake() {72 executeDriverMethod(ShakesDevice.class, ShakesDevice::shake);73 }74 public static void performTouchId(boolean match) {75 executeDriverMethod(PerformsTouchID.class, (PerformsTouchID driver) -> driver.performTouchID(match));76 }77 public static void toggleTouchIDEnrollment(boolean enabled) {78 executeDriverMethod(PerformsTouchID.class, (PerformsTouchID driver) -> driver.toggleTouchIDEnrollment(enabled));79 }80 // the next methods are for Android only81 public static void fingerPrint(int fingerPrintId) {82 executeDriverMethod(AuthenticatesByFinger.class, (AuthenticatesByFinger driver) -> driver.fingerPrint(fingerPrintId));83 }84}...
AuthenticatesByFinger.java
Source:AuthenticatesByFinger.java
1package io.appium.java_client.android;2import static io.appium.java_client.android.AndroidMobileCommandHelper.fingerPrintCommand;3import io.appium.java_client.CommandExecutionHelper;4import io.appium.java_client.ExecutesMethod;5public interface AuthenticatesByFinger extends ExecutesMethod {6 /**7 * Authenticate users by using their finger print scans on supported emulators.8 *9 * @param fingerPrintId finger prints stored in Android Keystore system (from 1 to 10)10 */11 default void fingerPrint(int fingerPrintId) {12 CommandExecutionHelper.execute(this, fingerPrintCommand(fingerPrintId));13 }14}
AuthenticatesByFinger
Using AI Code Generation
1import io.appium.java_client.android.AndroidDriver;2import io.appium.java_client.android.AndroidKeyCode;3import io.appium.java_client.android.AndroidElement;4import io.appium.java_client.android.nativekey.AndroidKey;5import io.appium.java_client.android.nativekey.KeyEvent;6import io.appium.java_client.android.nativekey.PressesKey;7import java.net.MalformedURLException;8import java.net.URL;9import java.util.concurrent.TimeUnit;10import org.openqa.selenium.By;11import org.openqa.selenium.Keys;12import org.openqa.selenium.WebDriver;13import org.openqa.selenium.WebElement;14import org.openqa.selenium.remote.DesiredCapabilities;15import org.openqa.selenium.support.ui.ExpectedConditions;16import org.openqa.selenium.support.ui.WebDriverWait;17public class Appium {18 public static void main(String[] args) throws MalformedURLException, InterruptedException {19 DesiredCapabilities capabilities = new DesiredCapabilities();20 capabilities.setCapability("deviceName", "Nexus 5");21 capabilities.setCapability("platformName", "Android");22 capabilities.setCapability("platformVersion", "5.1.1");23 capabilities.setCapability("appPackage", "com.android.settings");24 capabilities.setCapability("appActivity", ".Settings");
AuthenticatesByFinger
Using AI Code Generation
1package appium;2import io.appium.java_client.android.AndroidDriver;3import io.appium.java_client.android.AndroidElement;4import io.appium.java_client.remote.MobileCapabilityType;5import java.net.MalformedURLException;6import java.net.URL;7import java.util.concurrent.TimeUnit;8import org.openqa.selenium.remote.DesiredCapabilities;9public class AuthenticatesByFinger {10public static void main(String[] args) throws MalformedURLException, InterruptedException {11DesiredCapabilities dc = new DesiredCapabilities();12dc.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Device");13dc.setCapability(MobileCapabilityType.APP, System.getProperty("user.dir")+"\\src\\apps\\ApiDemos-debug.apk");
AuthenticatesByFinger
Using AI Code Generation
1import io.appium.java_client.android.AndroidDriver;2import io.appium.java_client.android.AndroidElement;3import io.appium.java_client.android.nativekey.AndroidKey;4import io.appium.java_client.android.nativekey.KeyEvent;5import io.appium.java_client.android.nativekey.PressesKey;6import io.appium.java_client.android.nativekey.PressesKeyCode;7import io.appium.java_client.android.nativekey.KeyEventFlag;8import io.appium.java_client.android.nativekey.PressesKey;9import io.appium.java_client.android.nativekey.PressesKeyCode;10import io.appium.java_client.android.nativekey.KeyEventFlag;11import io.appium.java_client.android.nativekey.KeyEventFlag;12import io.appium.java_client.android.nativekey.PressesKey;13import io.appium.java_client.android.nativekey.PressesKeyCode;14import io.appium.java_client.android.nativekey.KeyEventFlag;15import io.appium.java_client.android.nativekey.KeyEventFlag;16public class Appium {17public static void main(String[] args) throws MalformedURLException, InterruptedException {18DesiredCapabilities caps = new DesiredCapabilities();19caps.setCapability("deviceName", "Pixel_3a_API_30_x86");20caps.setCapability("platformName", "Android");21caps.setCapability("platformVersion", "11.0");22caps.setCapability("skipUnlock", "true");23caps.setCapability("appPackage", "com.google.android.apps.authenticator2");24caps.setCapability("appActivity", "com.google.android.apps.authenticator.AuthenticatorActivity");25caps.setCapability("noReset", "true");
AuthenticatesByFinger
Using AI Code Generation
1package appium;2import io.appium.java_client.android.AndroidDriver;3import io.appium.java_client.android.AuthenticatesByFinger;4import io.appium.java_client.AppiumDriver;5import io.appium.java_client.MobileElement;6import io.appium.java_client.TouchAction;7import io.appium.java_client.android.AndroidDriver;8import io.appium.java_client.android.AndroidKeyCode;9import io.appium.java_client.android.AndroidTouchAction;10import io.appium.java_client.android.Connection;11import io.appium.java_client.android.HasNetworkConnection;12import io.appium.java_client.android.HasOnScreenButtons;13import io.appium.java_client.android.LocksDevice;14import io.appium.java_client.android.StartsActivity;15import io.appium.java_client.android.nativekey.AndroidKey;16import io.appium.java_client.android.nativekey.KeyEvent;17import io.appium.java
AuthenticatesByFinger
Using AI Code Generation
1import io.appium.java_client.android.AndroidDriver;2import io.appium.java_client.android.AndroidElement;3import io.appium.java_client.remote.MobileCapabilityType;4import org.openqa.selenium.By;5import org.openqa.selenium.WebElement;6import org.openqa.selenium.remote.DesiredCapabilities;7import org.testng.annotations.AfterTest;8import org.testng.annotations.BeforeTest;9import org.testng.annotations.Test;10import java.io.File;11import java.io.IOException;12import java.net.URL;13import java.util.concurrent.TimeUnit;14public class FingerPrint {15 public static AndroidDriver<AndroidElement> driver;16 public void setUp() throws IOException {17 DesiredCapabilities capabilities = new DesiredCapabilities();18 capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");19 capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");20 capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "8.1");21 capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");22 capabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, "100");23 capabilities.setCapability(MobileCapabilityType.APP, (new File("E:\\Appium\\ApiDemos-debug.apk")).getAbsolutePath());
AuthenticatesByFinger
Using AI Code Generation
1package com.appium;2import java.net.MalformedURLException;3import java.net.URL;4import java.util.concurrent.TimeUnit;5import org.openqa.selenium.By;6import org.openqa.selenium.WebElement;7import org.openqa.selenium.remote.DesiredCapabilities;8import org.testng.annotations.Test;9import io.appium.java_client.android.AndroidDriver;10import io.appium.java_client.android.AndroidElement;11import io.appium.java_client.android.nativekey.AndroidKey;12import io.appium.java_client.android.nativekey.KeyEvent;13public class FingerPrintAuth {14 public void FingerPrintTest() throws MalformedURLException, InterruptedException {15 DesiredCapabilities cap= new DesiredCapabilities();16 cap.setCapability("deviceName", "Android Device");17 cap.setCapability("udid", "emulator-5554");18 cap.setCapability("platformName", "Android");19 cap.setCapability("platformVersion", "10.0");20 cap.setCapability("appPackage", "com.android.settings");21 cap.setCapability("appActivity", "com.android.settings.Settings");22 cap.setCapability("noReset", "true");23 cap.setCapability("automationName", "UiAutomator2");24 cap.setCapability("autoGrantPermissions", "true");25 cap.setCapability("autoAcceptAlerts", "true");26 cap.setCapability("autoDismissAlerts", "true");
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!