Follow the below steps to run appium programmatically
Note : If you see any permissions issues in Mac
Don't launch the Appium Desktop server when you're launching through a program.
Run this command :
sudo chown -R $USER /usr/local/lib/node_modules
Example : In the below example we are launching the app on an Android device without an appium desktop server and clicking on a button using id locator.
As we launch the Appium using code with help of nodejs.
Start_Appium_Programatically.py
import time
from appium import webdriver
# Step 1 : Import Appium Service class
from appium.webdriver.appium_service import AppiumService
# Step 2 : Create object for Appium Service class
appium_service = AppiumService()
# Step 3 : Call Start method by using Appium Service class object
appium_service.start()
# Step 4 : Create "Desired Capabilities"
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['automationName'] = 'UiAutomator2'
desired_caps['platformVersion'] = '10'
desired_caps['deviceName'] = 'Pixel3XL'
desired_caps['app'] = ('/Skill2Lead/Appium_Demo_App/Android/Android_Appium_Demo.apk')
desired_caps['appPackage'] = 'com.skill2lead.appiumdemo'
desired_caps['appActivity'] = 'com.skill2lead.appiumdemo.MainActivity'
# Step 5 : Create "Driver object"
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
# Step 6 : "Click on the button using ID locator value"
ele_id = driver.find_element(By.ID,"com.skill2lead.appiumdemo:id/EnterValue")
ele_id.click()
# Step 7 : Wait for 2 seconds
time.sleep(2)
# Step 8 : Close the driver object
driver.quit()
# Step 9 : Call stop method by using Appium Service class object
appium_service.stop()