Home  >  Article  >  Java  >  Detailed introduction to APPIUM configuration application based on SPRING

Detailed introduction to APPIUM configuration application based on SPRING

高洛峰
高洛峰Original
2017-03-19 11:16:381894browse

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

}

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

}

Then, we write the Spring configuration file:




    
    
    
    

    
    
    
    
    
    
        
            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}
        
    
    
    
        
    
    
    
        
        
        
    

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

e589a5cbd5a225a84b6486834674c2abdde4123f2ed5a21d0bae333af89830f9

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

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

baseconfig.properties

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

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

}

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!

Statement:
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