Best Carina code snippet using com.qaprosoft.carina.core.foundation.webdriver.device.Device.AdbExecutor
Source:AndroidUtils.java
...13import org.openqa.selenium.support.ui.ExpectedConditions;14import org.openqa.selenium.support.ui.WebDriverWait;15import com.qaprosoft.carina.core.foundation.utils.Configuration;16import com.qaprosoft.carina.core.foundation.utils.Configuration.Parameter;17import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.AdbExecutor;18import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine;19import com.qaprosoft.carina.core.foundation.webdriver.DriverPool;20import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedWebElement;21import com.qaprosoft.carina.core.foundation.webdriver.device.DevicePool;22import io.appium.java_client.MobileBy;23import io.appium.java_client.MobileDriver;24import io.appium.java_client.MobileElement;25import io.appium.java_client.PressesKeyCode;26import io.appium.java_client.SwipeElementDirection;27import io.appium.java_client.TouchAction;28import io.appium.java_client.android.AndroidDeviceActionShortcuts;29import io.appium.java_client.android.AndroidDriver;30import io.appium.java_client.android.AndroidElement;31public class AndroidUtils {32 protected static final Logger LOGGER = Logger.getLogger(AndroidUtils.class);33 protected static final long IMPLICIT_TIMEOUT = Configuration34 .getLong(Parameter.IMPLICIT_TIMEOUT);35 protected static final long EXPLICIT_TIMEOUT = Configuration36 .getLong(Parameter.EXPLICIT_TIMEOUT);37 public static final int MINIMUM_TIMEOUT = 2;38 public static final int DEFAULT_SWIPE_TIMEOUT = 1000;39 public enum Direction {40 LEFT, RIGHT, UP, DOWN, VERTICAL, VERTICAL_DOWN_FIRST, HORIZONTAL, HORIZONTAL_RIGHT_FIRST41 }42 /**43 * Useful Android utilities. For usage: import44 * com.qaprosoft.carina.core.foundation.utils.android.AndroidUtils;45 *46 */47 /**48 * execute Key Event49 *50 * @param keyCode int51 */52 public static void executeKeyEvent(int keyCode) {53 WebDriver driver = DriverPool.getDriver();54 LOGGER.info("Execute key event: " + keyCode);55 HashMap<String, Integer> keyCodeMap = new HashMap<String, Integer>();56 keyCodeMap.put("keycode", keyCode);57 ((JavascriptExecutor) driver).executeScript("mobile: keyevent",58 keyCodeMap);59 }60 /**61 * press Key Code62 *63 * @param keyCode int64 * @return boolean65 */66 public static boolean pressKeyCode(int keyCode) {67 try {68 LOGGER.info("Press key code: " + keyCode);69 ((PressesKeyCode) DriverPool.getDriver()).pressKeyCode(keyCode);70 return true;71 } catch (Exception e) {72 LOGGER.error("Exception during pressKeyCode:", e);73 try {74 LOGGER.info("Press key code by old method: " + keyCode);75 ((AndroidDeviceActionShortcuts) DriverPool.getDriver())76 .pressKeyCode(keyCode);77 return true;78 } catch (Exception err) {79 LOGGER.error("Exception during pressKeyCode with old method:", err);80 try {81 LOGGER.info("Press key code by javaScript: " + keyCode);82 executeKeyEvent(keyCode);83 } catch (Exception err2) {84 LOGGER.error("Exception during pressKeyCode with JavaScript:", err2);85 }86 }87 }88 return false;89 }90 /**91 * scrollTo specified text92 *93 * @param text - String94 * @return boolean95 */96 @Deprecated97 public static boolean scrollTo(final String text) {98 boolean scrolled = false;99 int repeat = 1;100 int tries = 3;101 // TODO: investigate how to:102 // AndroidUIAutomator("setMaxSearchSwipes(200)");103 do {104 try {105 LOGGER.info("Scroll to '" + text + "'");106 ((AndroidDriver<?>) DriverPool.getDriver())107 .findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\""108 + text + "\").instance(0))");109 scrolled = true;110 } catch (Exception e) {111 LOGGER.warn("Exception occurred for scroll operation! "112 + String.format(113 "For try #'%s'. Scrolling to text '%s'",114 repeat, text));115 repeat++;116 scrolled = false;117 }118 } while (!scrolled && repeat < tries);119 if (!scrolled) {120 try {121 LOGGER.info("Another solution Scroll to '" + text + "'");122 scrollToText(text);123 scrolled = true;124 } catch (Exception e) {125 LOGGER.warn("Exception occurred for scroll operation using Solution 2. "126 + String.format("Scrolling to text '%s'", text));127 scrolled = false;128 }129 }130 return scrolled;131 }132 /**133 * scrollTo specified text using findElementByAndroidUIAutomator solution134 *135 * @param text136 * - String137 * @return boolean138 */139 /*140 @Deprecated141 public static boolean scrollTo1(final String text) {142 boolean scrolled = false;143 int repeat = 1;144 int tries = 3;145 // TODO: investigate how to:146 // AndroidUIAutomator("setMaxSearchSwipes(200)");147 do {148 try {149 LOGGER.info("Scroll to '" + text + "' using findElementByAndroidUIAutomator.");150 ((AndroidDriver<?>) DriverPool.getDriver())151 .findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\""152 + text + "\").instance(0))");153 scrolled = true;154 } catch (Exception e) {155 LOGGER.warn("Exception occurred for scroll operation! "156 + String.format(157 "For try #'%s'. Scrolling to text '%s'",158 repeat, text));159 repeat++;160 scrolled = false;161 }162 } while (!scrolled && repeat < tries);163 return scrolled;164 }165*/166 /**167 * scrollTo specified text168 *169 * @param text - String170 * @return boolean171 */172 @Deprecated173 public static boolean scrollTo2(final String text) {174 boolean scrolled = false;175 try {176 LOGGER.info("Scroll to '" + text + "' using AndroidDriver default solution.");177 scrollToText(text);178 scrolled = true;179 } catch (Exception e) {180 LOGGER.warn("Exception occurred for scroll operation using Solution 2. "181 + String.format("Scrolling to text '%s'", text));182 scrolled = false;183 }184 return scrolled;185 }186 /**187 * swipe Until Element Presence188 *189 * @param element ExtendedWebElement190 * @return boolean191 */192 public static boolean swipeUntilElementPresence(193 final ExtendedWebElement element) {194 int swipeTimes = 20;195 WebDriver driver = DriverPool.getDriver();196 Dimension scrSize;197 int x;198 int y;199 boolean isPresent = element.isElementPresent(MINIMUM_TIMEOUT);200 LOGGER.info("Swipe down to element: ".concat(element.toString()));201 while (!isPresent && swipeTimes-- > 0) {202 LOGGER.debug("Element not present! Swipe down will be executed.");203 scrSize = driver.manage().window().getSize();204 x = scrSize.width / 2;205 y = scrSize.height / 2;206 ((AndroidDriver<?>) driver).swipe(x, y, x, y / 2, 500);207 LOGGER.info("Swipe was executed. Attempts remain: " + swipeTimes);208 isPresent = element.isElementPresent(1);209 LOGGER.info("Result: " + isPresent);210 }211 if (!isPresent) {212 LOGGER.info("Swipe up to element: ".concat(element.toString()));213 swipeTimes = 20;214 while (!isPresent && swipeTimes-- > 0) {215 LOGGER.debug("Element not present! Swipe up will be executed.");216 scrSize = driver.manage().window().getSize();217 x = scrSize.width / 2;218 y = scrSize.height / 2;219 ((AndroidDriver<?>) driver).swipe(x, y / 2, x, y, 500);220 LOGGER.info("Swipe was executed. Attempts remain: " + swipeTimes);221 isPresent = element.isElementPresent(1);222 LOGGER.info("Result: " + isPresent);223 }224 }225 return isPresent;226 }227 /**228 * universal Scroll To text with different methods229 *230 * @param scrollToText String231 * @param containerElement ExtendedWebElement232 * @return boolean233 */234 public static boolean universalScrollToBase(String scrollToText,235 ExtendedWebElement containerElement) {236 return universalScrollToBase(scrollToText, containerElement, 3, false);237 }238 /**239 * universal Scroll To text with different methods240 *241 * @param scrollToText String242 * @param containerElement ExtendedWebElement243 * @param tries - how much tries should be spent for scrolling. If 0 - it will244 * be quick check for not present element with scrolling try.245 * @param oldMethod boolean246 * @return boolean247 */248 public static boolean universalScrollToBase(String scrollToText,249 ExtendedWebElement containerElement, int tries, boolean oldMethod) {250 boolean scrolled = false;251 if ((tries > 0) && (!oldMethod)) {252 scrolled = AndroidUtils.scrollTo(scrollToText);253 }254 if (scrolled) {255 return true;256 } else {257 WebDriver driver = DriverPool.getDriver();258 LOGGER.info("Scrolling with old scroll method. Just old method:"259 + oldMethod + ". With " + tries + " tries.");260 try {261 try {262 driver.manage().timeouts()263 .implicitlyWait(1, TimeUnit.SECONDS);264 } catch (Exception e) {265 LOGGER.error("Strange error with implicitlyWait" + e);266 }267 RemoteWebElement element = (RemoteWebElement) driver268 .findElement(By.name(scrollToText));269 if (element.isDisplayed()) {270 try {271 driver.manage()272 .timeouts()273 .implicitlyWait(IMPLICIT_TIMEOUT,274 TimeUnit.SECONDS);275 } catch (Exception e) {276 LOGGER.error("Strange error with implicitlyWait" + e);277 }278 return true;279 }280 } catch (Exception e) {281 // restore timeout282 try {283 driver.manage().timeouts()284 .implicitlyWait(IMPLICIT_TIMEOUT, TimeUnit.SECONDS);285 } catch (Exception err) {286 LOGGER.error("Strange error with implicitlyWait" + err);287 }288 }289 LOGGER.info(String.format(290 "Scrolling to text '%s', Scroll container: %s",291 scrollToText, containerElement.getNameWithLocator()));292 try {293 final HashMap<String, String> scrollMap = new HashMap<String, String>();294 final JavascriptExecutor executor = (JavascriptExecutor) driver;295 scrollMap.put("text", scrollToText);296 scrollMap.put("element", ((RemoteWebElement) driver297 .findElement(containerElement.getBy())).getId());298 LOGGER.info(scrollMap);299 scrolled = false;300 int i = 0;301 while (!scrolled && ++i <= tries) {302 try {303 LOGGER.info("attempt #" + i);304 executor.executeScript("mobile: scrollTo", scrollMap);305 scrolled = true;306 } catch (Exception e) {307 LOGGER.warn("Exception occurred for scroll operation! "308 + String.format(309 "Scrolling to text '%s', Scroll container: %s",310 scrollToText,311 containerElement.getNameWithLocator()));312 }313 }314 } catch (Exception e) {315 LOGGER.error("Error happened during call JavascriptExecutor executor : "316 + e);317 scrolled = false;318 }319 }320 LOGGER.info("Successfully scrolled to text '" + scrollToText + "': "321 + scrolled);322 return scrolled;323 }324 /**325 * tap And Swipe specific ExtendedWebElement element to required direction326 *327 * @param elem ExtendedWebElement328 * @param direction SwipeElementDirection329 * @param duration of swipe (int)330 * @return boolean331 */332 public static boolean tapAndSwipe(ExtendedWebElement elem,333 SwipeElementDirection direction, int duration) {334 return tapAndSwipe(elem.getBy(), direction, duration);335 }336 /**337 * tap And Swipe specific element to left by default338 *339 * @param elem By340 * @return boolean341 */342 public static boolean tapAndSwipe(By elem) {343 return tapAndSwipe(elem, SwipeElementDirection.LEFT, 1000);344 }345 /**346 * tap And Swipe specific element to required direction347 *348 * @param elem By349 * @param direction SwipeElementDirection350 * @param duration of swipe (int)351 * @return boolean352 */353 public static boolean tapAndSwipe(By elem, SwipeElementDirection direction,354 int duration) {355 MobileElement element;356 WebDriver driver = DriverPool.getDriver();357 try {358 element = (MobileElement) driver.findElement(elem);359 element.swipe(direction, duration);360 return true;361 } catch (Exception e) {362 LOGGER.error("Exception occurred when "363 + "element.swipe(SwipeElementDirection."364 + direction.toString() + ", " + duration + ") "365 + "was provided in tapAndSwipe functionality. Error: "366 + e.getLocalizedMessage());367 }368 return false;369 }370 /**371 * swipe Up372 *373 * @param elem By374 * @param time int375 */376 public static void swipeUp(By elem, int time) {377 tapAndSwipe(elem, SwipeElementDirection.UP, time);378 }379 /**380 * swipe In Container381 *382 * @param elem - scrollable container383 * @param times - swipe times384 * @param direction -Direction {LEFT, RIGHT, UP, DOWN}385 * @param duration - duration in msec.386 */387 public static void swipeInContainer(ExtendedWebElement elem, int times,388 Direction direction, int duration) {389 // Default direction left390 double directMultX1 = 0.9;391 double directMultX2 = 0.1;392 double directMultY1 = 0.5;393 double directMultY2 = 0.5;394 WebDriver driver = DriverPool.getDriver();395 if (direction.equals(Direction.RIGHT)) {396 directMultX1 = 0.2;397 directMultX2 = 0.9;398 directMultY1 = 0.5;399 directMultY2 = 0.5;400 LOGGER.info("Swipe right");401 } else if (direction.equals(Direction.LEFT)) {402 directMultX1 = 0.9;403 directMultX2 = 0.2;404 directMultY1 = 0.5;405 directMultY2 = 0.5;406 LOGGER.info("Swipe left");407 } else if (direction.equals(Direction.UP)) {408 directMultX1 = 0.1;409 directMultX2 = 0.1;410 directMultY1 = 0.2;411 directMultY2 = 0.9;412 LOGGER.info("Swipe up");413 } else if (direction.equals(Direction.DOWN)) {414 directMultX1 = 0.1;415 directMultX2 = 0.1;416 directMultY1 = 0.9;417 directMultY2 = 0.2;418 LOGGER.info("Swipe down");419 } else if (direction.equals(Direction.VERTICAL)420 || direction.equals(Direction.HORIZONTAL)421 || direction.equals(Direction.HORIZONTAL_RIGHT_FIRST)422 || direction.equals(Direction.VERTICAL_DOWN_FIRST)) {423 LOGGER.info("Incorrect swipe direction: " + direction.toString());424 return;425 }426 int x = elem.getElement().getLocation().getX();427 int y = elem.getElement().getLocation().getY();428 int width = elem.getElement().getSize().getWidth();429 int height = elem.getElement().getSize().getHeight();430 int screen_size_x = driver.manage().window().getSize().getWidth();431 int screen_size_y = driver.manage().window().getSize().getHeight();432 LOGGER.debug("x=" + x + ", y=" + y + ", width=" + width + ", height="433 + height + ", screen width=" + screen_size_x434 + ", screen height=" + screen_size_y);435 LOGGER.info("Swiping in container:" + elem.getNameWithLocator());436 for (int i = 0; i <= times; i++) {437 int pointX1 = (int) (x + (width * directMultX1));438 int pointY1 = (int) (y + (height * directMultY1));439 int pointX2 = (int) (x + (width * directMultX2));440 int pointY2 = (int) (y + (height * directMultY2));441 LOGGER.debug("Direction:" + direction + ". Try #" + i442 + ". Points: X1Y1=" + pointX1 + ", " + pointY1 + ", X2Y2="443 + pointX2 + ", " + pointY2);444 try {445 ((AndroidDriver<?>) driver).swipe(pointX1, pointY1, pointX2,446 pointY2, duration);447 } catch (Exception e) {448 LOGGER.error("Exception: " + e);449 }450 }451 }452 /**453 * Quick solution for scrolling To Button or element.454 *455 * @param extendedWebElement ExtendedWebElement456 * @return boolean457 */458 @Deprecated459 public static boolean scrollTo(final ExtendedWebElement extendedWebElement) {460 int i = 0;461 try {462 WebDriver driver = DriverPool.getDriver();463 int x = driver.manage().window().getSize().getWidth();464 int y = driver.manage().window().getSize().getHeight();465 LOGGER.info("Swipe down");466 while (!extendedWebElement.isElementPresent(1) && ++i <= 10) {467 LOGGER.debug("Swipe down. Attempt #" + i);468 ((AndroidDriver<?>) driver)469 .swipe((int) (x * 0.1), (int) (y * 0.9),470 (int) (x * 0.1), (int) (y * 0.2), 2000);471 }472 if (!extendedWebElement.isElementPresent(1)) {473 LOGGER.info("Swipe up");474 i = 0;475 x = driver.manage().window().getSize().getWidth();476 y = driver.manage().window().getSize().getHeight();477 while (!extendedWebElement.isElementPresent(1) && ++i <= 10) {478 LOGGER.debug("Swipe up. Attempt #" + i);479 ((AndroidDriver<?>) driver).swipe((int) (x * 0.1),480 (int) (y * 0.2), (int) (x * 0.1), (int) (y * 0.9),481 2000);482 }483 }484 return extendedWebElement.isElementPresent(1);485 } catch (Exception e) {486 LOGGER.info("Error happen during scrollTo ExtendedWebElement: " + e);487 return true;488 }489 }490 /**491 * swipe Coordinates492 *493 * @param startX int494 * @param startY int495 * @param endX int496 * @param endY int497 * @param duration int498 */499 public static void swipeCoord(int startX, int startY, int endX, int endY,500 int duration) {501 WebDriver driver = DriverPool.getDriver();502 ((AndroidDriver<?>) driver).swipe(startX, startY, endX, endY, duration);503 }504 /**505 * swipe Coordinates506 *507 * @param startX int508 * @param startY int509 * @param endX int510 * @param endY int511 */512 public static void swipeCoord(int startX, int startY, int endX, int endY) {513 swipeCoord(startX, startY, endX, endY, DEFAULT_SWIPE_TIMEOUT);514 }515 /**516 * swipe In Container To required Element517 *518 * @param extendedWebElement - expected element519 * @param container - scrollable container520 * @param direction - Direction {LEFT, RIGHT, UP, DOWN, HORIZONTAL, VERTICAL }521 * @param duration - duration522 * @param times - times523 * @return boolean524 */525 @Deprecated526 public static boolean swipeInContainerToElement(527 final ExtendedWebElement extendedWebElement,528 ExtendedWebElement container, Direction direction, int duration,529 int times) {530 int i = 0;531 boolean bothWay = false;532 Direction oppositeDirection = Direction.DOWN;533 try {534 WebDriver driver = DriverPool.getDriver();535 if (extendedWebElement.isElementPresent(1)) {536 LOGGER.info("Element already present");537 return true;538 }539 if (direction.equals(Direction.HORIZONTAL)) {540 bothWay = true;541 direction = Direction.LEFT;542 oppositeDirection = Direction.RIGHT;543 } else if (direction.equals(Direction.HORIZONTAL_RIGHT_FIRST)) {544 bothWay = true;545 direction = Direction.RIGHT;546 oppositeDirection = Direction.LEFT;547 } else if (direction.equals(Direction.VERTICAL_DOWN_FIRST)) {548 bothWay = true;549 direction = Direction.DOWN;550 oppositeDirection = Direction.UP;551 } else if (direction.equals(Direction.VERTICAL)) {552 bothWay = true;553 direction = Direction.UP;554 oppositeDirection = Direction.DOWN;555 }556 while (!extendedWebElement.isElementPresent(1) && ++i <= times) {557 LOGGER.debug("Swipe " + direction.toString());558 swipeInContainer(container, 1, direction, duration);559 }560 if (!extendedWebElement.isElementPresent(1) && bothWay) {561 LOGGER.info("Swipe in opposite direction");562 i = 0;563 while (!extendedWebElement.isElementPresent(1) && ++i <= times) {564 LOGGER.debug("Swipe " + direction.toString());565 swipeInContainer(container, 1, oppositeDirection, duration);566 }567 }568 return extendedWebElement.isElementPresent(1);569 } catch (Exception e) {570 LOGGER.info("Error happened during swipe in container for element: "571 + e);572 return true;573 }574 }575 /**576 * Hide keyboard if needed577 */578 public static void hideKeyboard() {579 try {580 ((AndroidDriver<?>) DriverPool.getDriver()).hideKeyboard();581 } catch (Exception e) {582 LOGGER.info("Keyboard was already hided or error occurs: " + e);583 }584 }585 /**586 * wait Until Element Not Present587 *588 * @param locator By589 * @param timeout long590 * @param pollingTime long591 */592 public static void waitUntilElementNotPresent(final By locator,593 final long timeout, final long pollingTime) {594 LOGGER.info(String.format("Wait until element %s disappear",595 locator.toString()));596 WebDriver driver = DriverPool.getDriver();597 driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);598 try {599 if (new WebDriverWait(driver, timeout, pollingTime)600 .until(ExpectedConditions601 .invisibilityOfElementLocated(locator))) {602 LOGGER.info(String.format(603 "Element located by: %s not present.",604 locator.toString()));605 } else {606 LOGGER.info(String.format(607 "Element located by: %s is still present.",608 locator.toString()));609 }610 } catch (TimeoutException e) {611 LOGGER.debug(e.getMessage());612 LOGGER.info(String.format(613 "Element located by: %s is still present.",614 locator.toString()));615 }616 driver.manage().timeouts()617 .implicitlyWait(IMPLICIT_TIMEOUT, TimeUnit.SECONDS);618 }619 /**620 * Tap and Hold (LongPress) on element in Android621 *622 * @param element ExtendedWebElement623 * @return boolean624 */625 public static boolean longPress(ExtendedWebElement element) {626 try {627 WebDriver driver = DriverPool.getDriver();628 TouchAction action = new TouchAction((MobileDriver) driver);629 action.longPress(element.getElement()).release().perform();630 return true;631 } catch (Exception e) {632 LOGGER.info("Error occurs: " + e);633 }634 return false;635 }636 @Deprecated637 public static ExtendedWebElement scrollToText(String text) {638 AndroidElement androidElement = ((AndroidDriver<AndroidElement>) getDriver()).findElement(MobileBy639 .AndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView(new UiSelector().text(\"" + text + "\"));"));640 return new ExtendedWebElement(androidElement, getDriver());641 }642 @Deprecated643 public static ExtendedWebElement scrollToText(String scrollViewId, String text) {644 AndroidElement androidElement = ((AndroidDriver<AndroidElement>) getDriver()).findElement(MobileBy645 .AndroidUIAutomator("new UiScrollable(new UiSelector().resourceId(\"" + scrollViewId + "\")).scrollIntoView(new UiSelector().text(\"" + text + "\"));"));646 return new ExtendedWebElement(androidElement, getDriver());647 }648 /**649 * newScrollTo. Try to use new java_appium solution. (Unstable) And 2 more scroll solutions from AndroidUtils.650 * https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/touch-actions.md651 *652 * @param scrollToText String653 * @param containerElement ExtendedWebElement654 * @param tries int655 * @return boolean656 */657 private static boolean newScrollTo(String scrollToText,658 ExtendedWebElement containerElement, int tries) {659 boolean scrolled = false;660 WebDriver driver = DriverPool.getDriver();661 try {662 JavascriptExecutor js = (JavascriptExecutor) driver;663 HashMap<String, String> scrollObject = new HashMap<String, String>();664 scrollObject.put("direction", "down");665 scrollObject.put("element", ((RemoteWebElement) driver666 .findElement(containerElement.getBy())).getId());667 scrollObject.put("text", scrollToText);668 //scrollObject.put("element", ((RemoteWebElement) element).getId());669 js.executeScript("mobile: scroll", scrollObject);670 scrolled = true;671 } catch (Exception e) {672 LOGGER.warn("Exception occurred for scroll operation using new Appium Java client! "673 + String.format(674 "Scrolling to text '%s', Scroll container: %s",675 scrollToText,676 containerElement.getNameWithLocator()));677 }678 /*if ((tries > 0) && (!scrolled )) {679 LOGGER.info("Using scrollTo1 method.");680 scrolled = AndroidUtils.scrollTo1(scrollToText);681 }*/682 if (!scrolled) {683 LOGGER.info("Using scrollTo2 method.");684 scrolled = AndroidUtils.scrollTo2(scrollToText);685 }686 return scrolled;687 }688 /**689 * universal Scroll To text with different methods (Extended)690 *691 * @param scrollToText String692 * @param containerElement ExtendedWebElement693 * @return boolean694 */695 public static boolean universalScrollToExtended(String scrollToText,696 ExtendedWebElement containerElement) {697 return universalScrollToExtended(scrollToText, containerElement, 3, false);698 }699 /**700 * universal Scroll To text with different methods (Extended)701 *702 * @param scrollToText String703 * @param containerElement ExtendedWebElement704 * @param tries - how much tries should be spent for scrolling. If 0 - it will705 * be quick check for not present element with scrolling try.706 * @param oldMethod - if true - will try to execute old methods.707 * @return boolean708 */709 public static boolean universalScrollToExtended(String scrollToText,710 ExtendedWebElement containerElement, int tries, boolean oldMethod) {711 //Set oldMethod to false for trying use as much as possible solutions for scrolling.712 //oldMethod = false;713 boolean scrolled = AndroidUtils.universalScrollToBase(scrollToText, containerElement, tries, oldMethod);714 if (!scrolled) {715 LOGGER.info("Try to use 3 more new solutions for scrolling. ");716 scrolled = newScrollTo(scrollToText, containerElement, tries);717 }718 if (scrolled) {719 LOGGER.info("Finally scrolled to text '" + scrollToText + "'.");720 }721 return scrolled;722 }723 /**724 * universal Scroll To text with different methods725 *726 * @param scrollToText String727 * @param containerElement ExtendedWebElement728 * @param tries - how much tries should be spent for scrolling. If 0 - it will729 * be quick check for not present element with scrolling try.730 * @param oldMethod - if true try to call old methods731 * @return boolean732 */733 public static boolean universalScrollTo(String scrollToText,734 ExtendedWebElement containerElement, int tries, boolean oldMethod) {735 return universalScrollToExtended(scrollToText, containerElement, tries, oldMethod);736 }737 /**738 * universal Scroll To text with different methods739 *740 * @param scrollToText String741 * @param containerElement ExtendedWebElement742 * @return boolean743 */744 public static boolean universalScrollTo(String scrollToText,745 ExtendedWebElement containerElement) {746 return universalScrollToExtended(scrollToText, containerElement, 3, false);747 }748 // TODO temporary decision. If it works it should be moved to carina749 public static boolean swipeUntilElementPresence(750 final ExtendedWebElement element, int times) {751 WebDriver driver = DriverPool.getDriver();752 Dimension scrSize;753 int x;754 int y;755 boolean isPresent = element.isElementPresent(1);756 LOGGER.info("Swipe down to element: ".concat(element.toString()));757 while (!isPresent && times-- > 0) {758 LOGGER.debug("Element not present! Swipe down will be executed.");759 scrSize = driver.manage().window().getSize();760 x = scrSize.width / 2;761 y = scrSize.height / 2;762 ((AndroidDriver<?>) driver).swipe(x, y, x, y / 2, 500);763 LOGGER.info("Swipe was executed. Attempts remain: " + times);764 isPresent = element.isElementPresent(1);765 LOGGER.info("Result: " + isPresent);766 }767 if (!isPresent) {768 LOGGER.info("Swipe up to element: ".concat(element.toString()));769 while (!isPresent && times-- > 0) {770 LOGGER.debug("Element not present! Swipe up will be executed.");771 scrSize = driver.manage().window().getSize();772 x = scrSize.width / 2;773 y = scrSize.height / 2;774 ((AndroidDriver<?>) driver).swipe(x, y / 2, x, y, 500);775 LOGGER.info("Swipe was executed. Attempts remain: " + times);776 isPresent = element.isElementPresent(1);777 LOGGER.info("Result: " + isPresent);778 }779 }780 return isPresent;781 }782 /**783 * change Android Device Language784 * <p>785 * Url: <a href="http://play.google.com/store/apps/details?id=net.sanapeli.adbchangelanguage&hl=ru&rdid=net.sanapeli.adbchangelanguage">786 * ADBChangeLanguage apk787 * </a>788 * Change locale (language) of your device via ADB (on Android OS version 6.0, 5.0, 4.4, 4.3, 4.2 and older).789 * No need to root your device! With ADB (Android Debug Bridge) on your computer,790 * you can fast switch the device locale to see how your application UI looks on different languages.791 * Usage:792 * - install this app793 * - setup adb connection to your device (http://developer.android.com/tools/help/adb.html)794 * - Android OS 4.2 onwards (tip: you can copy the command here and paste it to your command console):795 * adb shell pm grant net.sanapeli.adbchangelanguage android.permission.CHANGE_CONFIGURATION796 * <p>797 * English: adb shell am start -n net.sanapeli.adbchangelanguage/.AdbChangeLanguage -e language en798 * Russian: adb shell am start -n net.sanapeli.adbchangelanguage/.AdbChangeLanguage -e language ru799 * Spanish: adb shell am start -n net.sanapeli.adbchangelanguage/.AdbChangeLanguage -e language es800 *801 * @param language to set. Can be es, en, etc.802 * @return boolean803 */804 public static boolean setDeviceLanguage(String language) {805 boolean status = false;806 String[] setLocalizationChangePermissionCmd;807 String[] setLocalizationCmd;808 AdbExecutor executor;809 language = language.toLowerCase();810 executor = new AdbExecutor();811 String[] initCmd = executor.getDefaultCmd();812 LOGGER.debug("Init cmd: ".concat(initCmd.toString()));813 String deviceUdid = DevicePool.getDeviceUdid();814 LOGGER.info("Device udid: ".concat(deviceUdid));815 if (!deviceUdid.isEmpty()) {816 setLocalizationChangePermissionCmd = CmdLine.insertCommandsAfter(817 initCmd, "-s", deviceUdid, "shell", "pm", "grant",818 "net.sanapeli.adbchangelanguage",819 "android.permission.CHANGE_CONFIGURATION");820 setLocalizationCmd = CmdLine.insertCommandsAfter(initCmd, "-s",821 deviceUdid, "shell", "am", "start", "-n",822 "net.sanapeli.adbchangelanguage/.AdbChangeLanguage", "-e",823 "language", language);824 } else {...
Source:Device.java
...7import com.qaprosoft.carina.core.foundation.utils.R;8import com.qaprosoft.carina.core.foundation.utils.Configuration.Parameter;9import com.qaprosoft.carina.core.foundation.utils.SpecialKeywords;10import com.qaprosoft.carina.core.foundation.utils.android.recorder.exception.ExecutorException;11import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.AdbExecutor;12import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine;13import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.Platform;14import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.ProcessBuilderExecutor;15import com.qaprosoft.carina.core.foundation.utils.factory.DeviceType.Type;16import com.qaprosoft.carina.core.foundation.webdriver.appium.status.AppiumStatus;17//Motorola|ANDROID|4.4|T01130FJAD|http://localhost:4725/wd/hub;Samsung_S4|ANDROID|4.4.2|5ece160b|http://localhost:4729/wd/hub;18public class Device19{20 private static final Logger LOGGER = Logger.getLogger(Device.class);21 private String name;22 private String type;23 private String os;24 private String osVersion;25 private String udid;26 private String seleniumServer;27 private String testId;28 29 AdbExecutor executor = new AdbExecutor();30 public Device(String args)31 {32 // Samsung_S4|ANDROID|4.4.2|5ece160b|4729|4730|http://localhost:4725/wd/hub33 LOGGER.debug("mobile_device_args: " + args);34 args = args.replaceAll("|", "|");35 LOGGER.debug("mobile_device_args: " + args);36 String[] params = args.split("\\|");37 // TODO: organize verification onto the params count38 this.name = params[0];39 LOGGER.debug("mobile_device_name: " + name);40 this.type = params[1];41 LOGGER.debug("mobile_device_type: " + params[1]);42 this.os = params[2];43 this.osVersion = params[3];...
AdbExecutor
Using AI Code Generation
1import com.qaprosoft.carina.core.foundation.webdriver.device.Device;2import com.qaprosoft.carina.core.foundation.webdriver.device.DevicePool;3public class AdbExecutorTest {4 public static void main(String[] args) throws Exception {5 Device device = DevicePool.getDevice();6 String output = device.getAdbExecutor().executeAdbCommand("shell", "dumpsys", "package", "com.android.chrome");7 System.out.println(output);8 }9}10import com.qaprosoft.carina.core.foundation.webdriver.device.Device;11import com.qaprosoft.carina.core.foundation.webdriver.device.DevicePool;12public class AdbExecutorTest {13 public static void main(String[] args) throws Exception {14 Device device = DevicePool.getDevice();15 String output = device.getAdbExecutor().executeAdbCommand("shell", "dumpsys", "package", "com.android.chrome");16 System.out.println(output);17 }18}19import com.qaprosoft.carina.core.foundation.webdriver.device.Device;20import com.qaprosoft.carina.core.foundation.webdriver.device.DevicePool;21public class AdbExecutorTest {22 public static void main(String[] args) throws Exception {23 Device device = DevicePool.getDevice();24 String output = device.getAdbExecutor().executeAdbCommand("shell", "dumpsys", "package", "com.android.chrome");25 System.out.println(output);26 }27}28import com.qaprosoft.carina.core.foundation.webdriver.device.Device;29import com.qaprosoft.carina.core.foundation.webdriver.device.DevicePool;30public class AdbExecutorTest {31 public static void main(String[] args) throws Exception {32 Device device = DevicePool.getDevice();33 String output = device.getAdbExecutor().executeAdbCommand("shell", "dumpsys", "package", "com.android.chrome");34 System.out.println(output);35 }36}37import com.qaprosoft.carina.core.foundation.webdriver
AdbExecutor
Using AI Code Generation
1import com.qaprosoft.carina.core.foundation.webdriver.device.Device;2import com.qaprosoft.carina.core.foundation.webdriver.device.DevicePool;3public class AdbExecutor {4 public static void main(String[] args) {5 Device device = DevicePool.getDevice("Android");6 String adbCommand = "adb shell dumpsys window windows | grep -E 'mCurrentFocus'";7 String result = device.executeAdbCommand(adbCommand);8 System.out.println(result);9 }10}11import com.qaprosoft.carina.core.foundation.webdriver.device.Device;12import com.qaprosoft.carina.core.foundation.webdriver.device.DevicePool;13public class AdbExecutor {14 public static void main(String[] args) {15 Device device = DevicePool.getDevice("Android");16 String adbCommand = "adb shell dumpsys window windows | grep -E 'mCurrentFocus'";17 String result = device.executeAdbCommand(adbCommand);18 System.out.println(result);19 }20}21import com.qaprosoft.carina.core.foundation.webdriver.device.Device;22import com.qaprosoft.carina.core.foundation.webdriver.device.DevicePool;23public class AdbExecutor {24 public static void main(String[] args) {25 Device device = DevicePool.getDevice("Android");26 String adbCommand = "adb shell dumpsys window windows | grep -E 'mCurrentFocus'";27 String result = device.executeAdbCommand(adbCommand);28 System.out.println(result);29 }30}31import com.qaprosoft.carina.core.foundation.webdriver.device.Device;32import com.qaprosoft.carina.core.foundation.webdriver.device.DevicePool;33public class AdbExecutor {34 public static void main(String[] args) {
AdbExecutor
Using AI Code Generation
1package com.qaprosoft.carina.demo;2import org.testng.annotations.Test;3import com.qaprosoft.carina.core.foundation.utils.ownership.MethodOwner;4import com.qaprosoft.carina.demo.gui.pages.HomePage;5import com.qaprosoft.carina.demo.gui.pages.LoginPage;6import com.qaprosoft.carina.demo.gui.pages.SignUpPage;7import com.qaprosoft.carina.demo.gui.pages.StartPage;8import com.qaprosoft.carina.demo.gui.pages.UserProfilePage;9public class AdbExecutorTest extends AbstractTest {10 @MethodOwner(owner = "qpsdemo")11 public void testAdbExecutor() {12 StartPage startPage = new StartPage(getDriver());13 startPage.open();14 HomePage homePage = startPage.openHomePage();15 LoginPage loginPage = homePage.openLoginPage();16 SignUpPage signUpPage = loginPage.openSignUpPage();17 UserProfilePage userProfilePage = signUpPage.registerNewUser();18 userProfilePage.isPageOpened();19 Device adb = new Device();20 adb.adbExecutor("adb shell input tap 100 200");21 adb.adbExecutor("adb shell input keyevent 4");22 adb.adbExecutor("adb shell input keyevent 3");23 adb.adbExecutor("adb shell input keyevent 26");24 adb.adbExecutor("adb shell input keyevent 82");25 }26}
AdbExecutor
Using AI Code Generation
1import com.qaprosoft.carina.core.foundation.webdriver.device.Device;2public class 1 {3public static void main(String[] args) {4Device adb = DevicePoolManager.getDevice();5adb.getAdbExecutor().execute("shell input keyevent 26");6}7}8import com.qaprosoft.carina.core.foundation.webdriver.device.Device;9public class 2 {10public static void main(String[] args) {11Device adb = DevicePoolManager.getDevice();12adb.getAdbExecutor().execute("shell input keyevent 26");13}14}15import com.qaprosoft.carina.core.foundation.webdriver.device.Device;16public class 3 {17public static void main(String[] args) {18Device adb = DevicePoolManager.getDevice();19adb.getAdbExecutor().execute("shell input keyevent 26");20}21}22import com.qaprosoft.carina.core.foundation.webdriver.device.Device;23public class 4 {24public static void main(String[] args) {25Device adb = DevicePoolManager.getDevice();26adb.getAdbExecutor().execute("shell input keyevent 26");27}28}29import com.qaprosoft.carina.core.foundation.webdriver.device.Device;30public class 5 {31public static void main(String[] args) {32Device adb = DevicePoolManager.getDevice();33adb.getAdbExecutor().execute("shell input keyevent 26");34}35}36import com.qaprosoft.carina.core.foundation.webdriver.device.Device;37public class 6 {38public static void main(String[] args) {39Device adb = DevicePoolManager.getDevice();40adb.getAdbExecutor().execute("shell input keyevent 26");41}42}43import com.qaprosoft.car
AdbExecutor
Using AI Code Generation
1import java.io.IOException;2import java.util.List;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.support.ui.WebDriverWait;5import org.testng.Assert;6import org.testng.annotations.AfterMethod;7import org.testng.annotations.BeforeMethod;8import org.testng.annotations.Test;9import com.qaprosoft.carina.core.foundation.webdriver.device.Device;10import com.qaprosoft.carina.core.foundation.webdriver.device.DevicePool;11import com.qaprosoft.carina.core.foundation.webdriver.device.DeviceType;12import com.qaprosoft.carina.core.foundation.webdriver.listener.DriverListener;13import com.qaprosoft.carina.core.foundation.webdriver.listener.RetryAnalyzer;14import com.qaprosoft.carina.core.foundation.webdriver.listener.RetryAnalyzer.RetryAnalyzerCount;15import com.qaprosoft.carina.core.foundation.webdriver.listener.RetryAnalyzer.RetryAnalyzerCount;16public class AdbExecutorTest {17 public void beforeMethod() {18 DriverListener.driver.set(null);19 }20 public void afterMethod() {21 DriverListener.driver.get().quit();22 }23 @RetryAnalyzerCount(2)24 public void testAdbExecutor() throws IOException, InterruptedException {25 Device device = DevicePool.getDevice(DeviceType.ANDROID_PHONE);26 WebDriver driver = device.getDriver();27 WebDriverWait wait = new WebDriverWait(driver, 5);28 List<String> output = Device.AdbExecutor.executeADBCommand("adb shell pm list packages -3");29 System.out.println("output: "+output);30 Assert.assertFalse(output.isEmpty());31 }32}33import java.io.IOException;34import java.util.List;35import org.openqa.selenium.WebDriver;36import org.openqa.selenium.support.ui.WebDriverWait;37import org.testng.Assert;38import org.testng.annotations.AfterMethod;39import org.testng.annotations.BeforeMethod;40import org.testng.annotations.Test;41import com.qaprosoft.carina.core.foundation.webdriver.device.Device;42import com.qaprosoft.carina.core.foundation.webdriver.device.DevicePool;43import com.qaprosoft.carina.core.foundation.webdriver.device.DeviceType;44import com.qaprosoft.carina.core.foundation.webdriver.listener.DriverListener;45import com.qaprosoft.carina.core.foundation.webdriver.listener.RetryAnalyzer;46import com.qapro
AdbExecutor
Using AI Code Generation
1public class AdbExecutorExample {2 public static void main(String[] args) {3 Device device = new Device();4 String adbCommand = "adb shell getprop ro.build.version.release";5 String version = device.executeAdbCommand(adbCommand);6 System.out.println("Android version is: " + version);7 }8}9public class AdbExecutorExample {10 public static void main(String[] args) {11 Device device = new Device();12 String version = device.executeAdbCommand("adb shell getprop ro.build.version.release");13 System.out.println("Android version is: " + version);14 }15}16public class AdbExecutorExample {17 public static void main(String[] args) {18 Device device = new Device();19 String version = device.executeAdbCommand("getprop ro.build.version.release");20 System.out.println("Android version is: " + version);21 }22}
AdbExecutor
Using AI Code Generation
1package com.qaprosoft.carina.demo;2import org.testng.annotations.Test;3import com.qaprosoft.carina.core.foundation.webdriver.device.Device;4public class AdbExecutorTest extends AbstractTest {5 public void testAdbExecutor() {6 Device device = new Device();7 device.AdbExecutor("adb shell input keyevent 26");8 }9}10package com.qaprosoft.carina.demo;11import org.testng.annotations.Test;12import com.qaprosoft.carina.core.foundation.webdriver.device.Device;13public class AdbExecutorTest extends AbstractTest {14 public void testAdbExecutor() {15 Device device = new Device();16 device.AdbExecutor("adb shell input keyevent 26");17 }18}19package com.qaprosoft.carina.demo;20import org.testng.annotations.Test;21import com.qaprosoft.carina.core.foundation.webdriver.device.Device;22public class AdbExecutorTest extends AbstractTest {23 public void testAdbExecutor() {24 Device device = new Device();25 device.AdbExecutor("adb shell input keyevent 26");26 }27}28package com.qaprosoft.carina.demo;29import org.testng.annotations.Test;30import com.qaprosoft.carina.core.foundation.webdriver.device.Device;31public class AdbExecutorTest extends AbstractTest {32 public void testAdbExecutor() {33 Device device = new Device();34 device.AdbExecutor("adb shell input keyevent 26");35 }36}
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!!