Start Appium Programmatically


#21

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);

#22

@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)

#23

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


#24

@galenyjl

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

#25

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


#26

@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!


#27

@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.