adb devices -l 获取手机型号
C:\Users\jeff.xie>adb devices -l
List of devices attached
9b2157cfaedb device product:laurel_sprout model:Mi_A3 device:laurel_sprout transport_id:1
adb -d shell getprop ro.product.brand 获取手机厂商
C:\Users\jeff.xie>adb -d shell getprop ro.product.brand
Xiaomi
package com.welab.automation.projects.demo;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;
import lombok.SneakyThrows;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.io.*;
import java.net.URISyntaxException;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import static io.appium.java_client.service.local.flags.GeneralServerFlag.LOG_LEVEL;
import static io.appium.java_client.service.local.flags.GeneralServerFlag.SESSION_OVERRIDE;
public class GetPhoneTypeByAppium {
public static final String WIN_DRIVER_PATH = "D:/Program Files/nodejs/node.exe";
public static final String WIN_APPIUM_PATH = "C:/Users/jeff.xie/AppData/Roaming/npm/node_modules/appium/build/lib/main.js";
public static final String MAC_DRIVER_PATH = "/usr/local/bin/node";
public static final String MAC_APPIUM_PATH = "/usr/local/lib/node_modules/appium/build/lib/main.js";
int width;
int height;
AndroidDriver driver;
public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException {
GetPhoneTypeByAppium getPhoneTypeByAppium = new GetPhoneTypeByAppium();
getPhoneTypeByAppium.startTest();
}
public DesiredCapabilities setAndroidDesiredCapabilities(){
DesiredCapabilities devices = new DesiredCapabilities();
devices.setCapability("platformName", "Android");
//adb devices -l 获取手机deviceName和手机型号model
//9f1829a8 device product:gemini model:MI_5 device:gemini transport_id:1
devices.setCapability("deviceName","9f1829a8");
//adb shell getprop ro.build.version.release
devices.setCapability("platformVersion", "8.0.0");
devices.setCapability("appPackage","com.android.settings");
devices.setCapability("appActivity","com.android.settings.Settings");
devices.setCapability("automation","uiautomator2");
devices.setCapability("unicodeKeyboard", true);
devices.setCapability("resetKeyboard",true);
return devices;
}
public void getWindowWidthAbdHeight(){
width = driver.manage().window().getSize().width;
height = driver.manage().window().getSize().height;
}
@SneakyThrows
public void startTest() throws URISyntaxException, IOException {
//需要打开appium server,并启动模拟器
//driver= new AndroidDriver(new URL("http://localhost:4723/wd/hub"),setAndroidDesiredCapabilities());
//不需要打开appium桌面版
AppiumDriverLocalService service ;
if (isWindows()) {
service =startAppiumService(WIN_DRIVER_PATH,WIN_APPIUM_PATH);
} else {
service =startAppiumService(MAC_DRIVER_PATH,MAC_APPIUM_PATH);
}
driver = new AndroidDriver(service.getUrl(), setAndroidDesiredCapabilities());
Thread.sleep(5000);
getAndroidPhoneType();
Thread.sleep(1000);
System.out.println("pass");
}
@SneakyThrows
public String getAndroidPhoneType() throws URISyntaxException, IOException{
String phoneType=null;
String cmds = String.format("adb devices -l");
System.out.println(cmds);
Process pcs = Runtime.getRuntime().exec(cmds);
pcs.waitFor();
Thread.sleep(1000);
BufferedInputStream in = new BufferedInputStream(pcs.getInputStream());// 字符流转换字节流
BufferedReader br = new BufferedReader(new InputStreamReader(in));// 这里也可以输出文本日志
String lineStr = null;
while ((lineStr = br.readLine()) != null) {
System.out.println(lineStr);
if(lineStr.contains("model:")){
int index = lineStr.indexOf("model:");
String temp = lineStr.substring(index+"model:".length()).trim();
phoneType = temp.split(" ")[0].trim();
}
}
System.out.println("phoneType: "+phoneType);
return phoneType;
}
public static boolean isWindows(){
String osName = System.getProperty("os.name");
if (osName.startsWith("Windows")) {
return true;
} else {
return false;
}
}
private static AppiumDriverLocalService startAppiumService(String driverPath, String appiumPath) {
AtomicInteger port = new AtomicInteger();
AppiumDriverLocalService service = null;
service = new AppiumServiceBuilder()
.usingAnyFreePort()
.withIPAddress("0.0.0.0")
.withArgument(SESSION_OVERRIDE)
.withArgument(LOG_LEVEL, "error")
.usingDriverExecutable(new File(driverPath))
.withAppiumJS(new File(appiumPath))
.build();
Optional.ofNullable(service).ifPresent(s -> {
s.start();
port.set(s.getUrl().getPort());
});
AppiumDriverLocalService appiumDriverLocalService = service;
return service;
}
}
package com.welab.automation.projects.demo; import io.appium.java_client.android.AndroidDriver; import io.appium.java_client.service.local.AppiumDriverLocalService; import io.appium.java_client.service.local.AppiumServiceBuilder; import lombok.SneakyThrows; import org.openqa.selenium.remote.DesiredCapabilities; import java.io.*; import java.net.URISyntaxException; import java.util.Optional; import java.util.concurrent.atomic.AtomicInteger; import static io.appium.java_client.service.local.flags.GeneralServerFlag.LOG_LEVEL; import static io.appium.java_client.service.local.flags.GeneralServerFlag.SESSION_OVERRIDE; public class GetPhoneTypeByAppium { public static final String WIN_DRIVER_PATH = "D:/Program Files/nodejs/node.exe"; public static final String WIN_APPIUM_PATH = "C:/Users/jeff.xie/AppData/Roaming/npm/node_modules/appium/build/lib/main.js"; public static final String MAC_DRIVER_PATH = "/usr/local/bin/node"; public static final String MAC_APPIUM_PATH = "/usr/local/lib/node_modules/appium/build/lib/main.js"; int width; int height; AndroidDriver driver; public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException { GetPhoneTypeByAppium getPhoneTypeByAppium = new GetPhoneTypeByAppium(); getPhoneTypeByAppium.startTest(); } public DesiredCapabilities setAndroidDesiredCapabilities(){ DesiredCapabilities devices = new DesiredCapabilities(); devices.setCapability("platformName", "Android"); //adb devices -l 获取手机deviceName和手机型号model //9f1829a8 device product:gemini model:MI_5 device:gemini transport_id:1 devices.setCapability("deviceName","9f1829a8"); //adb shell getprop ro.build.version.release 获取手机版本号 devices.setCapability("platformVersion", "8.0.0"); devices.setCapability("appPackage","com.android.settings"); devices.setCapability("appActivity","com.android.settings.Settings"); devices.setCapability("automation","uiautomator2"); devices.setCapability("unicodeKeyboard", true); devices.setCapability("resetKeyboard",true); return devices; } public void getWindowWidthAbdHeight(){ width = driver.manage().window().getSize().width; height = driver.manage().window().getSize().height; } @SneakyThrows public void startTest() throws URISyntaxException, IOException { //需要打开appium server,并启动模拟器 //driver= new AndroidDriver(new URL("http://localhost:4723/wd/hub"),setAndroidDesiredCapabilities()); //不需要打开appium桌面版 AppiumDriverLocalService service ; if (isWindows()) { service =startAppiumService(WIN_DRIVER_PATH,WIN_APPIUM_PATH); } else { service =startAppiumService(MAC_DRIVER_PATH,MAC_APPIUM_PATH); } driver = new AndroidDriver(service.getUrl(), setAndroidDesiredCapabilities()); Thread.sleep(5000); getAndroidPhoneType(); Thread.sleep(1000); System.out.println("pass"); } @SneakyThrows public String getAndroidPhoneType() throws URISyntaxException, IOException{ String phoneType=null; String cmds = String.format("adb devices -l"); System.out.println(cmds); Process pcs = Runtime.getRuntime().exec(cmds); pcs.waitFor(); Thread.sleep(1000); BufferedInputStream in = new BufferedInputStream(pcs.getInputStream());// 字符流转换字节流 BufferedReader br = new BufferedReader(new InputStreamReader(in));// 这里也可以输出文本日志 String lineStr = null; while ((lineStr = br.readLine()) != null) { System.out.println(lineStr); if(lineStr.contains("model:")){ int index = lineStr.indexOf("model:"); String temp = lineStr.substring(index+"model:".length()).trim(); phoneType = temp.split(" ")[0].trim(); } } System.out.println("phoneType: "+phoneType); return phoneType; } public static boolean isWindows(){ String osName = System.getProperty("os.name"); if (osName.startsWith("Windows")) { return true; } else { return false; } } private static AppiumDriverLocalService startAppiumService(String driverPath, String appiumPath) { AtomicInteger port = new AtomicInteger(); AppiumDriverLocalService service = null; service = new AppiumServiceBuilder() .usingAnyFreePort() .withIPAddress("0.0.0.0") .withArgument(SESSION_OVERRIDE) .withArgument(LOG_LEVEL, "error") .usingDriverExecutable(new File(driverPath)) .withAppiumJS(new File(appiumPath)) .build(); Optional.ofNullable(service).ifPresent(s -> { s.start(); port.set(s.getUrl().getPort()); }); AppiumDriverLocalService appiumDriverLocalService = service; return service; } }