Java利用Appium实现获取Android手机型号

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;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值