1. First introduce the next uiautomator tool
Uiautomator is the official Android App Interface Automation Test tool, ideal for APK for automated functional regression testing.
2.UIAutomator Test Environment Setup
2.1 in PyPI. python.org website Download Uiautomator Compressed package, unzip the Python setup.py install installation, or direct PIP install Uiautomator
2.2 For Uiautomator tools, ADB is essential, ADB provides the ADB shell for Android remote operation, installation of ADB,ADB device can view USB connected mobile device, install ADB recommended to install 91 mobile phone assistant directly, 91 Mobile Assistant will automatically install ADB for you,
2.3 Introduction to the UI tools for the Uiautomator test framework: Uiautomatorviewer such as:
Uiautomatorviewer is located in the Sdk/tools directory, you can scan, analyze the application interface to be tested, analysis results can be exported to XML and. This tool allows you to analyze various properties, such as id,text,focusable of UI controls, and even hierarchical relationships on the layout.
You can start the tool by using./uiautomatorviewer.
Use of 3.UIAutomator Tools
First small Demo code:
#-*-coding:utf-8-*- fromUiautomatorImportDevice as DImport TimeImportSYSImportRandomImportUnitTestImporthtmltestrunnerreload (SYS) sys.setdefaultencoding ("Utf-8")classMy_test_suite (unittest. TestCase):defsetUp (self):Try: D.press.home () d (Text="***"). Click () time.sleep (2) ifD (text="my own"). Exists:d (Text="my own"). Click () d (Text="Logout"). Click () d (Text="Determine"). Click ()ifD (text="Login"). Exists:d (ResourceId="Com.isentech.attendance:id/title_back"). Click ()Else: Time.sleep (3) PrintU"Open App" exceptException, E:PrintU"Error: Failed to open app \ n", E#Test Registration defTest_reg (self):Try: D (Text="Register"). Click ()#test registered phone numberD (text="Please enter your mobile phone number"). Set_text ("1313384****") d (Text="Get Verification Code"). Click ()#Test RegistrationD (text="Please enter your mobile phone number"). Set_text (Phone_number) d (Text="Please enter a verification code"). Set_text ("8888") d (resourceId="Com.isentech.attendance:id/regis_pass"). Set_text ("123456") d (resourceId="Com.isentech.attendance:id/regis_passagain"). Set_text ("123456") d (Text="Register"). Click () time.sleep (2) ifD (text="go to login now"). Exists:d (Text="go to login now"). Click () d (ResourceId="Com.isentech.attendance:id/txtloginpassword"). Set_text ("123456") d (Text="Login"). Click ()exceptException, E:PrintU"Error: Registration failed \ n", E#Test Login defTest_login (self, phone):Try: D (Text="Login"). Click () d (ResourceId="Com.isentech.attendance:id/txtloginusername"). Clear_text () d (ResourceId="Com.isentech.attendance:id/txtloginusername"). Set_text (phone) d (resourceId="Com.isentech.attendance:id/txtloginpassword"). Set_text ("123456") d (Text="Login"). Click () d (Text="Please enter your name"). Set_text ("123456") d (Text="Complete"). Click () time.sleep (2) ifD (text="sign in"). Exists:PrintU"Login Successful" exceptException, E:PrintU"Error: Login failed \ n", E#Test forgot Password defTest_forget_password (self):Try: Pass #some test steps exceptException, E:PrintU"Error: resetting password or changing password failed \ n", E#...... More Test module Use cases defTearDown (self):Try: D.press.home () d.press.recent () Time.sleep (3) D.swipe (0, steps=10) D.press.home ()PrintU"Close App" exceptException, E:PrintU"Error: Closing app failed \ n", Eif __name__=="__main__": Phone_number= Random.choice (['139','188','185','136','158','151'])+"". Join (Random.choice ("0123456789") forIinchRange (8)) Test_unit=UnitTest. TestSuite () test_unit.addtest (My_test_suite ("Test_reg")) FileName='./result_auto_android.html'FP= File (filename,'WB') Runner= Htmltestrunner.htmltestrunner (stream=fp,title=u"Test Report", Description=u"test Result Details:") Runner.run (test_unit)
The above code is an app's overall test framework, some code is hidden, the understanding of the code I want you to check this article: Https://github.com/Xuyangting/uiautomator
Why Android Automation?! Every time the regression test, like hit the ground mouse, hit one after another, really heartache, testers too hard, every day to take a cell phone point Ah point, so only choose to use uiautomator tools to help test, but this tool also has a lot of bad places, many test scenarios difficult to simulate out, Manual testing has to be done manually. After writing this Android automation, the next thing to write iOS Automated test tool-appium, this tool and Uiautomator similar, is a little different in the environment
"Go" Python + Android + uiautomator Automated test