Appium Wrapper for Java

转载 2015年11月17日 16:27:45

转载地址:https://testerhome.com/topics/1864


基于Appium的移动测试框架,Appium的Wrapper很关键。Talk is cheap, Java版本代码如下:

package com.ctrip.cap.lanucher;

/**
 * A service wrapper for Appium Server
 * 
 * @author ltyao
 *
 */
public class AppiumServer {

    private static final Logger logger = LoggerFactory
            .getLogger(AppiumServer.class);
    private static final long START_TIMEOUT_MILLISECONDS = 30000;
    private static final HttpClient httpClient = HttpClients.createDefault();
    private static final String STATUS_PATH = "/wd/hub/status";
    private static final String PATH = "/wd/hub";
    private String ip = "localhost";

    // private volatile boolean started = false;

    private Process process;

    private int appiumPort = -1;
    private int bootstrapPort = -1;
    private int selendroidPort = -1;
    private int chromeDriverPort = -1;
    private int robotPort = -1;

    private String appiumLog;

    private AndroidDevice device;

    public AppiumServer() {
    }

    public AppiumServer(AndroidDevice device) {
        this.device = device;
    }

    public void stopAppium() {
        try {
            WinProcess winp = new WinProcess(process);
            logger.warn("try to kill process {} Recursively", winp.getPid());
            winp.killRecursively();
        } catch (Exception e) {
            logger.warn("stopAppium", e);
        }
    }


    public void startAppium() {
        startAppium(START_TIMEOUT_MILLISECONDS);
    }

    public void startAppium(long milliseconds) {
        List<String> cmds = buildCmds();

        final ProcessBuilder pb = new ProcessBuilder(cmds)
                .redirectErrorStream(true);

        logger.debug("start appium with {}", cmds.toString());

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    process = pb.start();

                    int exitvalue = process.waitFor();

                    logger.warn("appium server exitvalue {}", exitvalue);
                } catch (Exception e) {
                    logger.warn("startAppium", e);
                }

            }
        }).start();

        long start = System.currentTimeMillis();
        boolean state = isRunning();
        while (!state) {
            long end = System.currentTimeMillis();
            if (end - start > milliseconds) {
                this.stopAppium();
                throw new AppiumTimeoutException("Appium can't be lanuched in "
                        + milliseconds + " seconds");
            }
            state = isRunning();
        }

        logger.warn("started appium server {}", this);
    }

    public void restartAppium() {
        this.stopAppium();
        this.startAppium();
    }

    public boolean isRunning() {
        try {
            URI uri = new URIBuilder().setScheme("http").setHost(ip)
                    .setPort(appiumPort).setPath(STATUS_PATH).build();

            HttpGet httpget;
            HttpResponse response;
            httpget = new HttpGet(uri);
            response = httpClient.execute(httpget);
            HttpEntity entity = response.getEntity();
            String rs = EntityUtils.toString(entity);
            JsonElement json = new JsonParser().parse(rs);
            int status = json.getAsJsonObject().get("status").getAsInt();
            return status == 0;

        } catch (Exception e) {
            // logger.warn("isRunning", e);
            return false;
        }

    }

    public URL getURL() {
        URI uri;
        try {
            uri = new URIBuilder().setScheme("http").setHost(ip)
                    .setPort(appiumPort).setPath(PATH).build();
            return uri.toURL();
        } catch (URISyntaxException | MalformedURLException e) {
            throw new CapException("getURL", e);
        }

    }


    public int getPid() {
        try {
            WinProcess winp = new WinProcess(process);
            return winp.getPid();
        } catch (Exception e) {
            logger.warn("", e);
            return -1;
        }
    }

    /**
     * need to be checked
     * 
     * @return
     */
    private List<String> buildCmds() {

        appiumPort = AvailablePortFinder.getNextAvailable();
        chromeDriverPort = AvailablePortFinder.getNextAvailable();
        bootstrapPort = AvailablePortFinder.getNextAvailable();
        selendroidPort = AvailablePortFinder.getNextAvailable();

        List<String> cmds = new LinkedList<>();
        cmds.add("appium.cmd");
        cmds.add(String.format("--port=%d", appiumPort));
        cmds.add(String.format("--chromedriver-port=%d", chromeDriverPort));
        cmds.add(String.format("--selendroid-port=%d", selendroidPort));
        cmds.add(String.format("--bootstrap-port=%d", bootstrapPort));
        this.appiumLog = Environment.appiumLog(device.getSerialNumber());
        String qappiumLog = StringUtils.quoteArgument(this.appiumLog);
        cmds.add(String.format("--log=%s", qappiumLog));
        cmds.add("--log-timestamp");

        // switch (config.getDriverType()) {
        // case Selendroid:
        // selendroidPort = AvailablePortFinder.getNextAvailable();
        // cmds.add(String.format("--selendroid-port=%d", selendroidPort));
        //
        // break;
        // case ChromeDriver:
        // chromeDriverPort = AvailablePortFinder.getNextAvailable();
        // cmds.add(String.format("--chromedriver-port=%d", chromeDriverPort));
        //
        // default:
        // break;
        // }
        return cmds;
    }

    public int getAppiumPort() {
        return appiumPort;
    }

    public void setAppiumPort(int appiumPort) {
        this.appiumPort = appiumPort;
    }

    public int getBootstrapPort() {
        return bootstrapPort;
    }

    public void setBootstrapPort(int bootstrapPort) {
        this.bootstrapPort = bootstrapPort;
    }

    public int getSelendroidPort() {
        return selendroidPort;
    }

    public void setSelendroidPort(int selendroidPort) {
        this.selendroidPort = selendroidPort;
    }

    public int getChromeDriverPort() {
        return chromeDriverPort;
    }

    public void setChromeDriverPort(int chromeDriverPort) {
        this.chromeDriverPort = chromeDriverPort;
    }

    public int getRobotPort() {
        return robotPort;
    }

    public void setRobotPort(int robotPort) {
        this.robotPort = robotPort;
    }

    public String getAppiumLog() {
        return appiumLog;
    }

    public Device getDevice() {
        return device;
    }

    public void setDevice(AndroidDevice device) {
        this.device = device;
    }

    @Override
    public String toString() {
        return "AppiumServer [pid=" + getPid() + ",ip=" + ip + ", appiumPort="
                + appiumPort + ", bootstrapPort=" + bootstrapPort
                + ", selendroidPort=" + selendroidPort + ", chromeDriverPort="
                + chromeDriverPort + ", appiumLog=" + appiumLog + "]";
    }

}

Windows 平台上的使用 Java 语言实现 appium 自动化程序 for Android(完整版)

首先说下楼主也是个新手,摸索了好几天天 终于完成了第一个appium程序,帖子写的不合理的地方,请大家轻喷,指导,大家一起学习。写这个帖子就是为了方便新手能快速入门,也算是自己的一种巩固吧。好了,开始...
  • wanglha
  • wanglha
  • 2015年01月08日 15:47
  • 1310

Saucelabs+Java+TestNG+Appium+Maven+Git+Jenkins+ReportNG for Android 自动化测试

最近学了利用Appium+Maven+TestNG(ReportNG)搭建全自动化测试环境,使用https://saucelabs.com/ 网站提供的VM平台(可以手动测试与自动化测试,并且可以多...

Windows 平台上的使用 Java 语言实现 appium 自动化程序 for Android(完整版)

首先说下楼主也是个新手,摸索了好几天天 终于完成了第一个appium程序,帖子写的不合理的地方,请大家轻喷,指导,大家一起学习。写这个帖子就是为了方便新手能快速入门,也算是自己的一种巩固吧。好了,开始...
  • wanglha
  • wanglha
  • 2014年12月31日 13:20
  • 677

Appium For Android and IOS By Java

目录: 前言 项目介绍 项目结构 如何编写一个Case Android Example IOS Example 总结 一. 前言 自动化测试趋势近几年也越来越火了,在APP泛滥的时代,对APP进行自...
  • Temanm
  • Temanm
  • 2016年10月29日 13:54
  • 1429

My C++ Wrapper for FreeTDS

This artical introduce you my C++ wrapper for FreeTDS MSSqlServer interface.With this wrapper you ca...
  • qwidget
  • qwidget
  • 2011年05月25日 14:38
  • 2095

Kompex SQLite Wrapper for C++ - Examples

Here you can see some examples. It shows a part of the functionality of the wrapper and how you can...

CppSQLite - C++ Wrapper for SQLite

Download CppSQLite demo projects, source and sqlite.dll for SQLite 2.8.15 - 156 KBDownload CppSQLite...
  • aasmfox
  • aasmfox
  • 2011年12月31日 19:28
  • 2443

gradle-wrapper for flutter

  • 2017年11月15日 12:26
  • 51KB
  • 下载

cURL封装类(Wrapper class for cURL)

  • 2010年11月25日 01:15
  • 4KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Appium Wrapper for Java
举报原因:
原因补充:

(最多只允许输入30个字)