Best Cerberus-source code snippet using org.cerberus.service.appium.impl.AndroidAppiumService.swipe
Source:AndroidAppiumService.java
...48 @Autowired49 private ParameterService parameters;50 51 /**52 * The Appium swipe duration parameter which is got thanks to the53 * {@link ParameterService}54 */55 private static final String CERBERUS_APPIUM_SWIPE_DURATION_PARAMETER = "cerberus_appium_swipe_duration";56 57 /**58 * The default Appium swipe duration if no59 * {@link AppiumService#APPIUM_SWIPE_DURATION_PARAMETER} has been defined60 */61 private static final int DEFAULT_CERBERUS_APPIUM_SWIPE_DURATION = 2000;62 /**63 * The {@link Pattern} related error when keyboard is absent64 */65 private static final Pattern IS_KEYBOARD_ABSENT_ERROR_PATTERN = Pattern.compile("Original error: Soft keyboard not present");66 @Override67 public MessageEvent keyPress(Session session, String keyName) {68 // First, check if key name exists69 KeyCode keyToPress;70 try {71 keyToPress = KeyCode.valueOf(keyName);72 } catch (IllegalArgumentException e) {73 return new MessageEvent(MessageEventEnum.ACTION_FAILED_KEYPRESS_NOT_AVAILABLE).resolveDescription("KEY", keyName);74 }75 // Then press the key76 try {77 ((AndroidDriver) session.getAppiumDriver()).pressKeyCode(keyToPress.getCode());78 return new MessageEvent(MessageEventEnum.ACTION_SUCCESS_KEYPRESS_NO_ELEMENT).resolveDescription("KEY", keyName);79 } catch (Exception e) {80 LOG.warn("Unable to key press due to " + e.getMessage(), e);81 return new MessageEvent(MessageEventEnum.ACTION_FAILED_KEYPRESS_OTHER)82 .resolveDescription("KEY", keyName)83 .resolveDescription("REASON", e.getMessage());84 }85 }86 @Override87 public MessageEvent hideKeyboard(Session session) {88 try {89 session.getAppiumDriver().hideKeyboard();90 return new MessageEvent(MessageEventEnum.ACTION_SUCCESS_HIDEKEYBOARD);91 } catch (Exception e) {92 // Instead of http://stackoverflow.com/questions/35030794/soft-keyboard-not-present-cannot-hide-keyboard-appium-android?answertab=votes#tab-top93 // and testing if keyboard is already hidden by executing an ADB command,94 // we prefer to parse error message to know if it's just due to keyboard which is already hidden.95 // This way, we are more portable because it is not necessary to connect to the Appium server and send the ADB command.96 if (IS_KEYBOARD_ABSENT_ERROR_PATTERN.matcher(e.getMessage()).find()) {97 return new MessageEvent(MessageEventEnum.ACTION_SUCCESS_HIDEKEYBOARD_ALREADYHIDDEN);98 }99 LOG.warn("Unable to hide keyboard due to " + e.getMessage(), e);100 return new MessageEvent(MessageEventEnum.ACTION_FAILED_HIDEKEYBOARD);101 }102 }103 /**104 * Translator between Cerberus key names and Android key codes105 */106 private enum KeyCode {107 RETURN(AndroidKeyCode.ENTER),108 ENTER(AndroidKeyCode.ENTER),109 SEARCH(AndroidKeyCode.KEYCODE_SEARCH),110 BACKSPACE(AndroidKeyCode.BACKSPACE),111 BACK(AndroidKeyCode.BACK); 112 private int code;113 KeyCode(int code) {114 this.code = code;115 }116 public int getCode() {117 return code;118 }119 }120 121 @Override122 public MessageEvent swipe(Session session, SwipeAction action) {123 try {124 SwipeAction.Direction direction = this.getDirectionForSwipe(session, action);125 // Get the parametrized swipe duration126 Parameter duration = parameters.findParameterByKey(CERBERUS_APPIUM_SWIPE_DURATION_PARAMETER, "");127 // Do the swipe thanks to the Appium driver128 TouchAction dragNDrop129 = new TouchAction(session.getAppiumDriver()).press(direction.getX1(), direction.getY1()).waitAction(Duration.ofMillis(duration == null ? DEFAULT_CERBERUS_APPIUM_SWIPE_DURATION : Integer.parseInt(duration.getValue())))130 .moveTo(direction.getX2(), direction.getY2()).release();131 dragNDrop.perform();132 133 return new MessageEvent(MessageEventEnum.ACTION_SUCCESS_SWIPE).resolveDescription("DIRECTION", action.getActionType().name());134 } catch (IllegalArgumentException e) {135 return new MessageEvent(MessageEventEnum.ACTION_FAILED_SWIPE)136 .resolveDescription("DIRECTION", action.getActionType().name())137 .resolveDescription("REASON", "Unknown direction");138 } catch (Exception e) {139 LOG.warn("Unable to swipe screen due to " + e.getMessage(), e);140 return new MessageEvent(MessageEventEnum.ACTION_FAILED_SWIPE)141 .resolveDescription("DIRECTION", action.getActionType().name())142 .resolveDescription("REASON", e.getMessage());143 }144 }145}...
swipe
Using AI Code Generation
1 @When("^I swipe from \"([^\"]*)\" to \"([^\"]*)\"$")2 public void iSwipeFromTo(String arg0, String arg1) throws Throwable {3 appiumService.swipe(arg0, arg1);4 }5 @When("^I swipe from \"([^\"]*)\" to \"([^\"]*)\"$")6 public void iSwipeFromTo(String arg0, String arg1) throws Throwable {7 appiumService.swipe(arg0, arg1);8 }9 @When("^I swipe from \"([^\"]*)\" to \"([^\"]*)\"$")10 public void iSwipeFromTo(String arg0, String arg1) throws Throwable {11 appiumService.swipe(arg0, arg1);12 }13 @When("^I swipe from \"([^\"]*)\" to \"([^\"]*)\"$")14 public void iSwipeFromTo(String arg0, String arg1) throws Throwable {15 appiumService.swipe(arg0, arg1);16 }17 @When("^I swipe from \"([^\"]*)\" to \"([^\"]*)\"$")18 public void iSwipeFromTo(String arg0, String arg1) throws Throwable {19 appiumService.swipe(arg0, arg1);20 }21 @When("^I swipe from \"([^\"]*)\" to \"([^\"]*)\"$")22 public void iSwipeFromTo(String arg0, String arg1) throws Throwable {23 appiumService.swipe(arg0, arg1);24 }
swipe
Using AI Code Generation
1 public void swipe(String direction, int duration) {2 int x = getDriver().manage().window().getSize().width / 2;3 int y = getDriver().manage().window().getSize().height / 2;4 int x1 = x;5 int y1 = y;6 int x2 = x;7 int y2 = y;8 int swipeDuration = duration;9 switch (direction) {10 y1 = y + 100;11 y2 = y - 100;12 break;13 y1 = y - 100;14 y2 = y + 100;15 break;16 x1 = x + 100;17 x2 = x - 100;18 break;19 x1 = x - 100;20 x2 = x + 100;21 break;22 throw new CerberusException("Unknown direction : " + direction);23 }24 getDriver().swipe(x1, y1, x2, y2, swipeDuration);25 }26 public void swipe(String direction, int duration) {27 int x = getDriver().manage().window().getSize().width / 2;28 int y = getDriver().manage().window().getSize().height / 2;29 int x1 = x;30 int y1 = y;31 int x2 = x;32 int y2 = y;33 int swipeDuration = duration;34 switch (direction) {35 y1 = y + 100;36 y2 = y - 100;37 break;38 y1 = y - 100;39 y2 = y + 100;40 break;41 x1 = x + 100;42 x2 = x - 100;43 break;44 x1 = x - 100;45 x2 = x + 100;46 break;47 throw new CerberusException("Unknown direction : " + direction);48 }49 getDriver().swipe(x1, y1, x2, y2, swipeDuration
swipe
Using AI Code Generation
1public void swipe(String startx, String starty, String endx, String endy, String duration) {2 try {3 int startX = Integer.valueOf(startx);4 int startY = Integer.valueOf(starty);5 int endX = Integer.valueOf(endx);6 int endY = Integer.valueOf(endy);7 int swipeDuration = Integer.valueOf(duration);8 TouchAction action = new TouchAction(driver);9 action.press(startX, startY).waitAction(Duration.ofMillis(swipeDuration)).moveTo(endX, endY).release().perform();10 } catch (Exception exception) {11 LOG.error(exception.toString());12 }13 }14public void testSwipe() {15 String startx = "100";16 String starty = "100";17 String endx = "200";18 String endy = "200";19 String duration = "1000";20 androidAppiumService.swipe(startx, starty, endx, endy, duration);21}22public void testSwipe() {23 String startx = "100";24 String starty = "100";25 String endx = "200";26 String endy = "200";27 String duration = "1000";28 androidAppiumService.swipe(startx, starty, endx, endy, duration);29}30public void testSwipe() {31 String startx = "100";
swipe
Using AI Code Generation
1public class AppiumSwipeTest {2 private static final Logger LOG = LogManager.getLogger(AppiumSwipeTest.class);3 private AndroidAppiumService appiumService;4 private WebDriver driver;5 public void initialize() throws MalformedURLException, CerberusException {6 appiumService = new AndroidAppiumService();7 driver = appiumService.initDriver();8 }9 public void testSwipe() throws CerberusException {10 appiumService.swipe(100, 100, 100, 200, 1000);11 LOG.info("Swipe from top to bottom");12 appiumService.swipe(100, 200, 100, 100, 1000);13 LOG.info("Swipe from bottom to top");14 appiumService.swipe(100, 100, 200, 100, 1000);15 LOG.info("Swipe from left to right");16 appiumService.swipe(200, 100, 100, 100, 1000);17 LOG.info("Swipe from right to left");18 }19 public void tearDown() {20 appiumService.quitDriver();21 }22}
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!!