Package: gauto-android
GAutomatorAndroid中文文档A simplified version of the Android script in GAutomator, removing support for the cloud platform, removing compatibility with python2.7, and modifying some interfaces.Download and Installpipinstallgauto-androidthe simplest usageimportosimportgauto.managerasmanagerdevice=manager.get_device()engine=manager.get_engine()logger=logging.getLogger("gauto")# The global default logger name is "gauto"definit():os.environ["FORWARD_LOCAL_PORT"]="53001"# Local port, one for each android deviceos.environ["ANDROID_SERIAL"]="emulator-5554"# Android device serial numberos.environ["PKGNAME"]="com.tencent.sgame"# Package nameos.environ["LAUNCHACTIVITY"]="com.tencent.sgame.MainActivity"# Main Activitydefrun():logger.info("Launch app")device.launch_app()time.sleep(2)logger.info("Click the button named Button.close")e=engine.find_element("/Canvas/Button.close")engine.click(e)logger.info("screenshot")device.mini_screencap("./screenshot.png")logger.info("Kill app")device.kill_app()run()Another way to start your script —— Read the arguments from the command lineimportgetoptimportoptparseimportosimportgauto.managerasmanageros.chdir(os.path.dirname(os.path.abspath(__file__)))logger=manager.loggerdefrun():# dosomething...passdefmain():usage="usage:%prog [options] --qqname= --qqpwd= --engineport= --uiport= --serial="parser=optparse.OptionParser(usage)parser.add_option("-w","--localport",dest="FORWARD_LOCAL_PORT",help="forward local port")parser.add_option("-s","--serial",dest="ANDROID_SERIAL",help="adb devices android mobile serial")parser.add_option("-k","--package",dest="PKGNAME",help="upload password")parser.add_option("-j","--mainactivity",dest="LAUNCHACTIVITY",help="upload password")(options,args)=parser.parse_args()try:ifoptions.FORWARD_LOCAL_PORT:os.environ["FORWARD_LOCAL_PORT"]=options.FORWARD_LOCAL_PORTifoptions.ANDROID_SERIAL:os.environ["ANDROID_SERIAL"]=options.ANDROID_SERIALifoptions.PKGNAME:os.environ["PKGNAME"]=options.PKGNAMEifoptions.LAUNCHACTIVITY:os.environ["LAUNCHACTIVITY"]=options.LAUNCHACTIVITYexceptgetopt.errorasmsg:logger.info("for help use --help")return2run()if__name__=="__main__":sys.exit(main())In the entry script above, your can code your automation logic in 'run()'. Before that, the script takes arguments from the command line and initializes some environment variables.The reason is simple, for some global environment variables are used in gauto-android package, and if you don't set the required global variables, you might get some exceptions.You can then launch it with the following command:pythonmain.py--serial=emular-5554--localport=53001--package=com.tencent.sgame--mainactivity=com.tencent.sgame.MainActivityThe parameters of the script are described as follows:paramparam abbreviationdescription--localport-eEstablish a local port for adb forwarding--serial-sDevice serial number--package-kPackage name--mainactivity-jMain activity, get it from client programmerDevice APIDevice APIdescriptionlaunch_appLaunch APPis_app_launchedDetermine if the app is startedrelaunch_appRelaunch appkill_appKill app processget_screenshotScreenshot (slightly slower, adb‘s built-in capabilities)mini_screencapScreenshot (good performance, but with possible compatibility issues)Engine APIThe Unity engine-related APIs are placed in the engine.py moduleEngine APIDescriptionfind_elementGets an instance of an object by its fullpathfind_elements_pathReturns a list of objects that correspond to a particular fullpath (expression)find_elements_by_componentFind the object by given type and return a listget_element_boundGets the boundary information of the objectget_element_textReturns the text of the TextView component on an objectget_element_imageReturns the texture name in the Render component on an objectget_sceneGets the name of the current sceneget_element_world_boundGets the boundary information of the object in world coordinatesclick_positionClick by coordinatesclickClick on the center of an objectpress_positionPress by coordinatepressPress the center of an objectswipe_positionSwipe by coordinatesswipeSlide from the center of one object to the center of anotherswipe_and_pressjoystick simulationinputSet the text for a TextView in an objectget_touchable_elements_boundGets the boundary information for all touchable objectsget_registered_handlersReturns the list of name of the methods registered in SDKcall_registered_handlerCall a method registered in SDKget_component_methodsGets a list of method in a componentcall_component_methodGets a component's method to call a method on a component