JavaApp自动化测试系列[v1.0.0][Appium自启动]

实际上每次测试都手动启动Appium服务并非合理的,当我们将测试代码放到DevOps流程的下游,很难想象还要手动启动Appium服务,实际上可以使用AppiumDriverLocalService在代码中来完成Appium服务的启动和关闭

package com.davieyang.base;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import java.net.URL;

import java.util.logging.Logger;
import org.apache.log4j.PropertyConfigurator;

import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.ITestContext;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;


import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServerHasNotBeenStartedLocallyException;
import java.net.MalformedURLException;


public class BaseActivityII {
    public static Logger log = Logger.getLogger(BaseActivity.class.getName());
    AndroidDriver<AndroidElement> driver;
    private static AppiumDriverLocalService service;

    @BeforeClass
    public void startTest(ITestContext context) throws MalformedURLException{
        log.info("--------启动Appium服务-------");
        PropertyConfigurator.configure("config/log4j.properties");
        service = AppiumDriverLocalService.buildDefaultService();
        service.start();
        if (service == null || !service.isRunning()){
            throw new AppiumServerHasNotBeenStartedLocallyException("Appium server node is not started");
        }
        log.info("--------测试开始----------");
        DesiredCapabilities cap = new DesiredCapabilities();
        cap.setCapability("deviceName", "Remi 3");
        cap.setCapability("udid", "claeae297d72");
        cap.setCapability("platformVersion", "5.1.1");
        cap.setCapability("platformName", "Android");
        cap.setCapability("appPackage", "io.selendroid.testapp");
        cap.setCapability("appActivity", ".HomeScreenActivity");
        cap.setCapability("unicodeKeyboard", "True");
        cap.setCapability("noSign", "True");
        driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), cap);
    }

    @AfterClass
    public void endTest(){
        log.info("-----测试结束-----");
        driver.quit();
        if (service !=null){
            service.stop();
        }
    }

    public AndroidDriver<AndroidElement> getDriver(){
        return driver;
    }
}

已标记关键词 清除标记
表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页