这段时间在进行 公司 android app 自动化测试项目的构建。
在这一过程中,顺便把多机并发运行的功能也大致开发完毕。
下面简单说一下需求来源,实现思路。
private static List<AppiumConfig> appiumConfigs = null;
private static ThreadLocal<AppiumConfig> appiumConfigLocal = new ThreadLocal<AppiumConfig>();
/****/
public static AppiumConfig getAvailableServerConfig(){
if(appiumConfigs == null) {
appiumConfigs = XmlReader.getAppiumConfigList();
}
//获取当前连接的设备
List<String> udidList = CommandUtil.getUdidList("127.0.0.1");
//通过设备uid获取对应的appium config
for (Iterator<AppiumConfig> iterator = appiumConfigs.iterator(); iterator.hasNext();) {
AppiumConfig config = iterator.next();
synchronized (config) {
if (!config.isOccupied()) {
if (udidList != null && udidList.contains(config.getUdid())) {
config.setOccupied(true);
LOG.debug("使用设备:ip " + config.getAddress() + " 设备id " + config.getUdid());
appiumConfigLocal.set(config);
return config;
}
}
}
}
return null;
}
private static ThreadLocal<RemoteWebDriver> remoteWebDriverThreadLocal = new ThreadLocal<>();
public static void initDriver(Configuration conf) {
AppiumConfig appiumConfig = AppiumConfigUtil.getAppiumConfig();
if(appiumConfig == null){
appiumConfig = AppiumConfigUtil.getAvailableServerConfig();
}
//通过 uid 获取对应 desired capability
CapabilityConfig capabilityConfig = AppiumConfigUtil.getCapabilityConfig(appiumConfig.getUdid());
RemoteWebDriver driver = AndroidDevice.getDriver(appiumConfig, capabilityConfig);
remoteWebDriverThreadLocal.set(driver);
}
测试用例代码初始化测试驱动对象
@Override
@BeforeSuite
public void onStart() {
LOG.info("单个套件测试开始,初始化appium driver");
Configuration conf = new Configuration();
DriverManager.initDriver(conf);
}
@Override
@BeforeClass
public void onBeforeClass() {
androidDriver = (AndroidDriver<WebElement>) DriverManager.getDriver();
androidOperateBase = new AndroidOperateBase(androidDriver);
}