我可以使用下面的代码启动和停止appium,但我想知道在启动java之前如何检查服务器是否已经运行。我google了它,但我似乎没有找到任何相关的任何建议,这将是非常感谢。提前致谢。请找到以下代码:
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();
}
}
答案 0 :(得分:3)
我发现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;
}
这里的逻辑是弄清楚端口是否在使用中。通常,您与Appium一起使用的端口不会被其他应用程序使用。因此,如果端口已在使用中,则可以安全地假设Appium服务器正在端口上运行。