本文主要是讲述,使用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> capabilityList;
private DesiredCapabilities capabilities;
private URL url;
private AndroidDriver androidDriver;
private IOSDriver iOSDriver;
public List> getCapabilityList() {
return capabilityList;
}
public void setCapabilityList(List> 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 getAndroidDriver() {
return androidDriver;
}
public void setAndroidDriver(AndroidDriver androidDriver) {
this.androidDriver = androidDriver;
}
public IOSDriver getiOSDriver() {
return iOSDriver;
}
public void setiOSDriver(IOSDriver iOSDriver) {
this.iOSDriver = iOSDriver;
}
}
然后我们创建一个DriverAdaptor,用来初始化和关闭Driverpackage 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 androidDriver = null;
private IOSDriver 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 arg : (List>) 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 arg : (List>) 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"?>
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">
platformName
#{android.platformName}
deviceName
#{android.deviceName}
platformVersion
#{android.platformVersion}
appPackage
#{android.appPackage}
appActivity
#{android.appActivity}
platformName
#{ios.platformName}
deviceName
#{ios.deviceName}
automationName
#{ios.automationName}
platformVersion
#{ios.platformVersion}
app
#{ios.app}
在这个配置文件中,我们定义了两个.properties,分别用来存放Android,IOS的相关配置
第三个配置文件,通过
来获取加载哪个配置文件
.properties配置文件如下:
android.properties 这里面我们模拟调起微信#APPium Android Driver
platformName:Android
deviceName:HUAWEIP8
platformVersion:6.0
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.propertiesenvironment: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 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);
}
}
测试方法里面只写了个延迟,如果微信能调起来,说明流程上是成功的。