Best Python code snippet using Airtest
Color_airtest.py
Source:Color_airtest.py
...36agree_bt, terms_link, privacy_link = is_enter_home()37touch(terms_link)38sleep(10)39flag = False40act_name = dev.get_top_activity_name()41try:42 assert_equal(act_name, "com.pixel.art.coloring.by.number/talefun.cd.sdk.webview.MyWebViewActivity", "æå¡æ¡æ¬¾é¾æ¥æ¯å¦æ£å¸¸è·³åº")43except:44 print("è·³åºå¤±è´¥")45if exists(Template(r"tpl1628653574859.png", record_pos=(-0.314, -0.596), resolution=(1080, 1920))):46 flag = True47try:48 assert_equal(flag, True, "å¤ææ¯å¦æ£å¸¸æ¾ç¤ºæå¡æ¡æ¬¾é¡µé¢")49except:50 print("æ¾ç¤ºå¤±è´¥")51keyevent('KEYCODE_BACK')52sleep(1)53touch(privacy_link)54sleep(10)55act_name = dev.get_top_activity_name()56flag = False57try:58 assert_equal(act_name, "com.pixel.art.coloring.by.number/talefun.cd.sdk.webview.MyWebViewActivity", "éç§åè®®é¾æ¥æ¯å¦æ£å¸¸è·³åº")59except:60 print("è·³åºå¤±è´¥")61if exists(Template(r"tpl1628653574859.png", record_pos=(-0.314, -0.596), resolution=(1080, 1920))):62 flag = True63try:64 assert_equal(flag, True, "å¤ææ¯å¦æ£å¸¸æ¾ç¤ºæå¡æ¡æ¬¾é¡µé¢")65except:66 print("æ¾ç¤ºå¤±è´¥")67keyevent('KEYCODE_BACK')68sleep(3)69touch(agree_bt)70sleep(1)71# è¿å
¥mypainting72touch(Template(r"tpl1628654205391.png", record_pos=(0.378, 0.822), resolution=(1080, 1920)))73sleep(2)74touch(Template(r"tpl1628654222593.png", record_pos=(-0.43, -0.82), resolution=(1080, 1920)))75sleep(2)76touch(Template(r"tpl1628666829596.png", record_pos=(-0.37, -0.632), resolution=(1080, 1920)))77sleep(1)78try:79 assert_exists(Template(r"tpl1628666871971.png", record_pos=(-0.018, -0.809), resolution=(1080, 1920)), "æå¥è°æ¥æå¼æå")80 touch(Template(r"tpl1628654344533.png", record_pos=(-0.407, -0.793), resolution=(1080, 1920)))81 sleep(1)82except Exception as e:83 print("æå¼å¤±è´¥")84 raise e85touch(Template(r"tpl1628654268937.png", record_pos=(-0.37, -0.313), resolution=(1080, 1920)))86touch(Template(r"tpl1628654287761.png", record_pos=(-0.372, 0.473), resolution=(1080, 1920)))87sleep(1)88touch(Template(r"tpl1628654314042.png", record_pos=(0.294, -0.564), resolution=(1080, 1920)))89touch(Template(r"tpl1628654344533.png", record_pos=(-0.407, -0.793), resolution=(1080, 1920)))90sleep(1)91swipe((500, 1344), (504, 153))92sleep(3)93touch(Template(r"tpl1628664592422.png", record_pos=(-0.373, -0.165), resolution=(1080, 1920)))94sleep(10)95try:96 assert_exists(Template(r"tpl1628664646850.png", record_pos=(-0.002, -0.706), resolution=(1080, 1920)), "FAQæå¼æå")97except:98 print("æå¼å¤±è´¥")99keyevent("KEYCODE_BACK")100sleep(1)101touch(Template(r"tpl1628664735847.png", record_pos=(-0.37, 0.146), resolution=(1080, 1920)))102if exists(Template(r"tpl1628664777138.png", threshold=0.8500000000000001, record_pos=(-0.378, 0.621), resolution=(1080, 1920))):103 touch(Template(r"tpl1628664777138.png", threshold=0.8500000000000001, record_pos=(-0.378, 0.621), resolution=(1080, 1920)))104 sleep(3)105 act_name = dev.get_top_activity_name()106 print(act_name)107 try:108 assert_equal(act_name, "com.google.android.gm/.ComposeActivityGmailExternal", "é®ç®±å¼¹åº")109 keyevent("KEYCODE_BACK")110 sleep(0.5)111 keyevent("KEYCODE_BACK")112 except Exception as e:113 print("é®ç®±å¼¹åºå¤±è´¥")114 raise e115 116touch(Template(r"tpl1628665851814.png", record_pos=(-0.371, 0.463), resolution=(1080, 1920)))117sleep(10)118flag = False119act_name = dev.get_top_activity_name()120try:121 assert_equal(act_name, "com.pixel.art.coloring.by.number/talefun.cd.sdk.webview.MyWebViewActivity", "æå¡æ¡æ¬¾é¾æ¥æ¯å¦æ£å¸¸è·³åº")122except:123 print("è·³åºå¤±è´¥")124if exists(Template(r"tpl1628653574859.png", record_pos=(-0.314, -0.596), resolution=(1080, 1920))):125 flag = True126try:127 assert_equal(flag, True, "å¤ææ¯å¦æ£å¸¸æ¾ç¤ºæå¡æ¡æ¬¾é¡µé¢")128except:129 print("æ¾ç¤ºå¤±è´¥")130keyevent('KEYCODE_BACK')131sleep(1)132touch(Template(r"tpl1628665908967.png", record_pos=(-0.372, 0.621), resolution=(1080, 1920)))133sleep(10)134act_name = dev.get_top_activity_name()135flag = False136try:137 assert_equal(act_name, "com.pixel.art.coloring.by.number/talefun.cd.sdk.webview.MyWebViewActivity", "éç§åè®®é¾æ¥æ¯å¦æ£å¸¸è·³åº")138except:139 print("è·³åºå¤±è´¥")140if exists(Template(r"tpl1628653574859.png", record_pos=(-0.314, -0.596), resolution=(1080, 1920))):141 flag = True142try:143 assert_equal(flag, True, "å¤ææ¯å¦æ£å¸¸æ¾ç¤ºæå¡æ¡æ¬¾é¡µé¢")144except:145 print("æ¾ç¤ºå¤±è´¥")146keyevent('KEYCODE_BACK')147sleep(1)148touch(Template(r"tpl1628654344533.png", record_pos=(-0.407, -0.793), resolution=(1080, 1920)))...
app_helper.py
Source:app_helper.py
...35 pattern = re.compile(r"(.+?)\tdevice")36 for match in pattern.finditer(list_output):37 device_list.append(match.group(1))38 return device_list39def get_top_activity_name(adb_path=None):40 adb_path = init_adb_path(adb_path)41 # simplify results42 output = os.popen(adb_path + " shell dumpsys window windows | grep -E \"mCurrentFocus\"").read()43 pattern = re.compile(r"mCurrentFocus.+?{.+?/(.+?)}")44 return pattern.search(output).group(1)45def start_activity(package_name, activity_name, adb_path=None, ):46 adb_path = init_adb_path(adb_path)47 os.popen(adb_path + " shell am start -n " + package_name + "/" + activity_name).read()48 return wait_for_activity_on_top(activity_name, adb_path)49def get_current_view_xml_tree(adb_path=None):50 adb_path = init_adb_path(adb_path)51 # ui dump on phone52 output = os.popen(adb_path + " shell uiautomator dump").read()53 pattern = re.compile(r"to:\s*(.+?.xml)")54 res = pattern.search(output)55 if res is None:56 return None57 xml_path = res.group(1)58 # add extra leading slash to avoid git-bash path issue59 xml_path = "/" + xml_path60 # copy back to current computer61 # TODO: change this to python temp file62 file_name = str(int(random.random() * RANDOM_RANGE_FOR_FILE)) + ".xml"63 file_path = "./" + TEMP_FOLDER + "/" + file_name64 os.popen(adb_path + " pull " + xml_path + " " + file_path).read()65 tree = et.parse(file_path)66 return tree67def parse_bounds_text(bounds):68 p = re.compile(r"\[(\d+),(\d+)\]\[(\d+),(\d+)\]")69 m = p.search(bounds)70 if m is None:71 return None, None, None, None72 return int(m.group(1)), int(m.group(2)), int(m.group(3)), int(m.group(4))73def wait_for_activity_on_top(name, adb_path=None):74 adb_path = init_adb_path(adb_path)75 dead_counter = 2076 while get_top_activity_name(adb_path) != name:77 time.sleep(1)78 dead_counter -= 179 if dead_counter == 0:80 print("ERROR: Dead loop, couldn't get correct top activity (current: "81 + get_top_activity_name(adb_path) + ")")82 return False83 print("Not activity - \"" + name + "\"is on top")84 return True85def click_on_screen(min_x, min_y, max_x, max_y, adb_path=None):86 adb_path = init_adb_path(adb_path)87 mid_x = (min_x + max_x) // 288 mid_y = (min_y + max_y) // 289 os.popen(adb_path + " shell input tap " + str(mid_x) + " " + str(mid_y)).read()90 return91def scroll_on_screen(from_x, from_y, to_x, to_y, adb_path=None):92 adb_path = init_adb_path(adb_path)93 return94# testing codes, remove them in production level95print(repr(get_device_list()))96print(get_top_activity_name())...
runtime.py
Source:runtime.py
...15 if m:16 return (m.group(1), m.group(2), m.group(3))17 else:18 return None19 def get_top_activity_name(self):20 """21 Get the top activity name22 Returns:23 package, activity and pid24 """25 tanp = self.get_top_activity_name_and_pid()26 if tanp:27 return tanp[0] + '/' + tanp[1]28 else:29 return None30 @property31 def name(self):32 return self._name33 ...
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!!