Top 20 better Appium methods-How to use guide

Appium Tutorial
Appium Tutorial

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) {
	androidDriver.pressKey(new KeyEvent(code));
   	 }
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) {
    	androidDriver.longPressKey(new KeyEvent(code));
        	}

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() {
	return androidDriver.currentActivity();
   	 }

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) {
	    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 + "\"));");
	    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) {
	MobileElement element = (MobileElement) androidDriver.findElement(by);
	int offset = 1;
	Point p = element.getCenter();
	Point location = element.getLocation();
	Dimension size = element.getSize();
	int startX=location.getX() + size.getWidth() - offset;
	int startY=p.getY();
	int endX=location.getX() + offset;
	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 right using Appium methods?

public void swipeRight(By by) {
	MobileElement element = (MobileElement) androidDriver.findElement(by);
	int offset = 1;
	Point p = element.getCenter();
	Point location = element.getLocation();
	Dimension size = element.getSize();
	int startX=location.getX() + offset + 20;
	int startY=p.getY();
	int endX=location.getX() + size.getWidth() - offset;
	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 up using Appium methods?

MobileElement element = (MobileElement) androidDriver.findElement(by);
    	Dimension size = element.getSize();
    	 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){
    	MobileElement element = (MobileElement) androidDriver.findElement(by);
    	Dimension size = element.getSize();
    	int 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) {

	try {
	    Runtime.getRuntime().
exec(androidSdkToolPath 
+ "/adb -s" + deviceId
+ " shell settings put secure location_providers_allowed ' '");

	} catch (Exception e) {
	    
	    e.printStackTrace();
}

How to switch on GPS in Android?

public static void swicthONGPSConnection(String androidSdkToolPath, String deviceId) {

	try {
	    Runtime.getRuntime()
.exec(androidSdkToolPath + "/adb -s" + deviceId
		    + " shell settings put secure location_providers_allowed gps ");

	} catch (Exception e) {
	  
	    e.printStackTrace();
	}
    }

How to switch off Wifi in Android?

public void switchOFFWifiConnection(String androidSdkToolPath,String deviceId) {
		
try {
Runtime.getRuntime()
.exec(androidSdkToolPath+
"/adb -s"+deviceId
+" shell am start -n io.appium.settings/.Settings -e wifi off");
} catch (IOException e) {
			
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) {
			
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() {
	Dimension window = iosDriver.
manage().window().getSize();
	try {
	int startX = window.width / 2;
		int startY = window.height;
		int endY = window.height / 10;
		new TouchAction(iosDriver)
		.press(PointOption.point(startX, startY))
		.waitAction(WaitOptions
		.waitOptions(java.time
.Duration.ofMillis(1000)))
		.moveTo(PointOption
		.point(startX, endY)).release().perform();														
		} catch (Exception e) {
		System.out.println("\nCould not swipe to top.\n");
		e.printStackTrace();
		}
		MobileElement flight = null;
		if ((flight = iosDriver
			.findElement(By
			.name("Airplane Mode"))).isDisplayed()) {
			flight.click();
			new TouchAction(iosDriver)
					.tap(TapOptions
			.tapOptions()
.withPosition(PointOption
			.point(window.width / 2, 10))).perform();
		} else {
			throw new RuntimeException("Flight mode button not found.");
		}
	}

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.

About Himadri Das

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 - https://www.linkedin.com/in/himadri-das-2b6b7421/.

Leave a Comment

Your email address will not be published. Required fields are marked *