We will discuss how to use Appium methods in this Appium tutorial. Appium is a wrapper on top of Selenium webDriver. All the Selenium methods are already present in Appium. Apart from that, Appium provides a good number of ways to interact with native applications.
How to use the pressKey code using Appium methods?
public void pressKeyCode(AndroidKey code) { \tandroidDriver.pressKey(new KeyEvent(code)); \t } Here you can pass keyEvent like AndroidKey.ENTER to enter or AndroidKey.BACK to tap on back button Similarly, users can use this for long key presses like: public void longPressKeyCode(AndroidKey code) { \tandroidDriver.longPressKey(new KeyEvent(code)); \t}
How to get the current activity using Appium commands?
To get the current activity, we can use the current Activity method. For Example: public String currentActivity() { \treturn androidDriver.currentActivity(); \t }
How to check if the app is installed or not?
To check if the app is installed or not we can use: public boolean isAppInstalled(String bundleId) { return androidDriver .isAppInstalled(bundleId); }
How to start activity in Android using Appium methods?
Please follow the below method to start the activity: public void startActivity(String packageName, String activity) { try { androidDriver.startActivity(new Activity(packageName, activity)); } catch (Exception e) { \t e.printStackTrace(); }
How to scroll to a particular text using Appium methods?
public MobileElement scrollTo(String text) { MobileElement textFinder = androidDriver .findElementByAndroidUIAutomator ("new UiScrollable(new UiSelector()" + ".resourceId(\\"android:id/list\\")) .scrollIntoView(" + "new UiSelector().text(\\"" + text + "\\"));"); \t return textFinder; }
How to hide the keyboard using Appium methods?
public void hideKeyboard() { try { androidDriver.hideKeyboard(); } catch (Exception e) { LoggerUtils.error("Exception in hiding keyboard" + e.getMessage()); }
How to get progress dialog using Appium methods?
public MobileElement getProgressDialog() { return androidDriver .findElementById ("android:id/progress"); }
How to swipe left using Appium methods??
public void swipeLeft(By by) { \tMobileElement element = (MobileElement) androidDriver.findElement(by); \tint offset = 1; \tPoint p = element.getCenter(); \tPoint location = element.getLocation(); \tDimension size = element.getSize(); \tint startX=location.getX() + size.getWidth() - offset; \tint startY=p.getY(); \tint endX=location.getX() + offset; \tnew TouchAction(androidDriver) .press(PointOption.point(startX, startY)) .waitAction(WaitOptions. waitOptions(java.time .Duration.ofMillis(1000))) .moveTo(PointOption.point(endX, startY)) .release() .perform(); }
How to swipe right using Appium methods?
public void swipeRight(By by) { \tMobileElement element = (MobileElement) androidDriver.findElement(by); \tint offset = 1; \tPoint p = element.getCenter(); \tPoint location = element.getLocation(); \tDimension size = element.getSize(); \tint startX=location.getX() + offset + 20; \tint startY=p.getY(); \tint endX=location.getX() + size.getWidth() - offset; \tnew TouchAction(androidDriver) .press(PointOption.point(startX, startY)) .waitAction(WaitOptions. waitOptions(java.time .Duration.ofMillis(1000))) .moveTo(PointOption.point(endX, startY)) .release() .perform(); }
How to swipe up using Appium methods?
MobileElement element = (MobileElement) androidDriver.findElement(by); \tDimension size = element.getSize(); \t int endX = (int) (size.height * 0.70); int startY = (int) (size.height * 0.30); int startX = (size.width / 2); new TouchAction(androidDriver) .press(PointOption.point(startX, startY)) .waitAction(WaitOptions. waitOptions(java.time .Duration.ofMillis(1000))) .moveTo(PointOption.point(endX, startY)) .release() .perform(); }
How to swipe down using Appium methods?
public void swipeDown(By by){ \tMobileElement element = (MobileElement) androidDriver.findElement(by); \tDimension size = element.getSize(); \tint startY = (int) (size.height * 0.70); int endY = (int) (size.height * 0.30); int startX = (size.width / 2); new TouchAction(androidDriver) .press(PointOption.point(startX, startY)) .waitAction(WaitOptions. waitOptions(java.time .Duration.ofMillis(1000))) .moveTo(PointOption.point(startX, endY)) .release() .perform(); }
How to double click in Appium using Appium commands?
public void doubleClick(WebElement element) { TouchActions action = new TouchActions(androidDriver); action.doubleClick(element); action.perform(); }
How to switch off GPS in Android?
public static void swicthOffGPSConnection(String androidSdkToolPath, String deviceId) { \ttry { \t Runtime.getRuntime(). exec(androidSdkToolPath + "/adb -s" + deviceId + " shell settings put secure location_providers_allowed ' '"); \t} catch (Exception e) { \t \t e.printStackTrace(); }
How to switch on GPS in Android?
public static void swicthONGPSConnection(String androidSdkToolPath, String deviceId) { \ttry { \t Runtime.getRuntime() .exec(androidSdkToolPath + "/adb -s" + deviceId \t\t + " shell settings put secure location_providers_allowed gps "); \t} catch (Exception e) { \t \t e.printStackTrace(); \t} }
How to switch off Wifi in Android?
public void switchOFFWifiConnection(String androidSdkToolPath,String deviceId) { \t\t try { Runtime.getRuntime() .exec(androidSdkToolPath+ "/adb -s"+deviceId +" shell am start -n io.appium.settings/.Settings -e wifi off"); } catch (IOException e) { \t\t\t e.printStackTrace(); } }
How to switch on Wifi in Android?
public void switchONWifiConnection(String androidSdkToolPath,String deviceId) { try { Runtime.getRuntime() .exec(androidSdkToolPath + "/adb -s"+deviceId+" shell am start -n io.appium.settings/.Settings -e wifi on"); } catch (IOException e) { \t\t\t e.printStackTrace(); } }
How to switch off data in Android?
public void switchOFFDataConnection(String androidSdkToolPath,String deviceId) { try { Runtime.getRuntime() .exec(androidSdkToolPath + "/adb -s"+deviceId+" shell am start -n io.appium.settings/.Settings -e data off"); System.out.println("************** Wifi off *******"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
How to switch on data in Android?
public void switchONDataConnection(String androidSdkToolPath,String deviceId) { try { Runtime.getRuntime() .exec(androidSdkToolPath + "/adb -s"+deviceId+" shell am start -n io.appium.settings/.Settings -e data on"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
How to uninstall the app in Android?
public void uninstallTheApp(String androidSdkToolPath, String deviceId, String pkg) { try { Runtime.getRuntime() .exec(androidSdkToolPath + "/adb -s " + deviceId + " uninstall " + pkg); } catch (Exception e) { System.out.println(e); } }
How to toggle flight mode in iOS?
public void toggleFlightMode() { \tDimension window = iosDriver. manage().window().getSize(); \ttry { \tint startX = window.width / 2; \t\tint startY = window.height; \t\tint endY = window.height / 10; \t\tnew TouchAction(iosDriver) \t\t.press(PointOption.point(startX, startY)) \t\t.waitAction(WaitOptions \t\t.waitOptions(java.time .Duration.ofMillis(1000))) \t\t.moveTo(PointOption \t\t.point(startX, endY)).release().perform();\t\t\t\t\t\t\t\t\t\t\t\t\t\t \t\t} catch (Exception e) { \t\tSystem.out.println("\ Could not swipe to top.\ "); \t\te.printStackTrace(); \t\t} \t\tMobileElement flight = null; \t\tif ((flight = iosDriver \t\t\t.findElement(By \t\t\t.name("Airplane Mode"))).isDisplayed()) { \t\t\tflight.click(); \t\t\tnew TouchAction(iosDriver) \t\t\t\t\t.tap(TapOptions \t\t\t.tapOptions() .withPosition(PointOption \t\t\t.point(window.width / 2, 10))).perform(); \t\t} else { \t\t\tthrow new RuntimeException("Flight mode button not found."); \t\t} \t}
Conclusion
Till now, We have discussed about the top 20 most used Appium methods. In the next topic, We will write about the Appium Inspector for Android and iOS. For more details for this section, please refer to this link.
Hi, I am Himadri Das, I am a blogger, and an open source contributor. I have about 11 years of experience in the Information Technology domain. Currently I am working in a Startup Company as Quality Assurance Manager. I have hands-on experience on Appium, Selenium, QTP, Locust, Automation framework, Performance Testing, Functional Testing, Java, python, Shell scripting, MySql, Redis, Kafka etc. Apart from my work and writing blogs, I love to play guitar, love to travel and love to watch cricket and football. If you want to know more about me, please visit my linkedin profile .