简体   繁体   English

如何使用Java检查Appium服务器是否已在运行

[英]How to check if appium server is already running using java

I'm able to start and stop appium using the below code but i want to know how to check if the server is already running before starting in java. 我可以使用以下代码启动和停止appium,但我想知道如何在使用Java启动之前检查服务器是否已经在运行。 I googled it but i dont seem to find anything relevant any advise on this would be very much appreciated. 我用谷歌搜索,但是我似乎没有找到任何相关的建议,对此我们将不胜感激。 Thanks in advance. 提前致谢。 Please find the code below: 请在下面找到代码:

package code;

import java.io.File;

import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;
import org.openqa.selenium.WebDriver;

public class AppiumServerStartStop
{

   static WebDriver driver;
   static String Appium_Node_Path = "C:\\Program Files\\nodejs\\node.exe";
   static String Appium_JS_Path = "C:\\Users\\Administrator\\AppData\\Local\\Programs\\appium-desktop\\resources\\app\\node_modules\\appium\\lib\\appium.js";
   static AppiumDriverLocalService service;
   static String service_url;

   public static void main(String args[])
      throws Exception
   {
      appiumStart();
   }

   public static void appiumStart() throws Exception
   {

      service = AppiumDriverLocalService.buildService(new AppiumServiceBuilder().usingAnyFreePort().usingDriverExecutable(new File(Appium_Node_Path)).withAppiumJS(new File(Appium_JS_Path)));
      service.start();
      service_url = service.getUrl().toString();
      System.out.println("Appium Service Address : - " + service_url);
      service.stop();
   }
}

I found that snipped on this site : 我发现在此网站上被窃听了:

public boolean checkIfServerIsRunnning(int port) {

    boolean isServerRunning = false;
    ServerSocket serverSocket;
    try {
        serverSocket = new ServerSocket(port);
        serverSocket.close();
    } catch (IOException e) {
        //If control comes here, then it means that the port is in use
        isServerRunning = true;
    } finally {
        serverSocket = null;
    }
    return isServerRunning;
}

The logic here is to figure out if the port is in use or not. 此处的逻辑是弄清楚端口是否在使用中。 Generally, the ports that you use with Appium are not used by other applications. 通常,与Appium一起使用的端口未被其他应用程序使用。 So if the port is already in use, it is safe to assume that Appium server is running on the port. 因此,如果端口已在使用中,则可以安全地假定该端口上正在运行Appium服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM