def traverse(pageNum, resultFolder): current = d.dump('temp.xml', compressed=False) thirdPartyComponentList = {} parser = xml.sax.make_parser() parser.setFeature(xml.sax.handler.feature_namespaces, 0) Handler = DumpFileHandler(thirdPartyComponentList) parser.setContentHandler(Handler) parser.parse('temp.xml') thirdPartyComponentList = Handler.thirdPartyComponentList if len(thirdPartyComponentList) > 0: d.screenshot(os.path.join(resultFolder, str(pageNum) + ".png")) d.dump(os.path.join(resultFolder, str(pageNum) + ".xml"), compressed=False) writePath = os.path.join(resultFolder, str(pageNum) + '.txt') writeFile = open(writePath, 'w') for component in thirdPartyComponentList: writeFile.write(component + '\n') writeFile.close() componentList = {} UISelectorList = d(packageName=packageName, clickable=True) for component in UISelectorList: componentList[component] = 'clickable' for component in componentList.keys(): if componentList[component] == 'clickable' and component.exists: print(component.info) component.click() new = d.dump('temp.xml', compressed=False) print(current == new) if (current != new): print('here') traverse(pageNum + 1, resultFolder) d.press.back()
def check_clicked(clicked_list): global back_counter global dev #os.system('adb start-server') #from uiautomator import device as dev #time.sleep(5) #dev.info # dump ui hierarchy into a xml file and extract info from it ISOTIMEFORMAT = '%m%d-%H-%M-%S' global current_time, nodelist current_time = time.strftime(ISOTIMEFORMAT, time.localtime()) dev.dump("./data/" + current_time + "hierarchy.xml") dev.screenshot("./data/" + current_time + ".png") dom = xml.dom.minidom.parse("./data/" + current_time + "hierarchy.xml") global flag_start_logcat global flag_start_activity global flag_stop_activity # 得到xml文档元素对象 root = dom.documentElement nodelist = root.getElementsByTagName('node') nodelist = nodelist_redef(nodelist) for i in nodelist: if i not in clicked_list: ui_interaction(i) return nodelist dev.press.back() global flag_back flag_back = True print "back2" back_counter += 1 return []
def read_inside_msg(): a=d.dump() count_1=d(resourceId='com.whatsapp:id/message_text').count time.sleep(30) count_2=d(resourceId='com.whatsapp:id/message_text').count b=d.dump() if a != b and count_1!=count_2: sendMessageOnWhatsApp("Hi")
def list_click(self): d(text='全部', className='android.widget.TextView').click() d.press.back() d.dump("hierarchy.xml") info = d(className="android.widget.ListView", resourceId="android:id/list").info # print(info) conunt_ = info['childCount'] for view in range(conunt_): self.check_pkg(view + 1)
def test_case(self): d.press.down() print "Done" d.press.down() d.press.down() d.press.down() d.press.down() d(text=u'无线网络').click() d.dump('hierarchy.xml') xml = d.dump() print type(xml)
def performEventsOnApp(): max_iterations = 5 i = 1 print("Current Package") while (i <= max_iterations): d.dump("Output3.xml") doc = minidom.parse("Output3.xml") nodes = doc.getElementsByTagName("node") package = doc.getElementsByTagName("node")[0].getAttribute("package") length = nodes.length print(d(text="START").exists) num_events_iteration = 50 for x in range(num_events_iteration): print(package) print(getCurrentPackageName()) if (package != getCurrentPackageName()): d.press.back() node = nodes[random.randint(1, length - 1)] print(node.getAttribute("class")) axisCoordinates = node.getAttribute("bounds").split("]") xAxis = axisCoordinates[0].replace('[', '').split(",") xCoordinate = (int(xAxis[0]) + int(xAxis[1])) / 2 yAxis = axisCoordinates[1].replace('[', '').split(",") yCoordinate = (int(yAxis[0]) + int(yAxis[1])) / 2 #screenshot before event --ss1 #d.click(xCoordinate, yCoordinate) randomEvent = loadRandomEvent() eventMessage = "No Event" #d.screenshot("BeforeEvent.png") if randomEvent == 'click': d.click(xCoordinate, yCoordinate) eventMessage = "Performed click at", str(xCoordinate), str( yCoordinate) elif randomEvent == 'swipe': d.swipe(xAxis[0], xAxis[1], yAxis[0], yAxis[1]) eventMessage = "Performed Swipe at", xAxis, yAxis elif randomEvent == 'press': d.press(xCoordinate, yCoordinate) eventMessage = "Performed press at", str(xCoordinate), str( yCoordinate) elif randomEvent == 'freeze': d.freeze_rotation() eventMessage = "Performed freeze rotation" #d.screenshot("AfterEvent.png") compareImage(eventMessage) #screenshot after event -ss2 #use some kind of image similarity algorithm: open cv(computer vision library) for #comparing these screenshots #identify threshold % i = i + 1
def runSimulation(self, duration, package_name, random, device_serial, session): from uiautomator import device as d # possible bug # initilize status logging self.event_log = logging.getLogger("start_status") self.event_log.setLevel(logging.DEBUG) START_LOG = os.path.join(session.logs_dir, "interaction.log") ih = logging.FileHandler(START_LOG) ih.setLevel(logging.ERROR) formatter = logging.Formatter("%(asctime)s - %(message)s") ih.setFormatter(formatter) self.event_log.addHandler(ih) # try: d.screen.on() adb = Adb() start_package_command = "monkey -p %s -c android.intent.category.LAUNCHER 1" % package_name adb.shell(start_package_command, device_serial=device_serial) self.event_log.info("Started %s" % package_name) time.sleep(10) # check screen now has the same package name visited_screen = [] screen_widgets = d.dump() root = ET.fromstring(screen_widgets) # DFS trigger self.dfs_trigger( current_screen=root, duration=duration, package_name=package_name, device_serial=device_serial, visited_screen=visited_screen, )
def dump(): xml = d.dump("screen.xml") with open("screen.csv", 'w', encoding="utf-8") as f: f.write("text,content-desc,id" + "\n") soup = BeautifulSoup(xml, "html.parser") for node in soup.find_all("node"): line = f"{node['text']},{node['content-desc']},{node['resource-id']}" f.write(line + "\n")
def getCoordinatesFromMatrix(index): d.wait.idle() soup = BeautifulSoup(d.dump(), 'xml') w, h = getWindowSize(soup) w0, h0, y0 = getItemSizeAndStartY(soup) print("w0, h0, y0 : ", w0, h0, y0) m = int(w/w0) #no of pics in a row print("m: ", m) x_req = ((index % m) - 0.5) * w0 y_req = y0 + (int(index/m) + 0.5) * h0 return [x_req, y_req]
def automate(self, decider): last_action = None try: print 'Begin automation.' while True: state = State() state.xml = device.dump() action = decider.predict(state) print 'Predicted action: %s' % action action.call() except KeyboardInterrupt: print 'Finished automation.'
def run_test(): logging.info("--- Start Test ---") sp.Popen('adb -s %s logcat -c' % device_id, shell=True) os.popen('adb -s %s install %s'%(device_id,apk_name)) os.popen('adb -s %s shell am start -n %s/%s'%(device_id,package,activity)) xml = d.dump() d.press(5) d.press.back() d(scrollable=True).fling() d(scrollable=True).fling.vert.backward() d(className='android.widget.RelativeLayout')[9].child( className='android.widget.CheckBox').click() d(text='viber').sibling(className='android.widget.CheckBox').click() logging.info("--- Test Finished! ---")
def dfs_trigger(self, current_screen, duration, package_name, device_serial, visited_screen): from uiautomator import device as d if current_screen == {}: # print "reach end of the world" d.press.back() self.event_log.info("back") return True for child in current_screen.iter("node"): node_dict = child.attrib m = hashlib.md5() m.update(node_dict.__str__()) md5 = m.digest() if not visited_screen.__contains__(md5): visited_screen.append(md5) # press device to go to next screen if node_dict["long-clickable"] == True: d(text=node_dict["text"], className=node_dict["class"]).long_click() self.event_log.info("long click: %s" % node_dict) else: d(text=node_dict["text"], className=node_dict["class"]).click() self.event_log.info("short click: %s" % node_dict) screen_widgets = d.dump() subScreen = ET.fromstring(screen_widgets) return self.dfs_trigger( current_screen=subScreen, duration=duration, package_name=package_name, device_serial=device_serial, visited_screen=visited_screen, ) # print "child screens visited!" d.press.back() # self.event_log.info("back") return True
def handle_window(): #dev.wait.idle() global clk_seq, back_counter, parent_width, newnode_counter global menu_index, menu_flag, last_is_back current_time = time.strftime(ISOTIMEFORMAT, time.localtime()) dev.dump("./data/" + current_time + "hierarchy.xml") dom = xml.dom.minidom.parse("./data/" + current_time + "hierarchy.xml") root = dom.documentElement nodelist = [] node_clk = {'node': None, 'index': -1} window = [] parent_width = 0 newnode_counter = 0 menu_flag = False DFS_xml(root, nodelist, window, node_clk) if newnode_counter > 3: #if press_menu: # dev.press.menu() if menu_flag: # indicates new window print 'MENU FOUND>>>>>>>>>>>>>' if shown_clickable[menu_index]['width'] == 0: shown_clickable[menu_index]['width'] += 1 print 'MMMMMMMMMMMMMMMMMMMMMMMMMENU AGAINNNNNNNN' #print node_clk['index'] print window #print shown_clickable print clk_seq if len(clk_seq): prinode_index = clk_seq[len(clk_seq) - 1] prinode = shown_clickable[prinode_index] print parent_width # if not is_same_window(window, priwindow): # flag = 0 # for i in window_list: # if is_same_window(window, i): # flag = 1 # break # if flag == 0: # window_list.append(window) # else: # samewindow += 1 #print node_clk['index'] #priwindow = window if node_clk['index'] != -1: node = shown_clickable[node_clk['index']] ui_interaction(node_clk['node'], current_time) print node['node_attri'] node['width'] -= 1 if clk_seq: parent_index = node['parent'] parent = shown_clickable[parent_index] parent['width'] -= 1 while parent['parent']: parent_index = parent['parent'] parent = shown_clickable[parent_index] parent['width'] -= 1 print 'just do it !!!!!!!!!!!!!! on ' print parent_index clk_seq.append(node_clk['index']) last_is_back -= 1 if last_is_back < -15: dev.press.back() last_is_back = 1 return True else: #clk_seq.append(-1) dev.press.back() print 'back' prinode['width'] = 0 back_counter += 1 last_is_back += 1 return False
if __name__ == '__main__': # import subprocess # subprocess.check_call('adb shell uiautomator dump /data/local/tmp/window_dump.xml') # subprocess.check_call('adb pull /data/local/tmp/window_dump.xml') # xmldata = open('window_dump.xml').read() import time import traceback import locale encoding = locale.getpreferredencoding() from uiautomator import device device.dump() layout = AndroidLayout() layout.highlight = np.zeros((1, 1, 3), np.uint8) cv2.namedWindow("layout") def on_mouse(event, x, y, flags, param): layout, downpos, ismove = param # record downpos if event == cv2.EVENT_LBUTTONDOWN: print 'click at', x * 2, y * 2 # picture is half-sized. param[1] = (x, y) param[2] = False return
}, "just": { "q": "", "a": ["", "", ""] }, "stup": { "q": "", "a": ["", "", ""] }, "loco": { "q": "", "a": ["", "", ""] } } xml = d.dump() soup = BeautifulSoup(xml, 'lxml-xml') question = soup.findAll("node", {"resource-id": cheat[app]["q"]})[0]["text"] answer_one = soup.findAll("node", {"resource-id": cheat[app]["a"][0]})[0] answer_two = soup.findAll("node", {"resource-id": cheat[app]["a"][1]})[0] answer_three = soup.findAll("node", {"resource-id": cheat[app]["a"][2]})[0] #search question directly url = "https://www.google.co.in/search?q={}".format(question.encode('utf-8')) webbrowser.open(url) #occurrence count print "\nOCCURRENCE COUNT\n****************" r = requests.get(url) response_text = r.text print answer_one["text"], "\t:", sum(
# get top score selector if __name__ == '__main__': # import subprocess # subprocess.check_call('adb shell uiautomator dump /data/local/tmp/window_dump.xml') # subprocess.check_call('adb pull /data/local/tmp/window_dump.xml') # xmldata = open('window_dump.xml').read() import time import traceback import locale encoding = locale.getpreferredencoding() from uiautomator import device device.dump() layout = AndroidLayout() layout.highlight = np.zeros((1, 1, 3), np.uint8) cv2.namedWindow("layout") def on_mouse(event, x, y, flags, param): layout, downpos, ismove = param # record downpos if event == cv2.EVENT_LBUTTONDOWN: print 'click at', x*2, y*2 # picture is half-sized. param[1] = (x, y) param[2] = False return
def screen_dump(): """Realiza um 'dump' da tela atual e retorna como ElementTree""" logger.debug('Despejando tela do dispositivo') dump = DEVICE.dump() return xml.etree.ElementTree.fromstring(dump)
from uiautomator import device as d d.dump('login.xml')
#https://github.com/xiaocong/uiautomator from uiautomator import device as d d.dump("hierarchy.xml") import time import datetime import random #d = Device('014E05DE0F02000E', adb_server_host='192.168.1.68', adb_server_port=5037) def auto_read(): #from uiautomator import device as d #d.screen.on() import uiautomator2 as u2 d = u2.connect() d.swipe(500, 100 * random.uniform(3, 5), 500, 100) if d(text="展开全文").exists: d(text='展开全文').click() def home(): d.press.home() #d.press.back() #d.press("back") #d.press(0x07, 0x02) def lock(): d.freeze_rotation() def unlock():
# In[8]: d.press.camera() # In[14]: d.orientation # In[90]: d.screenshot("home.png") # In[84]: d.dump("hierarchy.xml", compressed=False) # In[104]: d(scrollable='true').info # In[82]: d.dump("hierarchy.xml") # In[40]: d.watcher("AUTO_FC_WHEN_ANR").when(clickable="true").click(text="") # In[23]:
# Check if state is a permission state if resourceId == str(denyPermissionsId+allowPermissionsId): print("Permission state detected.") permissionState = True # Return Hash return hashlib.md5((package+view+resourceId+contentDesc+longClickable+scroll)).digest().encode("base64") def generateHashedHierachy(state): return hashlib.md5(state).digest().encode("base64") # Use uiAutomator's dump to get screen details state1 = d.dump(compressed=True).encode('utf-8') time.sleep(1.5) # Initialise all required arrays/variables view = [] resourceId = [] contentDesc = [] clickableXCoor = np.array([]) clickableYCoor = np.array([]) eClickableXCoor = np.array([]) eClickableYCoor = np.array([]) cVisitFreq = [] package = '' state = '' longClickable = [] longClickableIndex = []
#subprocess.Popen(cmd, stdout=subprocess.PIPE, # stderr=subprocess.STDOUT, shell = True) time.sleep(2) cmd = 'adb -s ' + series + ' pull /sdcard/screenshot.png ./monkeylog/' + x + current_time + '.png' os.system(cmd) capture_pcap(True) os.system('adb -s ' + series +' shell monkey -p ' + x + ' -vvv --throttle 5 --ignore-crashes 480 > ./monkeylog/' + x + current_time + '.txt') #monkey_process = subprocess.Popen('adb -s ' + series +' shell monkey -p ' + x + ' -vvv --throttle 5 --ignore-crashes 480 > ./monkeylog/' + x + current_time + '.txt', stdout=subprocess.PIPE, # stderr=subprocess.STDOUT, shell = True) print 'start monkey' print dev.info #dev.screenshot('./monkeylog/' + x + current_time + '.png') #subprocess.Popen(cmd, stdout=subprocess.PIPE, # stderr=subprocess.STDOUT, shell = True) dev.dump('./monkeylog/' + x + current_time + '.xml') print 'screenshot' #time.sleep(15) #monkey_process.terminate() os.system('adb -s ' + series +' shell am force-stop ' + x) print 'done monkey' capture_pcap(False) print 'done pcap' os.system('adb -s ' + series +' uninstall ' + x) print 'done uninstall' os.system('adb -s ' + series +' pull /sdcard/' + x + current_time + '.pcap ./pcap/' ) os.system('cp ' + fline + ' ./monkeylog/' + x + current_time + '.apk') # rename the apk
def __uidump(self): """ 获取当前Activity控件树 """ d.dump("hierarchy.xml") print(d.dump())
def getXmlSnapshot(): xml = d.dump() #message_1=d(resourceId="com.whatsapp:id/single_msg_tv" ).text soup = BeautifulSoup(xml, 'xml') #print(message_1) print(xml)
from uiautomator import device as d d.dump("loco.xml")
from uiautomator import device as d import os import re import time text = '*****@*****.**' d.click(350, 565) time.sleep(0.5) # get emailname os.system('adb shell input text "{}"'.format(text)) d.click(350, 655) time.sleep(0.5) d.click(350, 655) time.sleep(0.5) # get password text = 'lingxiuguigu' os.system('adb shell input text "{}"'.format(text)) time.sleep(1) d(description="Login").click() time.sleep(10) d.dump('string') with open('string', 'r') as f: string = f.read() code = re.findall('([\d]{6}|[\d]{5}) is your', string)[0]
def handle_window(): #dev.wait.idle() global clk_seq, back_counter, parent_width, newnode_counter global menu_index, menu_flag, last_is_back current_time = time.strftime(ISOTIMEFORMAT, time.localtime()) dev.dump(dir_data + current_time + "hierarchy.xml") dev.screenshot(dir_data + current_time + ".png") logger.info('screen shot at ' + current_time) dom = xml.dom.minidom.parse(dir_data + current_time + "hierarchy.xml") root = dom.documentElement nodelist = [] node_clk = {'node': None, 'index': -1, 'node_attri': {}} window = [] parent_width = 0 newnode_counter = 0 menu_flag = False DFS_xml(root, nodelist, window, node_clk) if newnode_counter > 3: #if is a new window if menu_flag: # and meet with menu again logger.info('MENU FOUND>>>>>>>>>>>>>') if shown_clickable[menu_index]['width'] == 0: shown_clickable[menu_index]['width'] += 1 logger.info('MMMMMMMMMMMMMMMMMMMMMMMMMENU AGAINNNNNNNN') # print node_clk['index'] #print window # useful for debug... # print shown_clickable #print clk_seq # useful for debug... # if not is_same_window(window, priwindow): # flag = 0 # for i in window_list: # if is_same_window(window, i): # flag = 1 # break # if flag == 0: # window_list.append(window) # else: # samewindow += 1 # print node_clk['index'] # priwindow = window if node_clk['index'] != -1: node = shown_clickable[node_clk['index']] ui_interaction(node_clk['node'], current_time) logger.info(node_clk['node_attri']) #print node_clk['index'] #print node['node_attri'] node['width'] -= 1 if clk_seq: parent_index = node['parent'] parent = shown_clickable[parent_index] parent['width'] -= 1 while parent['parent']: parent_index = parent['parent'] parent = shown_clickable[parent_index] parent['width'] -= 1 #print 'just do it !!!!!!!!!!!!!! on ' #print parent_index clk_seq.append(node_clk['index']) last_is_back -= 1 if last_is_back < -15: dev.press.back() last_is_back = 1 return True else: #clk_seq.append(-1) dev.press.back() logger.info('press back') if len(clk_seq): prinode_index = clk_seq[len(clk_seq) - 1] prinode = shown_clickable[prinode_index] prinode['width'] = 0 back_counter += 1 if last_is_back < 0: last_is_back = 0 last_is_back += 1 return False
def getXmlSnapshot(): xml = d.dump() soup = BeautifulSoup(xml, 'xml') print(xml)
time.sleep(2) # ==================== 滑動到 System ====================== print "Scroll to System and then click" d(className="android.support.v7.widget.RecyclerView", resourceId="com.android.settings:id/dashboard_container") \ .child_by_text( "System", allow_scroll_search=True, className="android.widget.TextView" ).click() time.sleep(2) # ==================== 設備操作 - 截屏和dump ================ print "Capture the screen" d.screenshot("home.png") d.dump("home.xml") d.orientation = "n" print "Go back to home screen" d.press.back() d.press.back() d.press.home() print "Operate the Calculator" d(text="Calculator").click() # ====================== UI對象 - 點擊 ========================== d(text="3", className="android.widget.Button").click() d(text="×", className="android.widget.Button").click() d(text="9", className="android.widget.Button").click() d(text="=", className="android.widget.Button").click() time.sleep(1)
def _add_ui_object_not_found_handler(self): """Logs the device dump upon uiautomator.UiObjectNotFoundException.""" from uiautomator import device as d d.handlers.on(lambda d: logging.debug('Device window dump:\n%s', d.dump()))
from uiautomator import device as d import time # dump the widown hierarchy and save to local file "hierarchy.xml" d.dump('/Users/tsui/eclipse-workspace/MonkeyDump/AndroidXML/' + str(time.time()) + '.xml') # or get the dumped content(unicode) from return. #xml = d.dump()