Ability to identify if appium server is already in use before each run and close it

Hi,
In my Java selenium jupiter Junit5 project, I am using Appium server that initialized with capabilities before tests run for example:
in BeforeAll method:

capabilities are:
{
“name”: “virtualDeviceNoApp”,
“caps”: {
“platformName”: “Android”,
“deviceName”: “emulator-5554”,
“noReset”: true,
“fullReset”: false}
}]

and in BeforeEach I upload the application from shell command

first question:
I need to identify if appium server is already in use before each run and close it,
I read that if I use appium capabilities it automatically needs to close before each run test, but when I run entire class or project - it means amount of tests, between the tests - before each test run it does not happens, and the appium not close.

How and what do I need to do for this task?

second issue in the same topic:
If I need Appium to run in different port (not on 4723), I know that I need to specified DriverUrl with different port, I specified it,
@DriverUrl
private String url = “http://127.0.0.1:5687/wd/hub”;
But the port still doesn’t change.
What the way to change the port in this using?

Hope I explained clearly,
Thanks a lots!
Efrat

Most of the time when people want this, it’s because they want to run their tests in parallel, a tutorial like this will explain how to specify the port to run on (java examples):

@wreed
Thanks for the helpful answers.:ok_hand:
So now I can know if Appium server is already in use.
but I also need an appropriate command for my case to close the Appium.
Do you have an answer for me?
Thanks a lots!

You mean quit the driver?

https://appium.io/docs/en/commands/session/delete/index.html

@wreed
I mean - that if Apium runs, that he should not start it again but continue on it session.
The problem is that when use capabilities, automatically Appium start every test run.
My need is control over opening and closing Appium, but still using capabilities. Then before each test I will check and if the port is already in use - I will not start Appium, if it is not in use - I will start it.

I Hope there is way!
Thank you!!

Ah, you are talking about Reset Strategies. I’m not sure exactly how you want to run it, but take a look here:

https://appium.io/docs/en/writing-running-appium/other/reset-strategies/index.html

@wreed
Thank you for the quick answer.
I did not mean use the fullReset , noReset capabilities. - (I use them already in certain tests)
My intention is a wrapper of the Appium (that currently automatically opens before each test although Appium had open already in the first test… ) in an object that I can use to open and close the Appium as I need.

I saw something that might suit me:


the first option in this site.

Question: is it a good and acceptable way?
Or what is the best way for all platforms - IOS and Android, that most easily will help in my problem?

Thanks a lots!!

Ok, I took a look at the article. It’s good, the first option looks fine. I think a wrapper is an interesting idea. It certainly gives a certain amount of control that you don’t have otherwise.

I don’t think that it should matter for iOS vs Android. Starting and maintaining the server should be pretty agnostic.

Hi @wreed
Thank you very much for the help.

So, I try to use the first option…
now I get an error when trying to use it with capabilities.
my code to start Appium service:

private AppiumDriverLocalService service;
private AppiumServiceBuilder builder;
private DesiredCapabilities cap;

public void startServer() {
//Set Capabilities
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(“noReset”, “false”);

    //Build the Appium service
    builder = new AppiumServiceBuilder();
    builder.withIPAddress("127.0.0.1");
    builder.usingPort(4356);
    builder.withCapabilities(caps);
    builder.withArgument(GeneralServerFlag.SESSION_OVERRIDE);
    builder.withArgument(GeneralServerFlag.LOG_LEVEL, "error");

    //Start the server with the builder
    service = AppiumDriverLocalService.buildService(builder);
    service.start();
}

But I get an error when start the service:
…\AppData\Roaming\npm\node_modules\appium\build\lib\main.js: error: argument -dc/–default-capabilities: invalid parseDefaultCaps value: ‘{noReset:false}’

io.appium.java_client.service.local.AppiumServerHasNotBeenStartedLocallyException: The local appium server has not been started. The given Node.js executable: C:\Program Files\nodejs\node.exe Arguments: [C:\Users\Efrat Cohen\AppData\Roaming\npm\node_modules\appium\build\lib\main.js, --port, 4356, --address, 127.0.0.1, --log-level, error, --session-override, --default-capabilities, {“noReset”:“false”}]

What can be the problem?
How do I need to set my capabilities when use AppiumServiceBuilder ?

Thanks a lots!!

This looks like a basic Java error. You are declaring the variable ‘cap’ as private just like the sample code but then you declare a non private, ‘caps’ and try to use that. The sample code looks like this:

private DesiredCapabilities cap;

cap = new DesiredCapabilities();
builder.withCapabilities(cap);

I would just advise you to follow the tutorial more closely.

Hi @wreed
Sorry for the error in the code, because I tried to fix it, I sent you with this error.
Of course I followed exactly according to the guide.
Even when I do this:


I get this error.

Thanks for the help!

Did you forget to post the error? I’m not seeing it.

Here I send the error that is received:
The specific error is highlighted below.

io.appium.java_client.service.local.AppiumServerHasNotBeenStartedLocallyException: The local appium server has not been started. The given Node.js executable: C:\Program Files\nodejs\node.exe Arguments: [C:\Users\Efrat Cohen\AppData\Roaming\npm\node_modules\appium\build\lib\main.js, --port, 4356, --address, 127.0.0.1, --log-level, error, --session-override, --default-capabilities, {“deviceName”:“emulator-5554”,“platformName”:“Android”}]
Process output: usage: C:\Users\Efrat Cohen\AppData\Roaming\npm\node_modules\appium\build\lib\main.js
[-h] [–shell] [–allow-cors] [–reboot] [–ipa IPA] [-a ADDRESS]
[-p PORT] [-pa BASEPATH] [-ka KEEPALIVETIMEOUT] [-ca CALLBACKADDRESS]
[-cp CALLBACKPORT] [-bp BOOTSTRAPPORT] [-r BACKENDRETRIES]
[–session-override] [-l] [-g LOGFILE]
[–log-level {info,info:debug,info:info,info:warn,info:error,warn,warn:debug,warn:info,warn:warn,warn:error,error,error:debug,error:info,error:warn,error:error,debug,debug:debug,debug:info,debug:warn,debug:error}]
[–log-timestamp] [–local-timezone] [–log-no-colors] [-G WEBHOOK]
[–safari] [–default-device] [–force-iphone] [–force-ipad]
[–tracetemplate AUTOMATIONTRACETEMPLATEPATH]
[–instruments INSTRUMENTSPATH] [–nodeconfig NODECONFIG]
[-ra ROBOTADDRESS] [-rp ROBOTPORT]
[–chromedriver-executable CHROMEDRIVEREXECUTABLE] [–show-config]
[–no-perms-check] [–strict-caps] [–isolate-sim-device]
[–tmp TMPDIR] [–trace-dir TRACEDIR] [–debug-log-spacing]
[–suppress-adb-kill-server] [–long-stacktrace]
[–webkit-debug-proxy-port WEBKITDEBUGPROXYPORT]
[–webdriveragent-port WDALOCALPORT] [-dc DEFAULTCAPABILITIES]
[–relaxed-security] [–allow-insecure ALLOWINSECURE]
[–deny-insecure DENYINSECURE]
[–command-timeout DEFAULTCOMMANDTIMEOUT] [-k]
[–platform-name PLATFORMNAME] [–platform-version PLATFORMVERSION]
[–automation-name AUTOMATIONNAME] [–device-name DEVICENAME]
[–browser-name BROWSERNAME] [–app APP] [-lt LAUNCHTIMEOUT]
[–language LANGUAGE] [–locale LOCALE] [-U UDID]
[–orientation ORIENTATION] [–no-reset] [–full-reset]
[–app-pkg APPPACKAGE] [–app-activity APPACTIVITY]
[–app-wait-package APPWAITPACKAGE]
[–app-wait-activity APPWAITACTIVITY]
[–device-ready-timeout DEVICEREADYTIMEOUT]
[–android-coverage ANDROIDCOVERAGE] [–avd AVD] [–avd-args AVDARGS]
[–use-keystore] [–keystore-path KEYSTOREPATH]
[–keystore-password KEYSTOREPASSWORD] [–key-alias KEYALIAS]
[–key-password KEYPASSWORD] [–intent-action INTENTACTION]
[–intent-category INTENTCATEGORY] [–intent-flags INTENTFLAGS]
[–intent-args OPTIONALINTENTARGUMENTS] [–dont-stop-app-on-reset]
[–calendar-format CALENDARFORMAT] [–native-instruments-lib]
[–keep-keychains] [–localizable-strings-dir LOCALIZABLESTRINGSDIR]
[–show-ios-log] [–async-trace] [–chromedriver-port CHROMEDRIVERPORT]
[–log-filters LOGFILTERS] [-v]
C:\Users\Efrat Cohen\AppData\Roaming\npm\node_modules\appium\build\lib\main.js: error: argument -dc/--default-capabilities: invalid parseDefaultCaps value: '{deviceName:emulator-5554,platformName:Android}'

I think it’s a Windows issue with the quotes. You may need to play around with escaping them. Something like this: