本文主要是讲述,使用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的相关配置

第三个配置文件,通过

<property name="capabilityList" ref="#{baseconfig.environment}"></property>

来获取加载哪个配置文件

.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配置应用的更多相关文章

  1. 实战:基于 Spring 的应用配置如何迁移至阿里云应用配置管理 ACM

    最近遇到一些开发者朋友,准备将原有的Java Spring的应用配置迁移到 阿里云应用配置管理 ACM 中.迁移过程中,遇到不少有趣的问题.本文将通过一个简单的样例来还原迁移过程中遇到的问题和相关解决 ...

  2. 基于spring的shiro配置

    shiro是一个特别简单,易用的框架,在此记录一下shiro的使用配置. 首先,创建四张表:user  role  user_role  permission,分别为用户.角色.用户与角色关系表和权限 ...

  3. 简单两步快速实现shiro的配置和使用,包含登录验证、角色验证、权限验证以及shiro登录注销流程(基于spring的方式,使用maven构建)

    前言: shiro因为其简单.可靠.实现方便而成为现在最常用的安全框架,那么这篇文章除了会用简洁明了的方式讲一下基于spring的shiro详细配置和登录注销功能使用之外,也会根据惯例在文章最后总结一 ...

  4. 基于注解的Spring多数据源配置和使用

    前一段时间研究了一下spring多数据源的配置和使用,为了后期从多个数据源拉取数据定时进行数据分析和报表统计做准备.由于之前做过的项目都是单数据源的,没有遇到这种场景,所以也一直没有去了解过如何配置多 ...

  5. 基于xml的Spring多数据源配置和使用

    上一篇讲了<基于注解的Spring多数据源配置和使用>,通过在类或者方法上添加@DataSource注解就可以指定某个数据源.这种方式的优点是控制粒度细,也更灵活. 但是当有些时候项目分模 ...

  6. 基于注解的Spring AOP的配置和使用

    摘要: 基于注解的Spring AOP的配置和使用 AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不 ...

  7. 基于Spring的可扩展Schema进行开发自定义配置标签支持

    一.背景 最近和朋友一起想开发一个类似alibaba dubbo的功能的工具,其中就用到了基于Spring的可扩展Schema进行开发自定义配置标签支持,通过上网查资料自己写了一个demo.今天在这里 ...

  8. Spring IoC — 基于Java类的配置

    普通的POJO只要标注@Configuration注解,就可以为Spring容器提供Bean定义的信息了,每个标注了@Bean的类方法都相当于提供一个Bean的定义信息. 基于Java类的配置方法和基 ...

  9. Spring Boot自动配置源码解析(基于Spring Boot 2.0.2.RELEASE)

    在Spring Boot官方介绍中,首一段话是这样的(如下图).我们可以大概了解到其所表达的含义:我们可以利用Spring Boot写很少的配置来创建一个非常方便的基于Spring整合第三方类库的单体 ...

随机推荐

  1. 深入探究stm32GPIO口模式(类比51)

    关于STM32GPIO口的8种工作模式,我们先引出一些问题? STM32GPIO口如果既要输入又要输出怎么办? 1.浮空输入模式 上图红色的表示便是浮空输入的过程,外部输入时0读出的就是0,外部输入时 ...

  2. jsp的开发模式

    JSP 存在两种 开发模式1.Model1 : JSP + JavaBean * 不适合开发业务逻辑特别复杂web应用 ----- 业务逻辑复杂,控制代码多,而在jsp中编写控制代码,十分不便 *JS ...

  3. SpringCloud网关ZUUL集成consul

    最近一直在搞基于springcloud的微服务开发,为了不限定微服务开发语言,服务发现决定采用consul不多说上代码 pom文件 <project xmlns="http://mav ...

  4. 基于Flink的windows--简介

    新的一年,新的开始,新的习惯,现在开始. 1.简介 Flink是德国一家公司名为dataArtisans的产品,2016年正式被apache提升为顶级项目(地位同spark.storm等开源架构).并 ...

  5. hibernate持久化框架

    Hibernate是一个优秀的持久化框架 瞬时状态:保存在内存的程序数据,程序退出后,数据就消失了,称为瞬时状态 持久状态:保存在磁盘上的程序数据,程序退出后依然存在,称为程序数据的持久状态 持久化: ...

  6. 浅谈C#抽象类

    抽象类 先说个事,一个类实例化为一个实例.就是一只狗,实例化一下,就成了一只哈士奇(具体的二哈).但是,一个动物类实例化呐,成了啥? 压根就不能实例化.这,就是抽象类的概念引入. 概念:C#允许把类和 ...

  7. zepto.js介绍

    是一个阉割版的jQuery zepto不支持jQuery过于复杂的选择器,比如:first :last :eq zepto如果要用动画必须再次引包 zepto能将css3中transition支持的动 ...

  8. [Linux] PHP程序员玩转Linux系列-升级PHP到PHP7

    1.PHP程序员玩转Linux系列-怎么安装使用CentOS 2.PHP程序员玩转Linux系列-lnmp环境的搭建 3.PHP程序员玩转Linux系列-搭建FTP代码开发环境 4.PHP程序员玩转L ...

  9. Centos7完全分布式搭建Hadoop2.7.3

    (一)软件准备 1,hadoop-2.7.3.tar.gz(包) 2,三台机器装有cetos7的机子 (二)安装步骤 1,给每台机子配相同的用户 进入root : su root ---------& ...

  10. Liunx的DHCP配置

    1.DHCP简介 (1)DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)是一个简化主机IP地址分配管理的TCP/IP标准协议,用户可以利用DHCP服 ...