2014-02-21 98 views
3

有沒有一種方法可以在我爲junit測試編寫的代碼中啓動appium?由於appium只需要在運行測試時運行,因此我始終保持appium服務器不變。在測試執行過程中自動啓動appium?

現在我正在使用junit和maven來運行測試版本。由於appium的穩定性問題,它有時會在構建過程中死掉,從而導致所有剩餘的測試失敗。我想知道在將WebDriver連接到它之前,是否可以在@Before方法中添加一些內容來啓動appium服務器,然後在@After方法中終止它。這應該解決任何關於appium故障的問題,因爲它可以在開始下一個測試之前重置。

仍在研究java中的開始和結束進程,看看它是否可行。如果我想出來,我會更新這篇文章,以幫助其他有興趣測試這種方式的人。

回答

0

我使用DefaultExecutor來跟蹤Appium過程,因此它可以在年底摧毀了一個非常類似的方式解決了這個的測試。這還允許在測試期間使用DefaultExecuteResultHandler註銷Appium輸出。

爲了避免使用休眠等待Appium開始,你可以一個try-catch

for (int i = 10; i > 0; i--) { 
     try { 
      driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); 
      // If we successfully attach to appium, exit the loop. 
      i = 0; 
     } catch (UnreachableBrowserException e) { 
      LOGGER.info("Waiting for Appium to start"); 
     } 
    } 

你可以在catch塊添加睡眠中創建您的WebDriver例如,如果你想輪詢頻率。

2

想出如何得到這個僅由代碼

@Before 
public void setUp() throws Exception { 
    closeSimulatorAndInstruments(); // also closes any appium servers 
    appium = Runtime.getRuntime().exec("/usr/local/bin/appium"); 
    Thread.sleep(1000); // wait for appium to start up, not sure how to check the status 
    ... // start test 
} 

@After 
public void tearDown() throws Exception { 
    captureScreenshot(testName.getMethodName()); 
    driver.quit(); 
    appium.destroy(); // kills the appium server so it wont collide with the next run 
} 

我看到我的CI機器上運行詹金斯試圖做到這一點的問題中運行的終端命令工作,但它可能是不相關的。本地這個工作很好,因爲我不必記得再分開運行appium,或者檢查它是否死亡。這,如果你需要看appium輸出可能包含重要的錯誤

不建議然而
0

我看到我們可以改善一點以上的解決方案。

  1. 爲每個環境(定義appium家)
  2. 你可以在過程輸出流重定向到文件創建配置文件。文件名可以在配置文件中定義在java文件中。
1

我已經爲此寫了一個庫。

/** 
*@author Raghu Nair 
*/ 
public class Appium { 

private static volatile Appium instance; 

public static Appium getInstance(String outFile, String errFile) { 
    if (instance == null) { 
     synchronized (Appium.class) { 
      if (instance == null) { 
       instance = new Appium(outFile, errFile); 
      } 
     } 
    } 
    return instance; 
} 
Process process; 
final String outFile; 
final String errFile; 

private Appium(String outFile, String errFile) { 
    this.outFile = outFile; 
    this.errFile = errFile; 
} 

public void start() throws IOException { 
    if (process != null) { 
     stop(); 
    } 

    String processName = System.getProperty("appium.bin"); 
    String processString = processName + " -lt 180000"; 
    ProcessBuilder builder = new ProcessBuilder("bash"); 
    process = builder.start(); 
    OutputStream outStream = System.out; 
    if (outFile != null) { 
     outStream = new FileOutputStream(outFile); 
    } 
    OutputStream errStream = System.err; 
    if (errFile != null) { 
     errStream = new FileOutputStream(errFile); 
    } 

    handleStream(process.getInputStream(), new PrintWriter(outStream)); 
    handleStream(process.getErrorStream(), new PrintWriter(errStream)); 
    try (PrintWriter writer = new PrintWriter(process.getOutputStream())) { 
     //writer.println("kill -9 `ps -ef | grep appium | cut -d' ' -f2`"); 
     writer.println("export PATH=$PATH:/usr/bin/:/usr/local/bin/"); 
     writer.println(processString); 
     writer.flush(); 
    } 

} 

private void handleStream(final InputStream processOut, final PrintWriter writer) { 
    Thread outHandler; 
    outHandler = new Thread(new Runnable() { 
     @Override 
     public void run() { 
      try { 
       BufferedReader stdout = new BufferedReader(
         new InputStreamReader(processOut)); 
       String line; 
       while ((line = stdout.readLine()) != null) { 
        writer.println(line); 
        writer.flush(); 
       } 
      } catch (IOException ex) { 
       ex.printStackTrace(System.err); 
      } 
     } 
    }); 
    outHandler.start(); 
} 

public void stop() { 
    System.out.println("Stopping the process"); 
    if (process != null) { 
     try { 
      process.destroy(); 
      process.getErrorStream().close(); 
      process.getInputStream().close(); 
      process.getOutputStream().close(); 
     } catch (IOException ex) { 
      ex.printStackTrace(System.err); 
     } 
    } 
} 

public static void main(String[] args) throws Exception { 
    System.setProperty("appium.bin", "/Applications/Appium.app//Contents/Resources/node_modules/.bin/appium"); 
    Appium appium = Appium.getInstance("/Users/<user>/tmp/appium.out", "/Users/<user>/tmp/appium.err"); 
    appium.start(); 
    TimeUnit.SECONDS.sleep(30); 
    appium.stop(); 
} 
相關問題