Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.appium.java_client.android;

import io.appium.java_client.battery.BatteryInfo;

import java.util.Map;

public class AndroidBatteryInfo extends BatteryInfo {

public AndroidBatteryInfo(Map<String, Object> input) {
super(input);
}

@SuppressWarnings("unchecked")
@Override
public BatteryState getState() {
final int state = ((Long) getInput().get("state")).intValue();
switch (state) {
case 2:
return BatteryState.CHARGING;
case 3:
return BatteryState.DISCHARGING;
case 4:
return BatteryState.NOT_CHARGING;
case 5:
return BatteryState.FULL;
default:
return BatteryState.UNKNOWN;
}
}

public enum BatteryState {
UNKNOWN, CHARGING, DISCHARGING, NOT_CHARGING, FULL
}
}
15 changes: 14 additions & 1 deletion src/main/java/io/appium/java_client/android/AndroidDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@
import static io.appium.java_client.android.AndroidMobileCommandHelper.endTestCoverageCommand;
import static io.appium.java_client.android.AndroidMobileCommandHelper.openNotificationsCommand;
import static io.appium.java_client.android.AndroidMobileCommandHelper.toggleLocationServicesCommand;
import static org.openqa.selenium.remote.DriverCommand.EXECUTE_SCRIPT;

import com.google.common.collect.ImmutableMap;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.CommandExecutionHelper;
import io.appium.java_client.FindsByAndroidUIAutomator;
import io.appium.java_client.LocksDevice;
import io.appium.java_client.PressesKeyCode;
import io.appium.java_client.android.connection.HasNetworkConnection;
import io.appium.java_client.battery.HasBattery;
import io.appium.java_client.remote.MobilePlatform;
import io.appium.java_client.screenrecording.CanRecordScreen;
import io.appium.java_client.service.local.AppiumDriverLocalService;
Expand All @@ -36,6 +40,8 @@
import org.openqa.selenium.remote.http.HttpClient;

import java.net.URL;
import java.util.Collections;
import java.util.Map;

/**
* Android driver implementation.
Expand All @@ -54,7 +60,8 @@ public class AndroidDriver<T extends WebElement>
FindsByAndroidUIAutomator<T>, LocksDevice, HasAndroidSettings, HasDeviceDetails,
HasSupportedPerformanceDataType, AuthenticatesByFinger,
CanRecordScreen, SupportsSpecialEmulatorCommands,
SupportsNetworkStateManagement, ListensToLogcatMessages, HasAndroidClipboard {
SupportsNetworkStateManagement, ListensToLogcatMessages, HasAndroidClipboard,
HasBattery<AndroidBatteryInfo> {

private static final String ANDROID_PLATFORM = MobilePlatform.ANDROID;

Expand Down Expand Up @@ -179,4 +186,10 @@ public void toggleLocationServices() {
CommandExecutionHelper.execute(this, toggleLocationServicesCommand());
}

@SuppressWarnings("unchecked")
@Override
public AndroidBatteryInfo getBatteryInfo() {
return new AndroidBatteryInfo((Map<String, Object>) execute(EXECUTE_SCRIPT, ImmutableMap.of(
"script", "mobile: batteryInfo", "args", Collections.emptyList())));
}
}
31 changes: 31 additions & 0 deletions src/main/java/io/appium/java_client/battery/BatteryInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.appium.java_client.battery;

import java.util.Map;

public abstract class BatteryInfo {
private final Map<String, Object> input;

public BatteryInfo(Map<String, Object> input) {
this.input = input;
}

/**
* @return Battery level in range [0.0, 1.0], where 1.0 means 100% charge.
*/
public double getLevel() {
final Object value = getInput().get("level");
if (value instanceof Long) {
return ((Long) value).doubleValue();
}
return (double) value;
}

/**
* @return Battery state value.
*/
public abstract <T> T getState();

protected Map<String, Object> getInput() {
return this.input;
}
}
29 changes: 29 additions & 0 deletions src/main/java/io/appium/java_client/battery/HasBattery.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.appium.java_client.battery;

import io.appium.java_client.ExecutesMethod;

public interface HasBattery<T extends BatteryInfo> extends ExecutesMethod {

/**
* Retrieves battery info from the device under test.
*
* @return BatteryInfo instance, containing the battery information
*/
T getBatteryInfo();
}
32 changes: 32 additions & 0 deletions src/main/java/io/appium/java_client/ios/IOSBatteryInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.appium.java_client.ios;

import io.appium.java_client.battery.BatteryInfo;

import java.util.Map;

public class IOSBatteryInfo extends BatteryInfo {

public IOSBatteryInfo(Map<String, Object> input) {
super(input);
}

@SuppressWarnings("unchecked")
@Override
public BatteryState getState() {
final int state = ((Long) getInput().get("state")).intValue();
switch (state) {
case 1:
return BatteryState.UNPLUGGED;
case 2:
return BatteryState.CHARGING;
case 3:
return BatteryState.FULL;
default:
return BatteryState.UNKNOWN;
}
}

public enum BatteryState {
UNKNOWN, UNPLUGGED, CHARGING, FULL
}
}
16 changes: 15 additions & 1 deletion src/main/java/io/appium/java_client/ios/IOSDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@

import static io.appium.java_client.MobileCommand.RUN_APP_IN_BACKGROUND;
import static io.appium.java_client.MobileCommand.prepareArguments;
import static org.openqa.selenium.remote.DriverCommand.EXECUTE_SCRIPT;

import com.google.common.collect.ImmutableMap;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.FindsByIosClassChain;
import io.appium.java_client.FindsByIosNSPredicate;
import io.appium.java_client.FindsByIosUIAutomation;
import io.appium.java_client.HidesKeyboardWithKeyName;
import io.appium.java_client.LocksDevice;
import io.appium.java_client.battery.HasBattery;
import io.appium.java_client.remote.MobilePlatform;
import io.appium.java_client.screenrecording.CanRecordScreen;
import io.appium.java_client.service.local.AppiumDriverLocalService;
Expand All @@ -39,6 +43,8 @@

import java.net.URL;
import java.time.Duration;
import java.util.Collections;
import java.util.Map;

/**
* iOS driver implementation.
Expand All @@ -56,7 +62,8 @@ public class IOSDriver<T extends WebElement>
extends AppiumDriver<T>
implements HidesKeyboardWithKeyName, ShakesDevice, HasIOSSettings,
FindsByIosUIAutomation<T>, LocksDevice, PerformsTouchID, FindsByIosNSPredicate<T>,
FindsByIosClassChain<T>, PushesFiles, CanRecordScreen, HasIOSClipboard, ListensToSyslogMessages {
FindsByIosClassChain<T>, PushesFiles, CanRecordScreen, HasIOSClipboard, ListensToSyslogMessages,
HasBattery<IOSBatteryInfo> {

private static final String IOS_PLATFORM = MobilePlatform.IOS;

Expand Down Expand Up @@ -177,6 +184,13 @@ public IOSDriver(Capabilities desiredCapabilities) {
return new InnerTargetLocator();
}

@SuppressWarnings("unchecked")
@Override
public IOSBatteryInfo getBatteryInfo() {
return new IOSBatteryInfo((Map<String, Object>) execute(EXECUTE_SCRIPT, ImmutableMap.of(
"script", "mobile: batteryInfo", "args", Collections.emptyList())));
}

private class InnerTargetLocator extends RemoteTargetLocator {
@Override public Alert alert() {
return new IOSAlert(super.alert());
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/io/appium/java_client/android/BatteryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.appium.java_client.android;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;

import org.junit.Test;

public class BatteryTest extends BaseAndroidTest {

@Test public void veryGettingBatteryInformation() {
final AndroidBatteryInfo batteryInfo = driver.getBatteryInfo();
assertThat(batteryInfo.getLevel(), is(greaterThan(0.0)));
assertThat(batteryInfo.getState(), is(not(AndroidBatteryInfo.BatteryState.UNKNOWN)));
}
}