2017-09-20 48 views
0

我試圖以編程方式運行Appium服務器。我想所有的Appium被提供,但沒有得到任何結果以編程方式運行appium服務器

在命令行,如果我想C:\Users\User>appium它的啓動服務器的可能選項。如何通過使用java代碼來做同樣的事情?

注:Appium的版本是1.6.5

AppiumDriverLocalService service=AppiumDriverLocalService.buildDefaultService(); 
service.start(); 

這我使用的編程運行appium服務器什麼的代碼。

我得到的錯誤是

java.lang.NoClassDefFoundError: org/apache/commons/validator/routines/InetAddressValidator

+0

你在執行程序時會遇到什麼錯誤?以及那裏正在使用的代碼是什麼? – nullpointer

+0

AppiumDriverLocalService service = AppiumDriverLocalService.buildDefaultService(); \t service.start();這是我用來以編程方式運行appium服務器的代碼。我得到的錯誤是java.lang.NoClassDefFoundError:org/apache/commons/validator/routines/InetAddressValidator – suryakanta939

+1

更新問題請完整性可能會丟失提供額外評論中的細節。 – nullpointer

回答

0

你有沒有試過,

import java.io.*; 

public class CmdTest { 
    public static void main(String[] args) throws Exception { 

     Process p = Runtime.getRuntime().exec("appium"); 

    } 
} 
+0

是我試過但得到類似java.io.IOException的錯誤:無法運行程序「C:/ Users/User> appium」:CreateProcess error = 2,系統在java.lang.ProcessBuilder找不到指定的文件 \t。開始(未知來源) – suryakanta939

+0

它明確指出'appium'不在此路徑下,請嘗試更改appium安裝時位於其中的路徑。 – DevGo

+0

但是,如果我想從命令行嘗試相同的操作,我能夠運行服務器 – suryakanta939

-2

嘗試addthe公共驗證lib添加到您的類路徑/ pom.xml的(公共 - 驗證 - 1.4.0的.jar)

0

這裏是你如何啓動和停止appium服務器程序使用Java代碼

import java.nio.file.Paths;  
public class AppiumSetupAndTearDown { 

     public static void startAppiumServer() throws IOException, InterruptedException { 

      String logDir = <the path where you want to create the appium output file>; 

      File appiumOutput = new File(logDir); 

      if (!appiumOutput.exists()) { 
       boolean status = appiumOutput.createNewFile(); 
       if (!status) { 
        throw new IOException("Failed to create Appium output file!"); 
       } 
      } 

      String cmd[] = {"/bin/bash", Paths.get(System.getProperty("user.dir")).getParent().getParent().toString() + "/Modules/Shared/startAppium.sh"}; 
      ProcessBuilder pb = new ProcessBuilder(cmd); 

      pb.redirectError(new File(logDir)); 
      pb.redirectInput(new File(logDir)); 
      pb.redirectOutput(new File(logDir)); 
      pb.start(); 
      System.out.println("Appium server started!"); 

     } 

     public static void stopAppiumServer() throws IOException, InterruptedException { 
      String cmd[] = {"/bin/bash", Paths.get(System.getProperty("user.dir")).getParent().getParent().toString() + "/Modules/Shared/stopAppium.sh"}; 

      ProcessBuilder pb = new ProcessBuilder(cmd); 
      pb.start(); 
      System.out.println("Appium server stopped!"); 

     } 
    } 

如果遇到計時問題,您也可以在啓動和停止appium服務器後添加Thread.sleep(5000)。

-Raj

Testim.io

0

我知道你問的Java,但我用C#這樣做編程。所以,我貼我的情況的解決方案,它可以幫助獲得一個想法:

C#代碼:

public void startAppiumServerLocally() 
    { 
     try 
     { 
      string nodejs = "\"" + Environment.GetEnvironmentVariable("nodejs") + "\""; // Environment path for node.exe 
      string appium = "\"" + Environment.GetEnvironmentVariable("appium") + "\""; // Environment path for main.js or appium.js 

      ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(nodejs); 

      myProcessStartInfo.UseShellExecute = false; 
      myProcessStartInfo.RedirectStandardOutput = true; 
      myProcessStartInfo.RedirectStandardError = true; 
      myProcessStartInfo.CreateNoWindow = true; 
      // getOverride() method returns '--session-override' 
      // getDesiredCapabilitiesJson() returns a JSON with some capabilities '--default-capabilities {"udid":identifier..., deviceName: "Samsung S7"....}' 
      // getDriverPort() returns --port X just in case I need to start in a different and unused port. 
      string args = appium + getOverride() + getDesiredCapabilitiesJson() + getDriverPort(); 
      myProcessStartInfo.Arguments = args; 

      nodeProcess = new Process(); 
      nodeProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 
      nodeProcess.StartInfo = myProcessStartInfo; 
      nodeProcess.Start(); 

      nodeProcess.BeginErrorReadLine(); 
      StreamReader myStreamReader = nodeProcess.StandardOutput; 
      while (_bAppiumForceEnd == false) 
      { 
       if (_bAppiumInit == false) 
       { 
        string line = myStreamReader.ReadLine(); 
        if (line.Contains("listener started")) 
        { 
         // _bAppiumInit is a flag to start my tests once the server started 
         _bAppiumInit = true; 
        } 
       } 
       else 
       { 
        myStreamReader.ReadLine(); 
       } 
      } 
      myStreamReader.Close(); 
     } 
     catch (Exception e) 
     { 
      //Log(e.Message); 
     } 
    } 

我知道,在創建while循環是不是最好的解決辦法,但有時我nodeProcess不停止...所以我改變了這種方式,它完美的作品。

相關問題