AppiumDriverLocalService.java

转载 2015年11月17日 16:31:24

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.appium.java_client.service.local;


import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.net.UrlChecker;
import org.openqa.selenium.os.CommandLine;
import org.openqa.selenium.remote.service.DriverService;


import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;


import static com.google.common.base.Preconditions.checkNotNull;


public final class AppiumDriverLocalService extends DriverService {


    private static final String URL_MASK = "http://%s:%d/wd/hub";
    private final File nodeJSExec;
    private final int nodeJSPort;
    private final ImmutableList<String> nodeJSArgs;
    private final ImmutableMap<String, String> nodeJSEnvironment;
    private final String ipAddress;
    private final long startupTimeout;
    private final TimeUnit timeUnit;
    private final ReentrantLock lock = new ReentrantLock();
    private final ListOutputStream stream = new ListOutputStream().add(System.out);






    private CommandLine process = null;


    AppiumDriverLocalService(String ipAddress, File nodeJSExec, int nodeJSPort,
                             ImmutableList<String> nodeJSArgs,
                             ImmutableMap<String, String> nodeJSEnvironment,
                             long startupTimeout,
                             TimeUnit timeUnit) throws IOException {
        super(nodeJSExec, nodeJSPort, nodeJSArgs, nodeJSEnvironment);
        this.ipAddress = ipAddress;
        this.nodeJSExec = nodeJSExec;
        this.nodeJSPort = nodeJSPort;
        this.nodeJSArgs = nodeJSArgs;
        this.nodeJSEnvironment = nodeJSEnvironment;
        this.startupTimeout = startupTimeout;
        this.timeUnit = timeUnit;
    }


    /**
     * @return The base URL for the managed appium server.
     */
    @Override
    public URL getUrl() {
        try {
            return new URL(String.format(URL_MASK, ipAddress,
                    nodeJSPort));
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
    }


    @Override
    public boolean isRunning() {
        lock.lock();
        if (process == null)
            return false;


        try {
            ping(500, TimeUnit.MILLISECONDS);
            return true;
        } catch (UrlChecker.TimeoutException e) {
            return false;
        } finally {
            lock.unlock();
        }


    }


    private void ping(long time, TimeUnit timeUnit) throws UrlChecker.TimeoutException{
        URL url = getUrl();
        try {
            URL status = new URL(url.toString() + "/status");
            new UrlChecker().waitUntilAvailable(time, timeUnit, status);
        } catch (MalformedURLException e) {
            throw new RuntimeException("There is something wrong with the URL " + url.toString().toString() + "/status");
        }
    }


    /**
     * Starts the defined appium server
     * @throws AppiumServerHasNotBeenStartedLocallyException If an error occurs while spawning the child process.
     * @see #stop()
     */
    public void start() throws AppiumServerHasNotBeenStartedLocallyException {
        lock.lock();
        if (isRunning())
            return;


        try {
            process = new CommandLine(this.nodeJSExec.getCanonicalPath(), nodeJSArgs.toArray(new String[] {}));
            process.setEnvironmentVariables(nodeJSEnvironment);
            process.copyOutputTo(stream);
            process.executeAsync();
            ping(startupTimeout, timeUnit);
        } catch (Throwable e) {
            destroyProcess();
            String msgTxt = "The local appium server has not been started. " +
                    "The given Node.js executable: " + this.nodeJSExec.getAbsolutePath() + " Arguments: " + nodeJSArgs.toString() + " " + "\n";
            String processStream = process.getStdOut();
            if (!StringUtils.isBlank(processStream))
                msgTxt = msgTxt + "Process output: " + processStream + "\n";


            throw new AppiumServerHasNotBeenStartedLocallyException(msgTxt,
                    e);
        } finally {
            lock.unlock();
        }
    }


    /**
     * Stops this service is it is currently running. This method will attempt to block until the
     * server has been fully shutdown.
     *
     * @see #start()
     */
    @Override
    public void stop() {
        lock.lock();
        destroyProcess();
        lock.unlock();
    }




    private void destroyProcess(){
        if (process != null)
            process.destroy();
    }


    /**
     * @return String logs if the server has been run.
     * null is returned otherwise.
     */
    public String getStdOut() {
        if (process != null)
            return process.getStdOut();


        return null;
    }


    public void addOutPutStream(OutputStream outputStream){
        checkNotNull(outputStream, "outputStream parameter is NULL!");
        stream.add(outputStream);
    }


    public void addOutPutStreams(List<OutputStream> outputStreams){
        checkNotNull(outputStreams, "outputStreams parameter is NULL!");
        for (OutputStream stream: outputStreams){
            addOutPutStream(stream);
        }
    }


    public static AppiumDriverLocalService buildDefaultService(){
        return buildService(new AppiumServiceBuilder());
    }


    public static  AppiumDriverLocalService buildService(AppiumServiceBuilder builder){
        return builder.build();
    }


}

使用Appium官方提供的AppiumDriverLocalService类实现在eclipse启动和停止Appium服务

在网上和QQ群中,看到大多数学习appium的童鞋都是使用windows版本的Appium,windows版本有界面好操作,但实际使用过程中发现,如果连续跑几次用例会发现由于之前的appium服务se...

在携程使用TestNG/JUnit 并行跑Appium 有两个关键的地方

转载地址:https://testerhome.com/topics/1944 在携程使用TestNG/JUnit 并行跑Appium 有两个关键的地方: 独立使用的A...
  • wanglha
  • wanglha
  • 2015年11月17日 16:38
  • 1047

Appium(Java)2.0.0以上运行时报错“java.lang.UnsupportedClassVersionError: io/appium/java_client/AppiumDriver”

Appium(Java)2.0.0以上运行时报错“java.lang.UnsupportedClassVersionError: io/appium/java_client/AppiumDriver”...

appium踩过的坑(持续更新..)

Appium踩过的坑一: 运行appium报错:Appium will not work if used or installed with sudo bixiaopeng@bixiaopengt...

常用文件处理方法(java)

  • 2017年12月14日 17:23
  • 13KB
  • 下载

Java从入门到精通(第3版)

  • 2017年12月14日 17:31
  • 6.93MB
  • 下载

Java 8 新特性:扩展注解(类型注解和重复注解)

一.注解(JDK1.5) 二.注解更新(JDK1.8) 类型注解和重复注解…………

SuperMap Objects Java跟踪图层查询

  • 2017年12月14日 11:36
  • 1.11MB
  • 下载

Java虚拟机(四):类文件结构

Class类文件的结构、字节码指令简介、公有设计和私有现实
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:AppiumDriverLocalService.java
举报原因:
原因补充:

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