Creating first Script

In last tutorial we saw how to set up Appium, getting the element hierarchy tree and getting to know how to use locators and what locators are used in Appium.

Now let’s get started with our first script. Here we’ll just simply try to open the app and wait for some time and then close the app.


Copy and paste this piece of code

import os
import time
from appium import webdriver

desired_caps = {}


#this is the android version you want to target- this should match the android version in the device


#this is the absolute path to the apk file. 

#launching appium
driver = webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)

#wait for 15 seconds

#quit Appium and close 

Once you execute this code, the 



Now let’s add one simple step to take a screenshot of this screen. In the code above after time.sleep, add these piece of code


directory = '%s/' % os.getcwd()
file_name = 'screenshot.png'

This will generate a screenshot named screenshot in the pwd of the project.


Now let’s add some interactions and actions to this script. We want to click on the Auth section and then go to the next screen.


Now see the screenshot of UIAutomatorViewer in this screen above


The Auth section can be accessed by clicking on this element right. If you see the hierarchy tree, we have this element inside android.widget.TextView and has a text of Auth, so we can use this to create an xpath as


We can use this xpath in our script to access this element and then click it. Add the below lines after the screenshot section

get_auth = driver.find_element_by_xpath("//android.widget.TextView[contains(@text,'Auth')]")
file_name = 'auth_screenshot.png'

Once you execute this code, the Appium will run the commands, open the app, then click on Auth and take a screenshot. – this added screenshot is the actual screenshot file taken by the code mentioned above.