package com.appiumair.util;
import io.appium.java_client.PerformsTouchActions;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidBatteryInfo;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;
import org.openqa.selenium.WebDriver;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Appiumåç§æä½å·¥å
·ç±»
*/
public class AppiumUtils {
public AppiumUtils() {}
/**
* @param driver
* @module 䏿»æä½
*/
public static void swipeUp(WebDriver driver) {
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
TouchAction touchAction = new TouchAction((PerformsTouchActions) driver);
PointOption pointOption = PointOption.point(width / 2, height * 3 / 4);
touchAction.press(pointOption).waitAction(WaitOptions.waitOptions(Duration.ofMillis(500))).moveTo(PointOption.point(width / 2, height / 4)).release().perform();
}
/**
* @param driver
* @module 䏿»æä½
*/
public static void swipeDown(WebDriver driver) {
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
TouchAction touchAction = new TouchAction((PerformsTouchActions) driver);
PointOption pointOption = PointOption.point(width / 2, height / 4);
touchAction.press(pointOption).waitAction(WaitOptions.waitOptions(Duration.ofMillis(500))).moveTo(PointOption.point(width / 2, height * 3 / 4)).release().perform();
}
/**
* @param driver
* @module å·¦æ»æä½
*/
public static void swipeLeft(WebDriver driver) {
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
TouchAction touchAction = new TouchAction((PerformsTouchActions) driver);
PointOption pointOption = PointOption.point(width * 4 / 5, height / 2);
touchAction.press(pointOption).waitAction(WaitOptions.waitOptions(Duration.ofMillis(500))).moveTo(PointOption.point(width / 5, height / 2)).release().perform();
}
/**
* @param driver
* @module 峿»æä½
*/
public static void swipeRight(WebDriver driver) {
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
TouchAction touchAction = new TouchAction((PerformsTouchActions) driver);
PointOption pointOption = PointOption.point(width / 5, height / 4);
touchAction.press(pointOption).waitAction(WaitOptions.waitOptions(Duration.ofMillis(500))).moveTo(PointOption.point(width * 4 / 5, height / 2)).release().perform();
}
/**
* æ§è¡æµè¯åï¼æ£æ¥å设å¤çµéæ
åµï¼çµéä¸è¶³ååºè¦å
* @param driver
* @return çµéç¶ææ¯å¦å
许å¯ç»§ç»æ§è¡
*/
public static Double checkBattery(AndroidDriver driver) {
String message = "";
double batteryLevel = driver.getBatteryInfo().getLevel();
String batteryState = driver.getBatteryInfo().getState().name();
return batteryLevel;
}
/**
* æ§è¡æµè¯åï¼æ£æ¥ç½ç»ç¶æ
* @param driver
* @return ç½ç»è¿æ¥ç¶æ
*/
public static String checkNetwork(AndroidDriver driver) {
List<String> networkList = new ArrayList<>();
String message = "";
boolean wiFiEnabled = driver.getConnection().isWiFiEnabled();
if(!true == wiFiEnabled) {
message = "ERROR";
}
if (true == wiFiEnabled) {
message = "WIFI";
}
return message;
}
}
package com.epam.jdi.light.mobile.elements.common;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.LocksDevice;
import io.appium.java_client.MobileDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AuthenticatesByFinger;
import io.appium.java_client.android.HasSupportedPerformanceDataType;
import io.appium.java_client.battery.BatteryInfo;
import io.appium.java_client.clipboard.HasClipboard;
import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.ios.PerformsTouchID;
import io.appium.java_client.ios.ShakesDevice;
import org.openqa.selenium.DeviceRotation;
import org.openqa.selenium.ScreenOrientation;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.html5.Location;
import java.time.Duration;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import static com.epam.jdi.light.common.Exceptions.runtimeException;
import static com.epam.jdi.light.driver.WebDriverFactory.getDriver;
import static com.epam.jdi.light.mobile.MobileUtils.executeDriverMethod;
public class MobileDevice {
public static void rotate(DeviceRotation rotation) {
executeDriverMethod(AppiumDriver.class, (AppiumDriver driver) -> driver.rotate(rotation));
}
public static DeviceRotation getRotation() {
return executeDriverMethod(AppiumDriver.class,
(Function<AppiumDriver, DeviceRotation>) AppiumDriver::rotation);
}
public static void rotate(ScreenOrientation orientation) {
executeDriverMethod(AppiumDriver.class, (AppiumDriver driver) -> driver.rotate(orientation));
}
public static ScreenOrientation getOrientation() {
return executeDriverMethod(AppiumDriver.class,
(Function<AppiumDriver, ScreenOrientation>) AppiumDriver::getOrientation);
}
public static void lockDevice() {
executeDriverMethod(LocksDevice.class, (Consumer<LocksDevice>) LocksDevice::lockDevice);
}
public static void lockDevice(Duration duration) {
executeDriverMethod(LocksDevice.class, (LocksDevice driver) -> driver.lockDevice(duration));
}
public static void unlockDevice() {
executeDriverMethod(LocksDevice.class, LocksDevice::unlockDevice);
}
public static boolean isLocked() {
return executeDriverMethod(LocksDevice.class, LocksDevice::isDeviceLocked);
}
public static BatteryInfo getBatteryInfo() {
WebDriver driver = getDriver();
if (driver instanceof IOSDriver) {
return ((IOSDriver) driver).getBatteryInfo();
} else if (driver instanceof AndroidDriver) {
return ((AndroidDriver) driver).getBatteryInfo();
} else {
throw runtimeException("This method is not supported by the driver. The driver needs to be the instance of either Ios or Android driver");
}
}
public static Location getLocation() {
return executeDriverMethod(AppiumDriver.class, (Function<AppiumDriver, Location>) AppiumDriver::location);
}
public static void setLocation(Location location) {
executeDriverMethod(AppiumDriver.class, (AppiumDriver driver) -> driver.setLocation(location));
}
public static String getDeviceTime() {
return executeDriverMethod(MobileDriver.class, (Function<MobileDriver, String>) MobileDriver::getDeviceTime);
}
public static String getDeviceTime(String format) {
return executeDriverMethod(MobileDriver.class, (MobileDriver driver) -> driver.getDeviceTime(format));
}
// the next methods are for IOS only
public static void shake() {
executeDriverMethod(ShakesDevice.class, ShakesDevice::shake);
}
public static void performTouchId(boolean match) {
executeDriverMethod(PerformsTouchID.class, (PerformsTouchID driver) -> driver.performTouchID(match));
}
public static void toggleTouchIDEnrollment(boolean enabled) {
executeDriverMethod(PerformsTouchID.class, (PerformsTouchID driver) -> driver.toggleTouchIDEnrollment(enabled));
}
// the next methods are for Android only
public static void fingerPrint(int fingerPrintId) {
executeDriverMethod(AuthenticatesByFinger.class, (AuthenticatesByFinger driver) -> driver.fingerPrint(fingerPrintId));
}
// this method is for Android only
public static void setClipBoardText(String text) {
executeDriverMethod(MobileDriver.class,
(MobileDriver driver) -> (HasClipboard) driver).setClipboardText(text);
}
// this method is for Android only
public static String getClipBoardText() {
return executeDriverMethod(MobileDriver.class,
(MobileDriver driver) -> (HasClipboard) driver).getClipboardText();
}
public static List<String> getPerformanceDataTypes() {
return executeDriverMethod(HasSupportedPerformanceDataType.class, HasSupportedPerformanceDataType::getSupportedPerformanceDataTypes);
}
public static List<List<Object>> getPerformanceData(String packageName, String dataType, int dataReadTimeout) {
return executeDriverMethod(HasSupportedPerformanceDataType.class, (HasSupportedPerformanceDataType driver) -> driver.getPerformanceData(packageName, dataType, dataReadTimeout));
}
public static void sendSMS(String phoneNumber, String smsText) {
executeDriverMethod(AndroidDriver.class,
(AndroidDriver driver) -> driver.sendSMS(phoneNumber, smsText));
}
}