How to use pressPower method in fMBT

Best Python code snippet using fMBT_python

devicectrl.py

Source:devicectrl.py Github

copy

Full Screen

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 ...

Full Screen

Full Screen

coffeemachine.py

Source:coffeemachine.py Github

copy

Full Screen

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__": ...

Full Screen

Full Screen

basicMenu.py

Source:basicMenu.py Github

copy

Full Screen

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):...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run fMBT automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful