java+appium使用ObjectMapper/HashMap读取yaml文件,配置desiredCapabilities

近期学习java+appium配置启动app的desiredCapabilities时,采用的HashMap读取yaml文件,进行初始化desiredCapabilities

1.原先driver中desiredCapabilities配置代码

public class Driver {
    private static AndroidDriver driver;
    public static void start() throws MalformedURLException {
        final DesiredCapabilities desiredCapabilities = new DesiredCapabilities();

       desiredCapabilities.setCapability("platformName","android");
      desiredCapabilities.setCapability("devicesName","6EB0217722001760");
       desiredCapabilities.setCapability("appPackage","com.xueqiu.android");
      desiredCapabilities.setCapability("appActivity",".view.WelcomeActivityAlias");
       desiredCapabilities.setCapability("autoGrantPermissions",true);
       desiredCapabilities.setCapability("noReset",true);
        URL remoteUrl = new URL(http://127.0.0.1:4723/wd/hub);
        driver = new AndroidDriver(remoteUrl, desiredCapabilities);
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    }
    public static AppiumDriver<WebElement> getCurrentDriver(){

        return driver;
    }
}

2.新方案

2.1 建立XueqiuConfig文件用于读取yaml文件中初始化启动app的配置desiredCapabilities

public class XueqiuConfig {
//    public修饰变量,让其他类也可被访问
    public String username="18814811148";
    public String password="123456";
    public String app="https://xxxxxx/xueqiu.apk";
    public String appium="";
    public int wait=6;
    public HashMap<String, Object> capabilities=new HashMap<String, Object>();
    //创建HashMap对象,将HashMap引用赋值给变量capabilities, Object是所有类型的父类,capabilities为数组形式{},里面每个元素是键值对形式存在
    static XueqiuConfig load(String path){
        ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); 
        //ObjectMapper类是Jackson库的主要类。它称为ObjectMapper的原因是因为它将JSON映射到Java对象(反序列化),或将Java对象映射到JSON(序列化)
        //new YAMLFactory()用于切换yaml状态,后面用readvalue读取yaml文件
        try {
            XueqiuConfig config = mapper.readValue(XueqiuConfig.class.getResource(path), XueqiuConfig.class);
            return config;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

关于HashMap讲解
https://www.cnblogs.com/wyhhh/p/11286178.html
源码讲解
https://wiki.jikexueyuan.com/project/java-collection/hashmap.html

关于ObjectMapper(new YAMLFactory())讲解
https://www.cnblogs.com/hogwarts/p/12981255.html
https://blog.csdn.net/lpfasd123/article/details/89346313

2.2 driver更改

public class Driver {
    private static AndroidDriver driver;
    public static void start() throws MalformedURLException {
        final DesiredCapabilities desiredCapabilities = new DesiredCapabilities();

        final XueqiuConfig config = XueqiuConfig.load("/xueqiu.yaml");
//       config.capabilities, capabilities为HashMap引用赋值的变量,keySet返回Set此映射中包含的键的视图
//        forEach进行遍历,java遍历map方式
        config.capabilities.keySet().forEach(key->{
            Object value=config.capabilities.get(key); // capabilities.get(key),HashMap对象通过key获取value值
            desiredCapabilities.setCapability(key, value);
        });

        URL remoteUrl = new URL(config.appium);
        driver = new AndroidDriver(remoteUrl, desiredCapabilities);
        driver.manage().timeouts().implicitlyWait(config.wait, TimeUnit.SECONDS);
//        WebDriverWait wait =new WebDriverWait(driver, 10);
//        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("元素")));

    }
    public static AppiumDriver<WebElement> getCurrentDriver(){

        return driver;
    }
}

foreach讲解
https://www.cnblogs.com/bingyimeiling/p/10741761.html

2.3yaml文件

---
username: xxx
password: dddd
app: 1.apk
appium: http://127.0.0.1:4723/wd/hub
wait: 15
capabilities:
  platformName: android
  devicesName: 6EB0217722001760
  appPackage: com.xueqiu.android
  appActivity: .view.WelcomeActivityAlias
  autoGrantPermissions: true

2.4 class XueqiuConfigTest

class XueqiuConfigTest {

    @Test
    void load() {
        XueqiuConfig config=XueqiuConfig.load("/xueqiu.yaml");
        assertThat(config, not(equalTo(null)));
        System.out.println(config);
        System.out.println(config.username);
        System.out.println("打印appium值");
        System.out.println(config.appium);
        System.out.println("打印capabilities");
        System.out.println(config.capabilities);
//        System.out.println(config.capabilities.get("aaa"));


    }
}
上一篇:我们常听到的WAL到底是什么


下一篇:centos下pip install fate-client问题