Best Python code snippet using fMBT_python
devicectrl.py
Source:devicectrl.py
1import os, sys, time2import subprocess34'''设å¤æä½ç±»'''5'''teky'''6class DeviceCtrl():78 def __init__(self, deviceid):9 self.deviceid = deviceid10 self.width = 011 self.height = 012 self.judge_device()1314 def judge_device(self):15 cmd="adb devices -l"16 rt = os.popen(cmd).read()17 if not "HW" in rt:18 print("å为ææºè°è¯çï¼å
¶ä»ææºå¯è½ä¼æäºä¸å
¼å®¹ï¼åºè¯¥é®é¢ä¸å¤§ï¼èªå·±æ¹æ¹ã")19202122 def adbpush(self, localfile, despath):23 '''adb push'''24 cmd = 'adb -s ' + self.deviceid + ' push ' + localfile + ' ' + despath25 rt = os.popen(cmd).read()26 if not "pushed" in rt:27 print(rt)28 29 def adbpull(self, mobilefil, despath):30 '''adb pull'''31 cmd = 'adb -s ' + self.deviceid + ' pull ' + mobilefil + ' ' + despath32 #print('å½ä»¤:' + cmd)33 rt = os.popen(cmd).read()34 if not "pulled" in rt:35 print(rt)36373839 def dumpUI_compressed(self, xmlpath=None):40 '''è·åuiautomator dump'''41 cmd = 'adb -s ' + self.deviceid + ' shell uiautomator dump --compressed /sdcard/tmp/uidump.xml'42 rt = os.popen(cmd).read()43 if rt.find('UI hierchary dumped')>=0:44 if xmlpath:45 self.adbpull('/sdcard/tmp/uidump.xml',xmlpath)46 return xmlpath47 else:48 self.adbpull('/sdcard/tmp/uidump.xml', self.deviceid + '_uidump.xml')49 return sys.path[0] + '/'+ self.deviceid +'_uidump.xml'50 else:51 print('dump 失败:' + rt)52 return None53 54 def dumpUI(self, xmlpath=None):55 '''è·åuiautomator dump'''56 cmd = 'adb -s ' + self.deviceid + ' shell uiautomator dump /sdcard/tmp/tmp/uidump.xml'57 #print('å½ä»¤:' + cmd)58 rt = os.popen(cmd).read()59 if rt.find('UI hierchary dumped')>=0:60 if xmlpath:61 self.adbpull('/sdcard/tmp/uidump.xml',xmlpath)62 return xmlpath63 else:64 self.adbpull('/sdcard/tmp/uidump.xml', self.deviceid + '_uidump.xml')65 return sys.path[0] + '/'+ self.deviceid +'_uidump.xml'66 else:67 print('dump 失败:' + rt)68 return None69 70 def screenshot(self, filepath):71 '''è·åuiautomator dump'''72 try:73 cmd = 'adb -s ' + self.deviceid + ' shell screencap -p /data/local/tmp/screen.png'74 #print('å½ä»¤:' + cmd)75 os.popen(cmd)76 time.sleep(2)77 self.adbpull('data/local/tmp/screen.png', filepath)#å¡æ»?78 time.sleep(2)79 except Exception as e:80 print(e)81 finally:82 return8384 def stopapp(self, package):85 cmd = 'adb -s ' + self.deviceid + ' shell am force-stop ' + package86 print('å½ä»¤:' + cmd)87 os.popen(cmd).read()88 time.sleep(3)#太快åºç¨èµ·ä¸æ¥8990 def startapp(self, package, activity):91 '''å¯å¨ä¸ä¸ªåºç¨ com.android.filemanager/.FileManagerActivity'''92 cmd = 'adb -s ' + self.deviceid + ' shell am start -n ' + package + '/' + activity93 print('å½ä»¤:' + cmd)94 rt = os.popen(cmd).read()95 print(rt)96 if rt.find('Starting:')>=0:97 time.sleep(2)#çå¾
åºç¨å¯å¨98 return True99 else:100 print('å¯å¨å¤±è´¥,æ£æ¥å
ååActivityåæ°')101 return False102 103 def trywakeup(self):104 '''æ§è¡å¤éè¿åTRUEï¼åæ¥æ¯ç¹äº®çè¿åFAlSE'''105106 cmd = 'adb -s ' + self.deviceid + ' shell dumpsys window policy | grep \"mScreenOnFully\" '107 print('å½ä»¤:' + cmd)108 rt = os.popen(cmd).read()109 if rt.find('mScreenOnFully=false')>=0:#éå± æä¸powerç¹äº®110 self.presspower()#KEYCODE_POWER111 print('ç¹äº®å±å¹')112 return True113 else:114 return False115116 def trywakeoff(self):117 '''æ§è¡ççè¿åTRUEï¼åæ¥æ¯çççè¿åFAlSE'''118 cmd = 'adb -s ' + self.deviceid + ' shell dumpsys window policy | grep \"mScreenOnFully\" '119 print('å½ä»¤:' + cmd)120 rt = os.popen(cmd).read()121 if rt.find('mScreenOnFully=true')>=0:#äº®å± æä¸powerç¹äº®122 self.presspower()#KEYCODE_POWER123 print('ççå±å¹')124 return True125 else:126 return False127 128 def presshome(self):129 self.sendkeycode(KeyCodes.KEYCODE_HOME)130 time.sleep(1)131 132 def pressback(self):133 self.sendkeycode(KeyCodes.KEYCODE_BACK)134 time.sleep(1)135136 def presspower(self):137 self.sendkeycode(KeyCodes.KEYCODE_POWER)138 time.sleep(1)139140141 # def wakeup(self):142 # '''ç¹äº®å¹¶è§£é'''143 # #èªå¨åç¹äº®å¹¶æ»å¨è§£é144 # self.getdisplaysize()145 # self.trywakeup()146 # self.set_screen_off_timeout(10)147 # time.sleep(1)148 # self.unlock_by_swipe()149 # self.presshome()150151152 def wakeupme(self):153 """解éå±å¹å¹¶ä¸è¿åé¦é¡µ"""154 self.trywakeup()155 time.sleep(1)156 cmd = 'adb -s ' + self.deviceid + ' shell input keyevent 82'157 rt = os.popen(cmd).read()158 time.sleep(1)159 self.presshome()160161162 def getdisplaysize(self):163 '''å¾å°å±å¹å辨ç'''164 cmd = 'adb -s ' + self.deviceid + ' shell wm size'165 print('å½ä»¤:' + cmd)166 rt = os.popen(cmd).read()167168 if rt.find('size:')>0:169 rt = rt.split(': ')170 rt = rt[1].split('x')171 self.width = int(rt[0])172 self.height = int(rt[1])173 174 def unlock_by_swipe(self):175 '''常è§åä¸æ»å¨è§£é'''176 x1 = x2 = str(self.width/2)177 y1 = str(self.height/2 + self.height/3)178 y2 = str(self.height/2 - self.height/3)179 cmd = 'adb -s ' + self.deviceid + ' shell input swipe ' + x1 + ' '+ y1 + ' ' + x2 + ' ' + y2 + ' 200'180 print('å½ä»¤:' + cmd)181 os.popen(cmd)182183 def swipe_up(self):184 '''常è§åä¸æ»å¨'''185 x1 = x2 = str(self.width/2)186 y1 = str(self.height/2 + self.height/8)187 y2 = str(self.height/2 - self.height/8)188 cmd = 'adb -s ' + self.deviceid + ' shell input swipe ' + x1 + ' '+ y1 + ' ' + x2 + ' ' + y2 + ' 300'189 os.popen(cmd)190 time.sleep(1)191 192 def swipe_down(self):193 '''常è§åä¸æ»å¨'''194 x1 = x2 = str(self.width/2)195 y1 = str(self.height/2 - self.height/8)196 y2 = str(self.height/2 + self.height/8)197 cmd = 'adb -s ' + self.deviceid + ' shell input swipe ' + x1 + ' '+ y1 + ' ' + x2 + ' ' + y2 + ' 300'198 os.popen(cmd)199 time.sleep(1)200201 def click_tuplexy(self, tuplexy):202 '''ç¹å»åæ '''203 if not tuplexy:204 return205 cmd = 'adb -s ' + self.deviceid + ' shell input tap ' + str(tuplexy[0]) + ' '+ str(tuplexy[1])206 #print('å½ä»¤:' + cmd)207 os.popen(cmd)208 time.sleep(1)209210 def click(self, x, y):211 '''ç¹å»åæ '''212 cmd = 'adb -s ' + self.deviceid + ' shell input tap ' + str(x) + ' '+ str(y)213 os.popen(cmd)214 time.sleep(1)215216 def waitfordevice(self):217 '''éå¯åçå¾
adbè¿æ¥'''218 cmd = 'adb -s ' + self.deviceid + ' shell dumpsys window policy | grep \"mScreenOnFully\" '219 print('å½ä»¤:' + cmd)220 rt = os.popen(cmd).read()221 if rt.find('mScreenOnFully=false')>=0:222 print('è¿æ¥æå')223 return True224 else:225 print('æªè¿æ¥æå')226 time.sleep(10)227 return self.waitfordevice()228 229 def sendbroadcast(self, file_path):230 '''广æ'''231 cmd = 'adb -s ' + self.deviceid + ' shell am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d '+ file_path232 print('å½ä»¤:' + cmd)233 os.popen(cmd)234 235 def input_text(self, text):236 '''è¾å
¥æå'''237 cmd = 'adb -s ' + self.deviceid + ' shell input text '+ text238 print('å½ä»¤:' + cmd)239 os.popen(cmd)240 241 def getversion(self):242 '''è·åçæ¬å·'''243 cmd = 'adb -s ' + self.deviceid + ' shell getprop ro.build.version.release'244 print('å½ä»¤:' + cmd)245 return os.popen(cmd).read().strip()246247 def getproductname(self):248 '''è·å项ç®å称'''249 cmd = 'adb -s ' + self.deviceid + ' shell getprop ro.product.name'250 print('å½ä»¤:' + cmd)251 return os.popen(cmd).read().strip()252 253 def sendkeycode(self, keycode):254 '''模ææé®'''255 cmd = 'adb -s ' + self.deviceid + ' shell input keyevent ' + str(keycode)256 os.popen(cmd)257 #print('å½ä»¤:' + cmd)258259 def get_topfullactivity(self):260 '''è·åæä¸æ¹Activity'''261 cmd = 'adb -s ' + self.deviceid + ' shell dumpsys activity | grep mResumedActivity '262 rt = os.popen(cmd).read()263 if rt.find('mResumedActivity: ActivityRecord')>=0:264 rts = rt.split(' ')265 return rts[7]266 else:267 return None268 269 def set_screen_off_timeout(self, minute):270 '''设置éå±æ¶é´'''271 cmd = 'adb -s ' + self.deviceid + ' shell settings put system screen_off_timeout ' + str(minute*60000)272 os.popen(cmd).read()273 print('设置éå±æ¶é´' + str(minute) + 'åé')274275276277#å®ä¹keycodeä¿¡æ¯278class KeyCodes():279 KEYCODE_MENU = 1280 KEYCODE_HOME = 3281 KEYCODE_BACK = 4282 KEYCODE_VOLUME_UP = 24283 KEYCODE_VOLUME_DOWN = 25284 KEYCODE_POWER = 26285286
...
coffeemachine.py
Source:coffeemachine.py
1def pressPower(powerOn):2 print("Power Button Pressed")3 if powerOn:4 print("Machine has been turned OFF\n")5 powerOn = False6 else: 7 print("Machine has been turned ON\n")8 powerOn = True9 return powerOn10 11 1213def addWater(waterin, capsulein, powerOn):14 if waterin == True:15 print("machine already has water\n")16 else:17 if capsulein == True: 18 print("Adding water...Capsule and water in. Ready to brew\n")19 elif capsulein == False:20 print("Adding water...Water in. Insert capsule to brew\n")21 waterin = True22 return waterin232425def insertCapsule(waterin, capsulein, powerOn):26 if capsulein == True:27 print("machine already has a capsule in\n")28 else:29 if waterin == True: 30 print("Adding capsule...Capsule and water in. Ready to brew\n")31 elif capsulein == False:32 print("Adding Capsule...Capsule in. Add water to brew\n")33 waterin = True34 return waterin353637def pressBrew(waterin, capsulein, powerOn):38 if powerOn: 39 if waterin and capsulein:40 print("brewing in progress \n . \n . \n . \n . \n ")41 wait30()42 print("Cup is ready\n")43 wait300()44 elif waterin:45 print("insert capsule first\n")46 elif capsulein:47 print("add water first\n")48 else:49 print("insert capsule and add water before brewing\n")50 else:51 print("--doing nothing because power is off--\n")5253def wait30():54 print("...you are waiting 30s for the brewing to finish\n")5556def wait300():57 print("...300 second time (5min) to turn off coffee maker\n")585960def main():61 waterin = False62 capsulein = False63 powerOn = False64 65 while True:66 action1 = input("what do you want to do? Enter 'water' to add water, 'capsule' to add coffee capsule, 'power' to press power button or 'brew' to press brew button\n")67 if action1 == 'water':68 waterin = addWater(waterin, capsulein, powerOn)69 elif action1 == 'capsule':70 capsulein = insertCapsule(waterin, capsulein, powerOn)71 elif action1 == 'power':72 powerOn = pressPower(powerOn)73 elif action1 == 'brew':74 pressBrew(waterin, capsulein, powerOn)75 else:76 print("I am sorry.. that is not a proper request")7778 wait30()79 wait300()8081if __name__ == "__main__":
...
basicMenu.py
Source:basicMenu.py
1global remoteObj2def pressPower():3 remoteObj.setKey(0)4def pressMute():5 remoteObj.setKey(1)6def pressInput():7 remoteObj.setKey(2)8def pressVolPlus():9 remoteObj.setKey(3)10def pressVolMin():11 remoteObj.setKey(4)12def pressPagePlus():13 remoteObj.setKey(5)14def pressPageMin():15 remoteObj.setKey(6)16def basicMenu(mainMenu,remObj):...
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!!