How to use getBatteryInfo method of io.appium.java_client.android.AndroidDriver class

Best io.appium code snippet using io.appium.java_client.android.AndroidDriver.getBatteryInfo

Run io.appium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

AppiumUtils.java

Source: AppiumUtils.java Github

copy
1package com.appiumair.util;
2
3import io.appium.java_client.PerformsTouchActions;
4import io.appium.java_client.TouchAction;
5import io.appium.java_client.android.AndroidBatteryInfo;
6import io.appium.java_client.android.AndroidDriver;
7import io.appium.java_client.touch.WaitOptions;
8import io.appium.java_client.touch.offset.PointOption;
9import org.openqa.selenium.WebDriver;
10
11import java.text.DecimalFormat;
12import java.text.NumberFormat;
13import java.time.Duration;
14import java.util.ArrayList;
15import java.util.HashMap;
16import java.util.List;
17import java.util.Map;
18
19/**
20 * Appium各种操作工具类
21 */
22public class AppiumUtils {
23    public AppiumUtils() {}
24
25    /**
26     * @param driver
27     * @module 上滑操作
28     */
29    public static void swipeUp(WebDriver driver) {
30        int width = driver.manage().window().getSize().width;
31        int height = driver.manage().window().getSize().height;
32        TouchAction touchAction = new TouchAction((PerformsTouchActions) driver);
33        PointOption pointOption = PointOption.point(width / 2, height * 3 / 4);
34        touchAction.press(pointOption).waitAction(WaitOptions.waitOptions(Duration.ofMillis(500))).moveTo(PointOption.point(width / 2, height / 4)).release().perform();
35    }
36
37    /**
38     * @param driver
39     * @module 下滑操作
40     */
41    public static void swipeDown(WebDriver driver) {
42        int width = driver.manage().window().getSize().width;
43        int height = driver.manage().window().getSize().height;
44        TouchAction touchAction = new TouchAction((PerformsTouchActions) driver);
45        PointOption pointOption = PointOption.point(width / 2, height / 4);
46        touchAction.press(pointOption).waitAction(WaitOptions.waitOptions(Duration.ofMillis(500))).moveTo(PointOption.point(width / 2, height * 3 / 4)).release().perform();
47    }
48
49    /**
50     * @param driver
51     * @module 左滑操作
52     */
53    public static void swipeLeft(WebDriver driver) {
54        int width = driver.manage().window().getSize().width;
55        int height = driver.manage().window().getSize().height;
56        TouchAction touchAction = new TouchAction((PerformsTouchActions) driver);
57        PointOption pointOption = PointOption.point(width * 4 / 5, height / 2);
58        touchAction.press(pointOption).waitAction(WaitOptions.waitOptions(Duration.ofMillis(500))).moveTo(PointOption.point(width / 5, height / 2)).release().perform();
59    }
60
61    /**
62     * @param driver
63     * @module 右滑操作
64     */
65    public static void swipeRight(WebDriver driver) {
66        int width = driver.manage().window().getSize().width;
67        int height = driver.manage().window().getSize().height;
68        TouchAction touchAction = new TouchAction((PerformsTouchActions) driver);
69        PointOption pointOption = PointOption.point(width / 5, height / 4);
70        touchAction.press(pointOption).waitAction(WaitOptions.waitOptions(Duration.ofMillis(500))).moveTo(PointOption.point(width * 4 / 5, height / 2)).release().perform();
71    }
72
73    /**
74     * 执行测试前,检查各设备电量情况,电量不足发出警告
75     * @param driver
76     * @return 电量状态是否允许可继续执行
77     */
78    public static Double checkBattery(AndroidDriver driver) {
79        String message = "";
80        double batteryLevel = driver.getBatteryInfo().getLevel();
81        String batteryState = driver.getBatteryInfo().getState().name();
82        return batteryLevel;
83    }
84
85    /**
86     * 执行测试前,检查网络状态
87     * @param driver
88     * @return 网络连接状态
89     */
90    public static String checkNetwork(AndroidDriver driver) {
91        List<String> networkList = new ArrayList<>();
92        String message = "";
93        boolean wiFiEnabled = driver.getConnection().isWiFiEnabled();
94        if(!true == wiFiEnabled) {
95            message = "ERROR";
96        }
97        if (true == wiFiEnabled) {
98            message = "WIFI";
99        }
100        return message;
101    }
102}
103
Full Screen

MobileDevice.java

Source: MobileDevice.java Github

copy
1package com.epam.jdi.light.mobile.elements.common;
2
3import io.appium.java_client.AppiumDriver;
4import io.appium.java_client.LocksDevice;
5import io.appium.java_client.MobileDriver;
6import io.appium.java_client.android.AndroidDriver;
7import io.appium.java_client.android.AuthenticatesByFinger;
8import io.appium.java_client.android.HasSupportedPerformanceDataType;
9import io.appium.java_client.battery.BatteryInfo;
10import io.appium.java_client.clipboard.HasClipboard;
11import io.appium.java_client.ios.IOSDriver;
12import io.appium.java_client.ios.PerformsTouchID;
13import io.appium.java_client.ios.ShakesDevice;
14import org.openqa.selenium.DeviceRotation;
15import org.openqa.selenium.ScreenOrientation;
16import org.openqa.selenium.WebDriver;
17import org.openqa.selenium.html5.Location;
18
19import java.time.Duration;
20import java.util.List;
21import java.util.function.Consumer;
22import java.util.function.Function;
23
24import static com.epam.jdi.light.common.Exceptions.runtimeException;
25import static com.epam.jdi.light.driver.WebDriverFactory.getDriver;
26import static com.epam.jdi.light.mobile.MobileUtils.executeDriverMethod;
27
28public class MobileDevice {
29    public static void rotate(DeviceRotation rotation) {
30        executeDriverMethod(AppiumDriver.class, (AppiumDriver driver) -> driver.rotate(rotation));
31    }
32
33    public static DeviceRotation getRotation() {
34        return executeDriverMethod(AppiumDriver.class,
35                (Function<AppiumDriver, DeviceRotation>) AppiumDriver::rotation);
36    }
37
38    public static void rotate(ScreenOrientation orientation) {
39        executeDriverMethod(AppiumDriver.class, (AppiumDriver driver) -> driver.rotate(orientation));
40    }
41
42    public static ScreenOrientation getOrientation() {
43        return executeDriverMethod(AppiumDriver.class,
44                (Function<AppiumDriver, ScreenOrientation>) AppiumDriver::getOrientation);
45    }
46
47    public static void lockDevice() {
48        executeDriverMethod(LocksDevice.class, (Consumer<LocksDevice>) LocksDevice::lockDevice);
49    }
50
51    public static void lockDevice(Duration duration) {
52        executeDriverMethod(LocksDevice.class, (LocksDevice driver) -> driver.lockDevice(duration));
53    }
54
55    public static void unlockDevice() {
56        executeDriverMethod(LocksDevice.class, LocksDevice::unlockDevice);
57    }
58
59    public static boolean isLocked() {
60        return executeDriverMethod(LocksDevice.class, LocksDevice::isDeviceLocked);
61    }
62
63    public static BatteryInfo getBatteryInfo() {
64        WebDriver driver = getDriver();
65        if (driver instanceof IOSDriver) {
66            return ((IOSDriver) driver).getBatteryInfo();
67        } else if (driver instanceof AndroidDriver) {
68            return ((AndroidDriver) driver).getBatteryInfo();
69        } else {
70            throw runtimeException("This method is not supported by the driver. The driver needs to be the instance of either Ios or Android driver");
71        }
72    }
73
74    public static Location getLocation() {
75        return executeDriverMethod(AppiumDriver.class, (Function<AppiumDriver, Location>) AppiumDriver::location);
76    }
77
78    public static void setLocation(Location location) {
79        executeDriverMethod(AppiumDriver.class, (AppiumDriver driver) -> driver.setLocation(location));
80    }
81
82    public static String getDeviceTime() {
83        return executeDriverMethod(MobileDriver.class, (Function<MobileDriver, String>) MobileDriver::getDeviceTime);
84    }
85
86    public static String getDeviceTime(String format) {
87        return executeDriverMethod(MobileDriver.class, (MobileDriver driver) -> driver.getDeviceTime(format));
88    }
89
90    // the next methods are for IOS only
91    public static void shake() {
92        executeDriverMethod(ShakesDevice.class, ShakesDevice::shake);
93    }
94
95    public static void performTouchId(boolean match) {
96        executeDriverMethod(PerformsTouchID.class, (PerformsTouchID driver) -> driver.performTouchID(match));
97    }
98
99    public static void toggleTouchIDEnrollment(boolean enabled) {
100        executeDriverMethod(PerformsTouchID.class, (PerformsTouchID driver) -> driver.toggleTouchIDEnrollment(enabled));
101    }
102
103    // the next methods are for Android only
104    public static void fingerPrint(int fingerPrintId) {
105        executeDriverMethod(AuthenticatesByFinger.class, (AuthenticatesByFinger driver) -> driver.fingerPrint(fingerPrintId));
106    }
107
108    // this method is for Android only
109    public static void setClipBoardText(String text) {
110        executeDriverMethod(MobileDriver.class,
111                (MobileDriver driver) -> (HasClipboard) driver).setClipboardText(text);
112    }
113    // this method is for Android only
114    public static String getClipBoardText() {
115        return executeDriverMethod(MobileDriver.class,
116                (MobileDriver driver) -> (HasClipboard) driver).getClipboardText();
117    }
118
119    public static List<String> getPerformanceDataTypes() {
120        return executeDriverMethod(HasSupportedPerformanceDataType.class, HasSupportedPerformanceDataType::getSupportedPerformanceDataTypes);
121    }
122
123    public static List<List<Object>> getPerformanceData(String packageName, String dataType, int dataReadTimeout) {
124        return executeDriverMethod(HasSupportedPerformanceDataType.class, (HasSupportedPerformanceDataType driver) -> driver.getPerformanceData(packageName, dataType, dataReadTimeout));
125    }
126
127    public static void sendSMS(String phoneNumber, String smsText) {
128        executeDriverMethod(AndroidDriver.class,
129                (AndroidDriver driver) -> driver.sendSMS(phoneNumber, smsText));
130    }
131}
132
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Trigger getBatteryInfo code on LambdaTest Cloud Grid

Execute automation tests with getBatteryInfo on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)