Premise: According to the previous environment construction introduction, install the relevant environment
Step1: Launch Android emulator
Step2: Start the Appium server
Step3: Demo Code execution
Here is the official Demo code: Address Book Management app, install Open app, and add a contact saved action
A. First go to download contactmanager.apk put to E disk E:\ContactManager.apk
https://github.com/appium/sample-code/blob/master/sample-code/apps/ContactManager/ContactManager.apk
B. Sample code for the android_contact.py download down in the Python directory
https://github.com/appium/sample-code/blob/master/sample-code/examples/python/android_contacts.py
C. Partial modification of Python code
#!/usr/bin/env python#-*-coding:utf-8-*-ImportOSImportUnitTest fromAppiumImportWebdriver fromTimeImportSleep#Returns ABS Path relative to this file and not CWDPATH =LambdaP:os.path.abspath (Os.path.join (Os.path.dirname (__file__), p))classcontactsandroidtests (unittest. TestCase):defsetUp (self): Desired_caps={} desired_caps['PlatformName'] ='Android'desired_caps['platformversion'] ='4.4.2'desired_caps['devicename'] ='Android Emulator'desired_caps['app'] =PATH ('E:\ContactManager.apk') desired_caps['Apppackage'] ='Com.example.android.contactmanager'desired_caps['appactivity'] ='. Contactmanager'Self.driver= Webdriver. Remote ('Http://localhost:4723/wd/hub', Desired_caps)defTearDown (self): Self.driver.quit ()deftest_add_contacts (self): El= self.driver.find_element_by_accessibility_id ("ADD Contact") El.click () Textfields= Self.driver.find_elements_by_class_name ("Android.widget.EditText") Textfields[0].send_keys ("Appium User") textfields[2].send_keys ("[email protected]") self.assertequal ('Appium User', Textfields[0].text) self.assertequal ('[email protected]', textfields[2].text) self.driver.find_element_by_accessibility_id ("Save"). Click ()#For some reason "save" breaks thingsAlert =Self.driver.switch_to_alert ()#No-to- handle alerts in AndroidSelf.driver.find_element_by_android_uiautomator ('new Uiselector (). Clickable (True)'). Click () Self.driver.press_keycode (3)if __name__=='__main__': Suite=UnitTest. Testloader (). Loadtestsfromtestcase (contactsandroidtests) unittest. Texttestrunner (verbosity=2). Run (Suite)
Appium 003--Script Development: Official Demo Demo android_contacts.py