How to use AndroidBatteryInfo class of io.appium.java_client.android package

Best io.appium code snippet using io.appium.java_client.android.AndroidBatteryInfo

Run io.appium automation tests on LambdaTest cloud grid

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

EmulatorPower.java

Source: EmulatorPower.java Github

copy
1package com.epam.jdi.light.mobile.elements.common;
2
3import io.appium.java_client.android.AndroidBatteryInfo;
4import io.appium.java_client.android.AndroidDriver;
5import io.appium.java_client.android.PowerACState;
6
7import static com.epam.jdi.light.driver.WebDriverFactory.getDriver;
8import static com.epam.jdi.light.mobile.MobileUtils.executeDriverMethod;
9
10public class EmulatorPower {
11
12    public static void setPowerCapacity(int value) {
13        executeDriverMethod(AndroidDriver.class, (AndroidDriver driver)
14                -> driver.setPowerCapacity(value));
15    }
16
17    public static void setPowerAC(PowerACState powerACState) {
18        executeDriverMethod(AndroidDriver.class, (AndroidDriver driver)
19                -> driver.setPowerAC(powerACState));
20    }
21
22
23    public AndroidBatteryInfo.BatteryState getStatusChargingAndroid() {
24        return ((AndroidDriver) getDriver()).getBatteryInfo().getState();
25    }
26
27}
28
Full Screen

BatteryTest.java

Source: BatteryTest.java Github

copy
1package AppiumProject;
2
3import io.appium.java_client.android.AndroidBatteryInfo;
4import io.appium.java_client.android.AndroidDriver;
5import io.appium.java_client.android.AndroidElement;
6import org.testng.annotations.Test;
7
8import java.net.MalformedURLException;
9
10import static org.hamcrest.MatcherAssert.assertThat;
11import static org.hamcrest.Matchers.greaterThan;
12import static org.hamcrest.Matchers.is;
13import static org.hamcrest.Matchers.not;
14
15public class BatteryTest extends AndroidTest {
16
17
18
19    @Test
20    public void veryGettingBatteryInformation() throws MalformedURLException {
21        AndroidDriver<AndroidElement> driver = capabilities();
22
23        final AndroidBatteryInfo batteryInfo = driver.getBatteryInfo();
24        assertThat(batteryInfo.getLevel(), is(greaterThan(0.0)));
25        assertThat(batteryInfo.getState(), is(not(AndroidBatteryInfo.BatteryState.UNKNOWN)));
26
27        System.out.println(batteryInfo.getState());
28
29    }
30}
Full Screen

ConnectToDevice.java

Source: ConnectToDevice.java Github

copy
1package intro;
2
3import io.appium.java_client.android.AndroidBatteryInfo;
4import io.appium.java_client.android.AndroidDriver;
5import io.appium.java_client.android.AndroidElement;
6import org.junit.Test;
7import org.openqa.selenium.remote.DesiredCapabilities;
8
9import java.net.MalformedURLException;
10import java.net.URL;
11
12public class ConnectToDevice {
13
14    @Test
15    public void test1() throws MalformedURLException {
16        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
17        desiredCapabilities.setCapability("platformName","android");
18        desiredCapabilities.setCapability("deviceName","cigdem");
19        desiredCapabilities.setCapability("automationName","uiautomator2");
20
21        URL serverUrl = new URL("http://localHost:4723/wd/hub");
22
23        AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(serverUrl, desiredCapabilities);
24
25        AndroidBatteryInfo batteryInfo = driver.getBatteryInfo();
26        System.out.println(batteryInfo.getState().name());
27        System.out.println(driver.getDeviceTime());
28        System.out.println(driver.getStatus());
29    }
30}
31
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

Most used methods in AndroidBatteryInfo

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

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)