Home > Java > javaTutorial > body text

Detailed introduction to APPIUM configuration application based on SPRING

高洛峰
Release: 2017-03-19 11:16:38
Original
2031 people have browsed it

This article mainly talks about using the Spring framework to optimize Appium's Driver call and define a large number of configuration parameters written in the code into the configuration file, which can also be flexibly Control whether to call AndroidDriver or IOSDriver.

Spring environment, please build it yourself.

The following use case is based on spring4.3, appium java client 4.1.2, selenium 3.0.1

First, we write a Driver and define some Bean properties, which are the same as creating AndroidDriver , IOSDriver related:

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;
    }

}
Copy after login

Then we create a DriverAdaptor to initialize and close the 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();
    }

}
Copy after login

Then, we write the Spring configuration file:

<?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>
Copy after login

In this In the configuration file, we have defined two .properties, which are used to store Android and IOS related configurations

The third configuration file is passed


To get which configuration file to load

.properties configuration file is as follows:

android.properties Here we simulate calling WeChat

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

ios.properties .app. Please configure the path yourself

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

baseconfig.properties

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

Finally, write a test class to see if WeChat can be called up

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);
    }

}
Copy after login

Only a delay is written in the test method. If WeChat can be called up, it means the process is successful.

The above is the detailed content of Detailed introduction to APPIUM configuration application based on SPRING. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!