How to stop appium server that initialized by capabilities before every running

Hi,
I am using Appium server that initialized by capabilities,
for example:
“platformName”: “Android”,
“deviceName”: “emulator-5554”,
“appPackage”: “com.menora_myway”,
“appActivity”: “com.menora_myway.MainActivity”,
“noReset”: true,
“fullReset”: false
so,_Selenium-Jupiter start automatically an instance of Appium Server (by default in port 4723) in the localhost.

The problem is: when I Proactively stop the running, when start running again I get the error:


[HTTP] Could not start REST http interface listener. The requested port may already be in use. Please make sure there is no other instance of this server running already.
Fatal Error: listen EADDRINUSE: address already in use 0.0.0.0:4723

and the run fails
How can I stop the Appium server in BeforeAll method when using this Appium initialized?

Thanks,
Efrat

Hi @Efrat_Cohen, I am not sure about “Appium server that initialized by capabilities”, but if you are using
AppiumDriverLocalService to start Appium server.
then you can simply use service.stop(); in teardown method to stop Appium server.

For Example:
AppiumDriverLocalService service = new AppiumServiceBuilder()
.withIPAddress(“127.0.0.1”).withArgument(GeneralServerFlag.BASEPATH, “/wd/hub”) .withArgument(GeneralServerFlag.RELAXED_SECURITY).withArgument(GeneralServerFlag.ALLOW_INSECURE,“adb_shell”)
.withTimeout(Duration.ofSeconds(120))
.usingPort(4723).build();

//Starts Appium server
service.start();
//Stop Appium server
service.stop();

Hi @Efrat_Cohen, usually when we write our tests we have some setup method in which you run service.start() for example to start the appium server.
and a teardown/cleanup method to clean the test environment such as service.stop()
when you Proactively stop the test you usually will do it in one of two ways: click the red square sign in your IDE to stop execution or if you execute your test from command line you will probably type control + c to stop execution.

In both ways your tearDown method is never invoked, meaning you never shut down the appium server which started programmatically (thus, you cannot start a new appium server on the same port number).

If you start appium server separately from the test execution process (in a separate terminal window), you can kill the appium server instead and it will automatically kill your open appium session (your test) and will clean the environment (will shut down and free up the port).
or, kill the test from your IDE or command line and then appium server will stay on for the next run if you want…

Now, if you stuck and cannot run any test since the port is occupied you can see here how to terminate a process in a specific port number: https://stackoverflow.com/questions/3855127/find-and-kill-process-locking-port-3000-on-mac#:~:text=10%20more%20comments-,2727,-Find%3A
there are other ways as well, I run
sudo lsof -i :portNumber; look for the PID (process id) in the result; run kill PID.
it will free your port.
If you are using windows and not mac then, see this post: https://stackoverflow.com/questions/39632667/how-do-i-kill-the-process-currently-using-a-port-on-localhost-in-windows

After all of that, I’ll say that you make me search in the internet and look for few solutions since I used to have the same problem.
I found this (if you use java client): https://stackoverflow.com/questions/8633955/how-to-gracefully-terminate-a-testng-test
never used it myself but thanks to you I gonna now try, basically it is registering a tearDown method to be executed if the current test process is terminated so, you can execute service.stop() (register it in the beginning of the test of course) and it should (never tries it myself, yet) terminate and cleanup the environment automatically each time you Proactively stop your test execution.

This is applicable if you use java client, if you don’t then, I guess there are similar implementations in other languages