Let us see a basic example of appium.Here we are going to launch the app in an android device and click on the button using id locator and close the app after 5 seconds.
Example: In the below example we are launching the app on an Android device.
LaunchApp.py
from appium import webdriver from appium.webdriver.common.appiumby import AppiumBy import time # Step 1 : Create "Desired Capabilities" desired_caps = {} desired_caps['platformName'] = 'Android' desired_caps['automationName'] = 'UiAutomator2' desired_caps['platformVersion'] = '10' desired_caps['deviceName'] = 'Pixel3XL' desired_caps['app'] = ('/Appium_Demo_App/Android/Android_Appium_Demo.apk') desired_caps['appPackage'] = 'com.skill2lead.appiumdemo' desired_caps['appActivity'] = 'com.skill2lead.appiumdemo.MainActivity' # Step 2 : Create "Driver object" driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps) # Step 3 : "Click on the App" ele_id = driver.find_element(AppiumBy.ID,"com.skill2lead.appiumdemo:id/EnterValue") ele_id.click() # Step 4 : Wait for 2 seconds time.sleep(2) # Step 5 : Close the driver object driver.quit()
1. First we need to import the webdriver class from the appium package to access all the required methods.
from appium import webdriver
2. Now we need to import the time module to wait for required amount of time before to close the app
import time
3. Create a Dictionary variable "desired_caps = {}"
and assign Appium Desired Capabilities to it in the form of Key and value pairs.
We need to pass PlatformName (Android or IOS or Windows) , PlatformVersion , automationName, deviceName , app path , appPackage and appActivity.
Here PlatformName , PlatformVersion , automationName , appPackage and appActivity are predefined (We need to enter correct data, it's case sensitive) , deviceName is user defined (We can enter any name,it's not predefined).
desired_caps = {} desired_caps['platformName'] = 'Android' desired_caps['automationName'] = 'UiAutomator2' desired_caps['platformVersion'] = '10' desired_caps['deviceName'] = 'Pixel3XL' desired_caps['app'] = ('/Appium_Demo_App/Android/Android_Appium_Demo.apk') desired_caps['appPackage'] = 'com.skill2lead.appiumdemo' desired_caps['appActivity'] = 'com.skill2lead.appiumdemo.MainActivity'
4. Create the object for WebDriver class "driver" to access all the methods in WebDriver class and pass the localhost and Port number in the form of url to connect to the local server and "desired_caps" dictionary variable as well to establish a connection to the device.
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
127.0.0.1 : This is a local host ip address which used to communicate with the appium server.
4723 : This is a port number, it can be changed.
wd/hub : This is used to connect to the remote path of appium server, Where ‘wd’ stands for webdriver.
5. By using the created WebDriver object "driver" now we can access all the methods to act on the App and assign it to a variable "ele_id" to perform further action on it.
ele_id = driver.find_element_by_id("com.code2lead.kwad:id/EnterValue") ele_id.click()
6. Call the time.sleep() method from the imported time module to wait for the required amount of time before closing the object.
Pass the integer value it takes as an argument in the form of seconds.
time.sleep(5)
7. Call the quite() method using webdriver object(driver) to close the driver instance.
driver.quit()