Start Appium Programmatically

Haha, thanks, ferruzoft. For the video you mentioned, I even bought his course on Udemy. The code I showed you is worked on his course but failed on mine…

AppiumServiceBuilder appiumServiceBuilder = new AppiumServiceBuilder()
.UsingAnyFreePort();

    AppiumLocalService appiumLocalService = appiumServiceBuilder.Build();
    if (!appiumLocalService.IsRunning)
        appiumLocalService.Start();
    DesiredCapabilities capabilities = new DesiredCapabilities();

    capabilities.SetCapability("deviceName", "EM1");
    capabilities.SetCapability("platformVersion", "6.0");
    capabilities.SetCapability("platformName", "Android");
    capabilities.SetCapability("appPackage", "com.thlonline.telme");
    capabilities.SetCapability("appActivity", "com.thlonline.telme.MainActivity");

    AndroidDriver<AndroidElement> androidDriver = new AndroidDriver<AndroidElement (appiumLocalService, capabilities);

@galenyjl pls share error in appium logs.

BTW last time i did bash_profile was:

export ANDROID_HOME=/Users/YOUR_USER/Library/Android/sdk
export PATH=$ANDROID_HOME/platform-tools:$PATH
export PATH=$ANDROID_HOME/tools:$PATH
export JAVA_HOME=$(/usr/libexec/java_home)

Hi Aleksei,

Please check the full error logs in Appium.

[Appium] Welcome to Appium v1.7.2
[Appium] Non-default server args:
[Appium] address: 127.0.0.1
[Appium] port: 52036
[Appium] Appium REST http interface listener started on 127.0.0.1:52036
[HTTP] --> GET /wd/hub/status {}
[debug] [MJSONWP] Calling AppiumDriver.getStatus() with args: []
[debug] [MJSONWP] Responding to client with driver.getStatus() result: {“build”:{“version”:“1.7.2”,“revision”:null}}
[HTTP] <-- GET /wd/hub/status 200 15 ms - 72
[HTTP] --> GET /wd/hub/status {}
[debug] [MJSONWP] Calling AppiumDriver.getStatus() with args: []
[debug] [MJSONWP] Responding to client with driver.getStatus() result: {“build”:{“version”:“1.7.2”,“revision”:null}}
[HTTP] <-- GET /wd/hub/status 200 10 ms - 72
[HTTP] --> POST /wd/hub/session {“desiredCapabilities”:{“deviceName”:“EM1”,“platformVersion”:“6.0”,“platformName”:“Android”,“appPackage”:“com.thlonline.telme”,“appActivity”:“com.thlonline.telme.MainActivity”}}
[debug] [MJSONWP] Calling AppiumDriver.createSession() with args: [{“deviceName”:“EM1”,“platformVersion”:“6.0”,“platformName”:“Android”,“appPackage”:“com.thlonline.telme”,“appActivity”:“com.thlonline.telme.MainActivity”},null,null]
[debug] [BaseDriver] Event ‘newSessionRequested’ logged at 1520980490769 (11:34:50 GMT+1300 (NZDT))
[Appium] Creating new AndroidDriver (v1.37.0) session
[Appium] Capabilities:
[Appium] deviceName: EM1
[Appium] platformVersion: 6.0
[Appium] platformName: Android
[Appium] appPackage: com.thlonline.telme
[Appium] appActivity: com.thlonline.telme.MainActivity
[BaseDriver] Session created with session id: 063ac285-7ada-4505-a41a-a2d924c1e284
[debug] [AndroidDriver] Getting Java version
[AndroidDriver] Java version is: 1.8.0_131
[ADB] Checking whether adb is present
[ADB] The ANDROID_HOME environment variable is not set to the Android SDK root directory path. ANDROID_HOME is required for compatibility with SDK 23+. Checking along PATH for adb.
[ADB] Error: Could not find adb Please set the ANDROID_HOME environment variable with the Android SDK root directory path.
at Object.wrappedLogger.errorAndThrow (/usr/local/lib/node_modules/appium/node_modules/appium-support/lib/logging.js:69:13)
at ADB.callee$0$0$ (/usr/local/lib/node_modules/appium/node_modules/appium-adb/lib/tools/system-calls.js:127:9)
at tryCatch (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:67:40)
at GeneratorFunctionPrototype.invoke [as _invoke] (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:315:22)
at GeneratorFunctionPrototype.prototype.(anonymous function) [as throw] (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:100:21)
at GeneratorFunctionPrototype.invoke (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:136:37)
at
Error: Could not find adb Please set the ANDROID_HOME environment variable with the Android SDK root directory path.
at Object.wrappedLogger.errorAndThrow (/usr/local/lib/node_modules/appium/node_modules/appium-support/lib/logging.js:69:13)
at ADB.callee$0$0$ (/usr/local/lib/node_modules/appium/node_modules/appium-adb/lib/tools/system-calls.js:127:9)
at tryCatch (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:67:40)
at GeneratorFunctionPrototype.invoke [as _invoke] (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:315:22)
at GeneratorFunctionPrototype.prototype.(anonymous function) [as throw] (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:100:21)
at GeneratorFunctionPrototype.invoke (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:136:37)
at
[debug] [AndroidDriver] Shutting down Android driver
[debug] [AndroidDriver] Called deleteSession but bootstrap wasn’t active
[MJSONWP] Encountered internal error running command: Error: Could not find adb Please set the ANDROID_HOME environment variable with the Android SDK root directory path.
at Object.wrappedLogger.errorAndThrow (/usr/local/lib/node_modules/appium/node_modules/appium-support/lib/logging.js:69:13)
at ADB.callee$0$0$ (/usr/local/lib/node_modules/appium/node_modules/appium-adb/lib/tools/system-calls.js:127:9)
at tryCatch (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:67:40)
at GeneratorFunctionPrototype.invoke [as _invoke] (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:315:22)
at GeneratorFunctionPrototype.prototype.(anonymous function) [as throw] (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:100:21)
at GeneratorFunctionPrototype.invoke (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:136:37)
at
[HTTP] <-- POST /wd/hub/session 500 257 ms - 246

@galenyjl

  1. did you try mine example for bash?
  2. can you start adb in terminal? just print “adb”.

Haha, finally I solved the problem. Thanks, Aleksei! You reminded me that the issue is related to the adb.

@Aleksei, @ferruzoft

Hi guys, by ‘brew cask install android-platform-tools’, I can start appium on code now. But it looks like there are two adb, one in ~sdk/platform-tools, another in usr/local/bin. When I run appium on code, ‘The ANDROID_HOME environment variable is not set to the Android SDK root directory path’ still exist but using adb from /usr/local/bin/adb.

My question is

  1. Is this correct about two adb existing?
  2. Running appium on code is only worked on existed app (using appPackage and appActivity), if I tried from apk.file, it’s failed. But it’s worked if I opened Appium desktop at first without running it on code.

Please help, many thanks!

@galenyjl i never installed android tools from brew. I did it with android studio. Also i always started appium myself in thread as process in code (like start in terminal). Thus i never met your problem.

im facing similar issue when i running appium programatically through my java code, its working fine on both platform windows and IOS but not with ubunto however, all variable are set properly and u can run adb command from terminal , appium doctor shows everything is okay as well

but im still getting this error "org.openqa.selenium.SessionNotCreatedException: Unable to create a new remote session. Please check the server log for more details. Original error: An unknown server-side error occurred while processing the command. Original error: Could not find ‘adb’ in PATH. Please set the ANDROID_HOME environment variable with the Android SDK root directory path.

Build info: version: ‘3.141.59’, revision: ‘e82be7d358’, time: ‘2018-11-14T08:17:03’
System info: host: ‘warrior’, ip: ‘192.168.1.8’, os.name: ‘Linux’, os.arch: ‘amd64’, os.version: ‘4.18.0-25-generic’, java.version: ‘1.8.0_212’
Driver info: driver.version: AndroidDriver
remote stacktrace: UnknownError: An unknown server-side error occurred while processing the command. Original error: Could not find ‘adb’ in PATH. Please set the ANDROID_HOME environment variable with the Android SDK root directory path.
at getResponseForW3CError (/home/linuxbrew/.linuxbrew/lib/node_modules/appium/node_modules/appium-base-driver/lib/protocol/errors.js:826:9)
at asyncHandler (/home/linuxbrew/.linuxbrew/lib/node_modules/appium/node_modules/appium-base-driver/lib/protocol/protocol.js:447:37)
Build info: version: ‘3.141.59’, revision: ‘e82be7d358’, time: ‘2018-11-14T08:17:03’
System info: host: ‘warrior’, ip: ‘192.168.1.8’, os.name: ‘Linux’, os.arch: ‘amd64’, os.version: ‘4.18.0-25-generic’, java.version: ‘1.8.0_212’
Driver info: driver.version: AndroidDriver
"
this my bash file
export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"
export MANPATH="/home/linuxbrew/.linuxbrew/share/man:$MANPATH"
export INFOPATH="/home/linuxbrew/.linuxbrew/share/info:$INFOPATH"

export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64"
export PATH=$PATH:$JAVA_HOME/bin
export PATH=$PATH:$JAVA_HOME/jre/bin

export ANDROID_HOME=$HOME/Android/Sdk
export PATH=${PATH}:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools
export PLATFORM_TOOLS="/home/amrka/Android/Sdk/platform-tools"

export NODE_HOME="/usr/local/bin/node"


this is sample from my script
service = AppiumDriverLocalService
.buildService(new AppiumServiceBuilder()
.usingDriverExecutable(new File("/usr/bin/node"))
.withAppiumJS(new File("/home/linuxbrew/.linuxbrew/lib/node_modules/appium/build/lib/main.js"))
.withArgument(Arg.ADDRESS,URL_)
.withArgument(Arg.PORT,Sport)
.withArgument(Arg.CALLBACKPORT,Sport)
.withArgument(Arg.WDALOCALPORT,wdaPort)
.withArgument(Arg.BootstrapPort,bootStrap)
// .withArgument(Arg.NODECONFIG,path) //uncomment this when use parallel test with grid
.withArgument(Arg.SESSIONOVERRIDE));
service.start();

where is “$HOME” - i do not see it

Thx alesksei solved after restart my machine, weird but its working with same bashrc file

Just add one more variable for build-tools