搜索
首页Javajava教程基于SPRING的APPIUM配置应用详细介绍

本文主要是讲述,使用Spring框架,优化Appium的Driver调用,并将写在代码里的大量配置参数定义到配置文件当中,还可灵活的控制调用AndroidDriver还是IOSDriver。

Spring的环境,请自行搭建。

下面的用例是基于spring4.3,appium java client 4.1.2, selenium 3.0.1

首先,我们写一个Driver,定义一些Bean属性,这些属性都和创建AndroidDriver,IOSDriver相关:

package test;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.remote.DesiredCapabilities;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.ios.IOSDriver;

@Component
@Scope("prototype")
public class Driver {

    private List<ArrayList<String>> capabilityList;
    private DesiredCapabilities capabilities;
    private URL url;
    private AndroidDriver<MobileElement> androidDriver;
    private IOSDriver<MobileElement> iOSDriver;

    public List<ArrayList<String>> getCapabilityList() {
        return capabilityList;
    }

    public void setCapabilityList(List<ArrayList<String>> capabilityList) {
        this.capabilityList = capabilityList;
    }

    public DesiredCapabilities getCapabilities() {
        return capabilities;
    }

    public void setCapabilities(DesiredCapabilities capabilities) {
        this.capabilities = capabilities;
    }

    public URL getUrl() {
        return url;
    }

    public void setUrl(URL url) {
        this.url = url;
    }

    public AndroidDriver<MobileElement> getAndroidDriver() {
        return androidDriver;
    }

    public void setAndroidDriver(AndroidDriver<MobileElement> androidDriver) {
        this.androidDriver = androidDriver;
    }

    public IOSDriver<MobileElement> getiOSDriver() {
        return iOSDriver;
    }

    public void setiOSDriver(IOSDriver<MobileElement> iOSDriver) {
        this.iOSDriver = iOSDriver;
    }

}

然后我们创建一个DriverAdaptor,用来初始化和关闭Driver

package test;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.openqa.selenium.remote.DesiredCapabilities;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.ios.IOSDriver;

@Component
public class DriverAdaptor {

    private AndroidDriver<MobileElement> androidDriver = null;
    private IOSDriver<MobileElement> iOSDriver = null;

    @Resource
    private Driver driver;

    public Driver getDriver() {
        return driver;
    }

    public void setDriver(Driver driver) {
        this.driver = driver;
    }

    @Resource
    ApplicationContext ctx;

    @Value("#{baseconfig.environment}")
    String environment;

    @SuppressWarnings("unchecked")
    public void initAndroidDriverByConfigFile() throws Exception {
        for (ArrayList<String> arg : (List<ArrayList<String>>) ctx.getBean(environment)) {
            ctx.getBean("capabilities", DesiredCapabilities.class).setCapability(arg.get(0), arg.get(1));
        }
        androidDriver = new AndroidDriver<>(driver.getUrl(), driver.getCapabilities());
        driver.setAndroidDriver(androidDriver);
    }

    public void quitAndoridSession() {
        if (androidDriver != null)
            androidDriver.quit();
    }

    @SuppressWarnings("unchecked")
    public void initIOSDriverByConfigFile() throws Exception {
        for (ArrayList<String> arg : (List<ArrayList<String>>) ctx.getBean(environment)) {
            ctx.getBean("capabilities", DesiredCapabilities.class).setCapability(arg.get(0), arg.get(1));
        }
        iOSDriver = new IOSDriver<>(driver.getUrl(), driver.getCapabilities());
        driver.setiOSDriver(iOSDriver);
    }

    public void quitIOSService() {
        if (iOSDriver != null)
            iOSDriver.quit();
    }

}

接着,我们把Spring的配置文件写一下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

    <!-- 组件扫描 -->
    <context:component-scan base-package="test"></context:component-scan>
    <!-- aspect -->
    <aop:aspectj-autoproxy proxy-target-class="false" />

    <!-- 定义配置文件properties -->
    <util:properties id="android" location="classpath:android.properties" />
    <util:properties id="ios" location="classpath:ios.properties" />
    <util:properties id="baseconfig" location="classpath:baseconfig.properties" />
    <!-- Android -->
    <util:list id="androidCapabilityList">
        <list>
            <value>platformName</value>
            <value>#{android.platformName}</value>
        </list>
        <list>
            <value>deviceName</value>
            <value>#{android.deviceName}</value>
        </list>
        <list>
            <value>platformVersion</value>
            <value>#{android.platformVersion}</value>
        </list>
        <list>
            <value>appPackage</value>
            <value>#{android.appPackage}</value>
        </list>
        <list>
            <value>appActivity</value>
            <value>#{android.appActivity}</value>
        </list>
    </util:list>
    <!-- IOS -->
    <util:list id="iOScapabilityList">
        <list>
            <value>platformName</value>
            <value>#{ios.platformName}</value>
        </list>
        <list>
            <value>deviceName</value>
            <value>#{ios.deviceName}</value>
        </list>
        <list>
            <value>automationName</value>
            <value>#{ios.automationName}</value>
        </list>
        <list>
            <value>platformVersion</value>
            <value>#{ios.platformVersion}</value>
        </list>
        <list>
            <value>app</value>
            <value>#{ios.app}</value>
        </list>
    </util:list>
    <!-- appium driver -->
    <bean id="url" class="java.net.URL">
        <constructor-arg index="0" value="#{baseconfig.url}"></constructor-arg>
    </bean>
    <bean id="capabilities" class="org.openqa.selenium.remote.DesiredCapabilities"></bean>
    <bean id="driver" class="test.Driver">
        <property name="capabilityList" ref="#{baseconfig.environment}"></property>
        <property name="capabilities" ref="capabilities"></property>
        <property name="url" ref="url"></property>
    </bean>
</beans>

在这个配置文件中,我们定义了两个.properties,分别用来存放Android,IOS的相关配置

第三个配置文件,通过

7fcafba8fc204cb68142ffcf0de02a94dde4123f2ed5a21d0bae333af89830f9

来获取加载哪个配置文件

 

.properties配置文件如下:

android.properties 这里面我们模拟调起微信

#APPium Android Driver
platformName:Android
deviceName:HUAWEIP8
platformVersion:6.0
# wechat
appPackage:com.tencent.mm
appActivity:.ui.LauncherUI

ios.properties .app的路径请自己配一下

#APPium IOS Driver
platformName:iOS
deviceName:iPhone Simulator
automationName:XCUITest
platformVersion:10.2
app:/X/X/X.app

baseconfig.properties

environment:androidCapabilityList
# Driver url
url:http://127.0.0.1:4723/wd/hub

最后写一个测试类看一下能不能调起微信

package test;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;

public class TestDemo {

    static ApplicationContext ctx;
    static AndroidDriver<MobileElement> driver;
    static DriverAdaptor driverAdaptor;

    @Before
    public void before() throws Exception {
        ctx = new ClassPathXmlApplicationContext("spring.xml");
        driverAdaptor = ctx.getBean("driverAdaptor", DriverAdaptor.class);
        driverAdaptor.initAndroidDriverByConfigFile();
    }

    @After
    public void after() throws Exception {
        if (driverAdaptor != null)
            driverAdaptor.quitAndoridSession();
    }

    @Test
    public void test1() throws InterruptedException {
        Thread.sleep(5000);
    }

}

测试方法里面只写了个延迟,如果微信能调起来,说明流程上是成功的。

以上是基于SPRING的APPIUM配置应用详细介绍的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
如何将Maven或Gradle用于高级Java项目管理,构建自动化和依赖性解决方案?如何将Maven或Gradle用于高级Java项目管理,构建自动化和依赖性解决方案?Mar 17, 2025 pm 05:46 PM

本文讨论了使用Maven和Gradle进行Java项目管理,构建自动化和依赖性解决方案,以比较其方法和优化策略。

如何使用适当的版本控制和依赖项管理创建和使用自定义Java库(JAR文件)?如何使用适当的版本控制和依赖项管理创建和使用自定义Java库(JAR文件)?Mar 17, 2025 pm 05:45 PM

本文使用Maven和Gradle之类的工具讨论了具有适当的版本控制和依赖关系管理的自定义Java库(JAR文件)的创建和使用。

如何使用咖啡因或Guava Cache等库在Java应用程序中实现多层缓存?如何使用咖啡因或Guava Cache等库在Java应用程序中实现多层缓存?Mar 17, 2025 pm 05:44 PM

本文讨论了使用咖啡因和Guava缓存在Java中实施多层缓存以提高应用程序性能。它涵盖设置,集成和绩效优势,以及配置和驱逐政策管理最佳PRA

如何将JPA(Java持久性API)用于具有高级功能(例如缓存和懒惰加载)的对象相关映射?如何将JPA(Java持久性API)用于具有高级功能(例如缓存和懒惰加载)的对象相关映射?Mar 17, 2025 pm 05:43 PM

本文讨论了使用JPA进行对象相关映射,并具有高级功能,例如缓存和懒惰加载。它涵盖了设置,实体映射和优化性能的最佳实践,同时突出潜在的陷阱。[159个字符]

Java的类负载机制如何起作用,包括不同的类载荷及其委托模型?Java的类负载机制如何起作用,包括不同的类载荷及其委托模型?Mar 17, 2025 pm 05:35 PM

Java的类上载涉及使用带有引导,扩展程序和应用程序类负载器的分层系统加载,链接和初始化类。父代授权模型确保首先加载核心类别,从而影响自定义类LOA

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 个月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
1 个月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
1 个月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它们
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器