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 = {} desired_caps['platformName']='Android' #this is the android version you want to target- this should match the android version in the device desired_caps['platformVersion']='8.0' desired_caps['deviceName']='Pixel' #this is the absolute path to the apk file. desired_caps['app']=os.path.join('/Users/zac/Downloads/Appium/kitten-tricks-c30a19b8ef07bfa06bb7718dff975465-signed.apk') #launching appium driver = webdriver.Remote('http://localhost:4723/wd/hub',desired_caps) #wait for 15 seconds time.sleep(15) #quit Appium and close driver.quit()
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' time.sleep(6) driver.save_screenshot(directory+file_name)
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
//android.widget.TextView[contains(@text,'Auth')]
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')]") get_auth.click() time.sleep(5) file_name = 'auth_screenshot.png' time.sleep(6) driver.save_screenshot(directory+file_name)
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.