Best Python code snippet using ATX
uiautomator_manager.py
Source:uiautomator_manager.py
...19_device_port=900820_uiautomator_port = os.environ.get("UIAUTOMATOR_PORT","19008")21def restartPlatformDialogHandler():22 try:23 kill_uiautomator()24 time.sleep(4) # wait for platform to relaunch uiautomator25 get_uiautomator().setdialogtextpattern(26 u'(^(å®æ|å
³é|好|好ç|ç¡®å®|确认|å®è£
|ä¸æ¬¡å说|æä¸å é¤)$|(.*(?<!ä¸|å¦)(忽ç¥|å
(\s)?许|åæ)|继ç»|ç¨å|æä¸|ä¸ä¸æ¥).*)')27 get_uiautomator().setdialogtextgrouppattern(28 u'((建议.*æ¸
ç)|æ¯å¦å¸è½½|å¸è½½å)&&&(åæ¶|以åå说|ä¸è½½å说);æ¯å¦åéé误æ¥å&&&å¦;为äºç»æ¨æä¾ä¸°å¯çå¾ä¹¦èµæº&&&åæ¶;ç®åå¤ä»½æ¢å¤æµç¨&&&(以åå说|ä¸æ¬¡å说)')29 pkgfilter = ""30 if os.environ.get('PKGNAME'):31 pkgfilter = u'(?=(^(?!' + os.environ.get("PKGNAME") + u"$)))"32 get_uiautomator().setdialogpkgpattern(33 pkgfilter + u'^(?!(com.tencent.mm|com.tencent.mqq|com.tencent.mobileqq)$).*')34 get_uiautomator().setPermissionMonitor(True)35 time.sleep(1)36 except Exception as e:37 logger.exception(e)38def write_dialog_config(config_path):39 if os.environ.get("PKGNAME") is None:40 return41 content=""42 # with open(config_path,"r") as f:43 # content=f.read()44 textpattern=u'(^(å®æ|å
³é|好|好ç|ç¡®å®|确认|å®è£
|ä¸æ¬¡å说|æä¸å é¤)$|(.*(?<!ä¸|å¦)(忽ç¥|å
(\s)?许|åæ)|继ç»|ç¨å|æä¸|ä¸ä¸æ¥).*)'45 textGroupPattern=u'((建议.*æ¸
ç)|æ¯å¦å¸è½½|å¸è½½å)&&&(åæ¶|以åå说|ä¸è½½å说);æ¯å¦åéé误æ¥å&&&å¦;为äºç»æ¨æä¾ä¸°å¯çå¾ä¹¦èµæº&&&åæ¶;ç®åå¤ä»½æ¢å¤æµç¨&&&(以åå说|ä¸æ¬¡å说)'46 pkgfilter=""47 if os.environ.get('PKGNAME'):48 pkgfilter = u'(?=(^(?!' + os.environ.get("PKGNAME") + u"$)))"49 pkgattern =pkgfilter + u'^(?!(com.tencent.mm|com.tencent.mqq|com.tencent.mobileqq)$).*'50 content += "textpattern=" + textpattern + "\n"51 content += "textGroupPattern=" + textGroupPattern + "\n"52 content += "pkgattern=" + pkgattern +"\n"53 content += "needDialogHandler=true" +"\n"54 with open(config_path, "w") as f:55 f.write(content)56def init_uiautomator_with_dialoghandler():57 """58 åå§åuiautomator59 :return:60 """61 file_path = os.path.split(os.path.realpath(__file__))[0]62 uiautomator_stub_path = os.path.abspath(63 os.path.join(file_path,"..","third","libs","uiAutomator","DialogHandler_polling.jar"))64 #adb=AdbTool()65 config_path= os.path.abspath(66 os.path.join(file_path,"..","third","libs","uiAutomator","wetest_dialog_config.properties"))67 write_dialog_config(config_path)68 excute_adb_process("push {0} /data/local/tmp".format(uiautomator_stub_path))69 excute_adb_process("push {0} /data/local/tmp".format(config_path))70 excute_adb_process("shell am force-stop com.tencent.wetest.rc.rc_uiautomatorstub")71 kill_uiautomator()72 logger.debug("Start UIAutomator")73 #os.system("adb shell ps")74 remove_forward(_uiautomator_port)75 uiautomator_process = excute_adb_process_daemon("shell uiautomator runtest DialogHandler_polling.jar -c com.github.uiautomatorstub.Stub",shell=True ,needStdout=False)76 # uiautomator_process = excute_adb_process_daemon("shell uiautomator runtest DialogHandler.jar -c com.github.uiautomatorstub.Stub", shell=True)77 #call_adb_shell("adb forward tcp:{0} tcp:{1} && adb shell uiautomator runtest uiautomator_stand_stub.jar -c com.github.uiautomatorstub.Stub".format(_uiautomator_port,_device_port))78 # call_adb_shell(["shell","uiautomator","runtest","/data/local/tmp/uiautomator_stand_stub.jar","-c","com.github.uiautomatorstub.Stub","--nohup"])#--nohup79 time.sleep(2)80 forward(_uiautomator_port, _device_port)81 # os.system("adb shell ps")82def init_uiautomator():83 """84 åå§åuiautomator85 :return:86 """87 kill_uiautomator()88 file_path = os.path.split(os.path.realpath(__file__))[0]89 uiautomator_stub_path = os.path.abspath(90 os.path.join(file_path, "..","third","libs","uiAutomator","uiautomator-stub.jar"))91 adb=AdbTool()92 logger.debug(adb.cmd_wait("push",uiautomator_stub_path,"/data/local/tmp"))93 logger.debug("Start UIAutomator")94 uiautomator_process=adb.cmd("shell","uiautomator","runtest","uiautomator-stub.jar","-c","com.github.uiautomatorstub.Stub")95 time.sleep(2)96 adb.forward(_uiautomator_port,_device_port)97def _init():98 port = os.environ.get("UIAUTOMATORPORT")99 if port:100 return int(port)101 else:102 """103 æ¬å°ï¼åå§åUiAutomator104 """105 init_uiautomator()106 # init_uiautomator_with_dialoghandler()107 atexit.register(kill_uiautomator)108 return int(_uiautomator_port)109def get_uiautomator():110 if get_uiautomator.instance:111 return get_uiautomator.instance112 else:113 port=_init()114 get_uiautomator.instance = AutomatorDevice(None, port, os.environ.get("PLATFORM_IP", "127.0.0.1"), None)115 return get_uiautomator.instance...
uiautomator.py
Source:uiautomator.py
...3#@å建æ¥æï¼2020-06-29 23:24 4# Appiumä¹uiautomatorå®ä½å
ç´ 5# å
ç´ å®ä½æ¹å¼æå¤ç§ï¼Androidä¹æèªèº«ç¬æçå®ä½æ¹å¼ãä¸é¢å°±åç¬ä»ç»å
¶åºäºuiautomatorå®ä½å
ç´ çæ¹æ³ï¼6# åºæ¬è¯æ³ï¼7# driver.find_element_by_android_uiautomator(xx)8# 1).éè¿textææ¬å®ä½è¯æ³ï¼new UiSelector().text("textææ¬")9# # text10# loc_text = 'new UiSelector().text("å¾ä¹¦")'11# driver.find_element_by_android_uiautomator(loc_text).click()12# 2).å¦æææ¬æ¯è¾é¿ï¼å¯ä»¥ç¨textContains模ç³å¹é
ï¼new UiSelector().textContains("å
å«textææ¬")13# # textContains14# loc_textContains = 'new UiSelector().textContains("å¾")'15# driver.find_element_by_android_uiautomator(loc_textContains).click()16# 3).åæ ·å¯ä»¥ç¨textStartsWithæ¯ä»¥æ个ææ¬å¼å¤´æ¥å¹é
ï¼new UiSelector().textStartsWith("以textææ¬å¼å¤´")17# # textStartsWith18# loc_textStart = 'new UiSelector().textStartsWith("å¾")'19# driver.find_element_by_android_uiautomator(loc_textStart).click()20# 4).ä¹å¯ä»¥ç¨æ£å表达å¼textMatcheså¹é
ï¼new UiSelector().textMatches("æ£å表达å¼")21#22# 2.resourceId ä¸by_idä¸æ ·23# new UiSelector().resourceId("id")24# # resourceId25# loc_id = 'new UiSelector().resourceId("com.baidu.yuedu:id/webbooktitle")'26# driver.find_element_by_android_uiautomator(loc_id).click()27#28# 3.className 页é¢ä¸çclasså±æ§ä¸è¬ä¸å¯ä¸ï¼å¤åç¨å¨å¤æ°å®ä½æ¶åãæ¤æ¶å®ä½ç¸åºä¸æ 29# new UiSelector().className("className")30# # classNameå¤æ°å®ä½31# loc_class = 'new UiSelector().className("android.widget.TextView")'32# driver.find_elements_by_android_uiautomator(loc_class)[2].click()33#34# 4.description ä¹æ¯ç¨contenet - deså±æ§å®ä½35# new UiSelector().description("contenet-deså±æ§")36#37# 5.ç»åå®ä½38# 1).idä¸textå±æ§ç»å39# # id+text40# id_text = 'resourceId("com.baidu.yuedu:id/webbooktitle").text("å°è¯´")'41# driver.find_element_by_android_uiautomator(id_text).click()42# 2).classä¸textå±æ§ç»å43# # class+text44# class_text = 'className("android.widget.TextView").text("å¾ä¹¦")'45# driver.find_element_by_android_uiautomator(class_text).click()46#47# 6.å
³ç³»å®ä½48# 1).ç¶åå®ä½childSelector49# ææ¶åä¸è½ç´æ¥å®ä½æ个å
ç´ ï¼ä½æ¯å®çç¶å
ç´ å¾å¥½å®ä½ï¼è¿æ¶åå°±å
å®ä½ç¶å
ç´ ï¼éè¿ç¶å
ç´ æ¾å¿å50# å¦ä¸å®ä½ä¹¦æ¶ï¼51# # ç¶åå
³ç³»childSelector52# son = 'resourceId("com.baidu.yuedu:id/rl_tabs").childSelector(text("å°è¯´"))'53# driver.find_element_by_android_uiautomator(son).click()54# 2).å
å¼å®ä½fromParent55# ææ¶åç¶å
ç´ ä¸å¥½å®ä½ï¼ä½æ¯è·ä»ç¸é»çå
å¼å
ç´ å¾å¥½å®ä½ï¼è¿æ¶åå°±å¯ä»¥éè¿å
å¼å
ç´ ï¼æ¾å°åä¸ç¶56# 级å
ç´ ä¸çåå
ç´ -å¦ä¸å®ä½ä¹¦æ¶ï¼57# # å
å¼å
³ç³»fromParent58# brother = 'resourceId("com.baidu.yuedu:id/lefttitle").fromParent(text("å¾ä¹¦"))'59# driver.find_element_by_android_uiautomator(brother).click()...
setup.py
Source:setup.py
1# Copyright 2013 The Chromium Authors. All rights reserved.2# Use of this source code is governed by a BSD-style license that can be3# found in the LICENSE file.4"""Generates test runner factory and tests for uiautomator tests."""5import logging6from pylib.uiautomator import test_package7from pylib.uiautomator import test_runner8def Setup(test_options):9 """Runs uiautomator tests on connected device(s).10 Args:11 test_options: A UIAutomatorOptions object.12 Returns:13 A tuple of (TestRunnerFactory, tests).14 """15 test_pkg = test_package.TestPackage(test_options.uiautomator_jar,16 test_options.uiautomator_info_jar)17 tests = test_pkg.GetAllMatchingTests(test_options.annotations,18 test_options.exclude_annotations,19 test_options.test_filter)20 if not tests:21 logging.error('No uiautomator tests to run with current args.')22 def TestRunnerFactory(device, shard_index):23 return test_runner.TestRunner(24 test_options, device, shard_index, test_pkg)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!