class SetTimeHandle(): def __init__(self,driver,screen_x,screen_y): self.driver = driver self.swip = Swip(screen_x, screen_y, driver) self.setTime_p = SetTimePage(driver) self.myDevice_p = MyDevicePage(driver) self.loger = UserLog().get_logger() #输入要设置的时间 def enter_setTime(self,time): # sleep(10) # ele = self.myDevice_p.get_meat_thermometer() # ele.click() # 进入定时设置界面 sleep(8) ele1 = self.myDevice_p.get_setTime_element() ele1.click() sleep(2) ele2 = self.setTime_p.get_delete_element() if ele2 != None: ele2.click() self.set_hours() sleep(1) self.set_min() sleep(1) self.set_second() sleep(1) ele3 = self.setTime_p.get_finish_element() ele3.click() def set_hours(self): start_x = 301 start_y = 1579 self.swip.swipe_up(85,start_x,start_y) def set_min(self): start_x = 680 start_y = 1579 self.swip.swipe_up(85, start_x, start_y) def set_second(self): start_x = 1059 start_y = 1579 self.swip.swipe_up(85, start_x, start_y)
class LightingEffectsLibrary(): dataList = '' def __init__(self, platformVersion, mobileName, device, com, bps, timeout): desired_caps = {} desired_caps['platformName'] = 'Android' desired_caps['platformVersion'] = platformVersion desired_caps['deviceName'] = mobileName desired_caps['appPackage'] = 'com.govee.home' # desired_caps['app'] = 'F:// debug.apk' desired_caps['appActivity'] = 'com.govee.home.HomeActivity' desired_caps['noReset'] = 'true' desired_caps['automationName'] = 'Uiautomator2' desired_caps['adbExecTimeout'] = 200000 self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) # 获取屏幕分辨率 self.screen_x = self.driver.get_window_size()['width'] # 屏幕宽度--单位是px self.screen_y = self.driver.get_window_size()['height'] # 屏幕的高度-单位是px self.swip = Swip(self.screen_x, self.screen_y, self.driver) # self.myDevice_p = MyDevicePage(self.driver) self.user_log = UserLog() self.loger = self.user_log.get_logger() # self.h6199_p = H6199MainPage(self.driver) self.device = device self.start = True #创建串口 self.port = com self.bps = bps self.timeout = timeout global Ret try: # 打开串口,并得到串口对象 self.main_engine = serial.Serial(self.port, self.bps, timeout=self.timeout) self.main_engine.flushInput() # 清空缓冲区 # 判断是否打开成功 if (self.main_engine.is_open): Ret = True except Exception as e: print("---异常---:", e) #进入设备主页 def enter_H6199_1540_main_face(self): try: WebDriverWait( self.driver, 20, 0.5 ).until(lambda x: x.find_element_by_android_uiautomator( 'new UiSelector().text(\"%s\")' % self.device)).is_displayed() except: print('进入设备详情页失败') else: sleep(2) self.driver.find_element_by_android_uiautomator( 'new UiSelector().text(\"%s\")' % self.device).click() #颜色切换 def switch_color(self, times): self.enter_H6199_1540_main_face() n = 0 sleep(4) try: WebDriverWait( self.driver, 30, 0.5).until(lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"灯效库\")")).is_displayed() except: print('进入设备详情页失败') else: sleep(2) self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"灯效库\")").click() try: WebDriverWait( self.driver, 20, 0.5).until(lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"灯效库\")")).is_displayed() except: print('进入灯效库页失败') else: sleep(3) for k in range(7): for j in range(3, 11, 2): for i in range(5): # 开始操作 sleep(1) self.driver.find_element_by_xpath( '//*[@resource-id="com.govee.home:id/rv_data"]/android.widget.RelativeLayout[%s]' % j + '/androidx.recyclerview.widget.RecyclerView[1]/android.widget.LinearLayout[%s]' % (i + 1)).click() sleep(2) # 监测串口日志 check_result = self.com_log_check(self.dataList) if check_result: n += 1 #向上滑 self.swip.swipe_up(862) sleep(2) self.swip.swipe_up(839) sleep(2) self.loger.info('颜色切换[%s]次,' % times + '成功了[%s]次!!!!!!!!' % n) #监测串口日志 def com_log_check(self, dataList): start = len(dataList) - 300 end = len(dataList) str1 = dataList[start:end] str2 = str1.replace(" ", "").replace("\r", "").replace("\n", "") # 去掉空格、回车符、换行符 strlist = str2.split(':', 10) # 以冒号进行分割 #打印串口日志 self.loger.info('本次操作后蓝牙日志:' + str(strlist)) for i in range(len(strlist)): sl = strlist[i][len(strlist[i]) - 7:len(strlist[i])] if sl == 'blesend': print('blesend:' + strlist[i + 1]) self.loger.info('蓝牙回复结果:blesend:' + str(strlist[i + 1])) if strlist[i + 1][4] == '0' and strlist[i + 1][5] == '0': self.loger.info('本次颜色切换成功') print('本次颜色切换成功') return True else: self.loger.info('本次颜色切换失败') print('本次颜色切换失败') return False # 截图 def screenshot(self, msg): # 截图保存 now = time.strftime("%Y-%m-%d %H-%M-%S") image_name = LOG_DIR + '/' + msg + " " + now + ".png" self.driver.save_screenshot(image_name) # 接收串口数据 def recv(self): print("开始接收数据:") while True: try: count = self.main_engine.inWaiting() # 获取串口缓冲区数据 if count != 0: data = self.main_engine.read( self.main_engine.in_waiting).decode( "gbk") # 读出串口数据,数据采用gbk编码 time.sleep(0.1) # 延时0.1秒,免得CPU出问题 self.dataList += data except Exception as e: print("异常报错:", e)
class H6087MainHandle(): dataList = '' def __init__(self, driver, device, com, bps, timeout, com2, bps2, bytesize, stopbits, Ret1, Ret2): self.device = device self.driver = driver self.Ret1 = Ret1 self.Ret2 = Ret2 #日志 self.loger = logging.getLogger( 'H6087MainCase.sub.module') #多模块使用logging # 获取屏幕分辨率 self.screen_x = self.driver.get_window_size()['width'] # 屏幕宽度--单位是px self.screen_y = self.driver.get_window_size()['height'] # 屏幕的高度-单位是px self.swip = Swip(self.screen_x, self.screen_y, self.driver) #选择设备 self.ch_device = ChooesDevice(self.device, self.driver) #打印串口 self.port = com self.bps = bps self.timeout = timeout self.start = True #截图路径 self.image_path = setting.TEST_ERROR_Image # global Ret1 try: # 打开串口,并得到串口对象 self.main_engine = serial.Serial(self.port, self.bps, timeout=self.timeout) self.main_engine.flushInput() # 清空缓冲区 # 判断是否打开成功 if (self.main_engine.is_open): self.Ret1 = True except Exception as e: print("---异常---:", e) #控制串口 self.port2 = com2 self.bps2 = bps2 self.bytesize = bytesize self.stopbits = stopbits # global Ret2 try: # 打开串口,并得到串口对象 self.main_engine2 = serial.Serial(self.port2, self.bps2, timeout=self.timeout, bytesize=self.bytesize, stopbits=self.stopbits) # 判断是否打开成功 if (self.main_engine2.is_open): self.Ret2 = True #初始化继电器类 self.relay_h = RelayHandle(self.main_engine2) except Exception as e: print("---异常---:", e) #设备配网 def connect_H6087(self, skip_wifi, times): if (self.Ret1 and self.Ret2): #功能线程运行 threading.Thread(target=self.add_device, name='add_device', args=( skip_wifi, times, )).start() #日志线程运行 threading.Thread( target=self.recv, name='recvData', ).start() else: print("创建串口失败") #模式切换 def modeChange_H6087(self): if (self.Ret1 and self.Ret2): #功能线程运行 threading.Thread( target=self.mode_change, name='mode_change', ).start() #日志线程运行 threading.Thread( target=self.recv, name='recvData', ).start() else: print("创建串口失败") def mode_change(self): n = 0 #进入设备详情页 try: WebDriverWait( self.driver, 120, 1 ).until(lambda x: x.find_element_by_android_uiautomator( 'new UiSelector().text(\"%s\")' % self.device)).is_displayed() except: self.loger.info('没有找到该设备,请先添加设备') else: self.driver.find_element_by_android_uiautomator( 'new UiSelector().text(\"%s\")' % self.device).click() #DIY模式切换 WebDriverWait(self.driver, 20, 1).until(lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"DIY\")")).click() #开始DIY模式循环切换 while True: # try: # #DIY-流水 # WebDriverWait(self.driver, 10, 1).until( # lambda x: x.find_element_by_android_uiautomator( # "new UiSelector().text(\"流水\")")).click() # self.loger.info('切换流水diy模式') # print('切换流水diy模式') # sleep(3) # # 关机 # self.relay_h.once_click() # self.loger.info('物理按键,控制设备关机') # print('物理按键,控制设备关机') # sleep(3) # # 开机 # self.relay_h.once_click() # self.loger.info('物理按键,控制设备开机') # print('物理按键,控制设备开机') # # # DIY-灿烂 # WebDriverWait(self.driver, 10, 1).until( # lambda x: x.find_element_by_android_uiautomator( # "new UiSelector().text(\"灿烂\")")).click() # self.loger.info('切换灿烂diy模式') # print('切换灿烂diy模式') # sleep(3) # # 关机 # self.relay_h.once_click() # self.loger.info('物理按键,控制设备关机') # print('物理按键,控制设备关机') # sleep(3) # # 开机 # self.relay_h.once_click() # self.loger.info('物理按键,控制设备开机') # print('物理按键,控制设备开机') # # # DIY-四季 # WebDriverWait(self.driver, 10, 1).until( # lambda x: x.find_element_by_android_uiautomator( # "new UiSelector().text(\"四季\")")).click() # self.loger.info('切换四季diy模式') # print('切换四季diy模式') # sleep(3) # # 关机 # self.relay_h.once_click() # self.loger.info('物理按键,控制设备关机') # print('物理按键,控制设备关机') # sleep(3) # # 开机 # self.relay_h.once_click() # self.loger.info('物理按键,控制设备开机') # print('物理按键,控制设备开机') # # # DIY-摇摆 # WebDriverWait(self.driver, 10, 1).until( # lambda x: x.find_element_by_android_uiautomator( # "new UiSelector().text(\"摇摆\")")).click() # self.loger.info('切换摇摆diy模式') # print('切换摇摆diy模式') # sleep(3) # # 关机 # self.relay_h.once_click() # self.loger.info('物理按键,控制设备关机') # print('物理按键,控制设备关机') # sleep(3) # # 开机 # self.relay_h.once_click() # self.loger.info('物理按键,控制设备开机') # print('物理按键,控制设备开机') # # n += 1 # self.loger.info('第[%s]循环次切换DIY模式结束' % n) # print('第[%s]循环次切换DIY模式结束' % n) # except Exception as e: # self.loger.info(e) # self.loger.info('DIY模式切换失败') # print('DIY模式切换失败') try: # 音乐模式 try: WebDriverWait(self.driver, 60, 0.2).until( EC.element_to_be_clickable((By.ID, 'mode_1_icon'))) except: self.loger.info('蓝牙连接失败') self.screenshot('蓝牙连接失败') self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"重新连接\")").click() else: WebDriverWait(self.driver, 10).until( lambda x: x.find_element_by_id("mode_1_icon")).click() if self.check_ble_status(): self.loger.info('切换音乐模式后,蓝牙连接正常') sleep(2) self.loger.info('再次物理点击颜色按键') self.relay_h.once_click() else: self.loger.info('切换音乐模式后,蓝牙断连') self.screenshot('切换音乐模式后,蓝牙断连') WebDriverWait(self.driver, 60, 0.5).until( lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"重新连接\")")).click() sleep(3) # 颜色模式 try: WebDriverWait(self.driver, 60, 0.2).until( EC.element_to_be_clickable((By.ID, 'mode_2_icon'))) except: self.loger.info('蓝牙连接失败') self.screenshot('蓝牙连接失败') self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"重新连接\")").click() else: WebDriverWait(self.driver, 10).until( lambda x: x.find_element_by_id("mode_2_icon")).click() if self.check_ble_status(): self.loger.info('切换颜色模式后,蓝牙连接正常') sleep(2) self.loger.info('再次物理点击颜色按键') self.relay_h.once_click() else: self.loger.info('切换颜色模式后,蓝牙断连') self.screenshot('切换颜色模式后,蓝牙断连') WebDriverWait(self.driver, 60, 0.5).until( lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"重新连接\")")).click() sleep(3) # 场景模式 try: WebDriverWait(self.driver, 60, 0.2).until( EC.element_to_be_clickable((By.ID, 'mode_3_icon'))) except: self.loger.info('蓝牙连接失败') self.screenshot('蓝牙连接失败') self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"重新连接\")").click() else: WebDriverWait(self.driver, 10).until( lambda x: x.find_element_by_id("mode_3_icon")).click() if self.check_ble_status(): self.loger.info('切换场景模式后,蓝牙连接正常') sleep(2) self.loger.info('再次物理点击颜色按键') self.relay_h.once_click() else: self.loger.info('切换场景模式后,蓝牙断连') self.screenshot('切换场景模式后,蓝牙断连') WebDriverWait(self.driver, 60, 0.5).until( lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"重新连接\")")).click() # DIY模式 sleep(3) try: WebDriverWait(self.driver, 60, 0.2).until( EC.element_to_be_clickable((By.ID, 'mode_4_icon'))) except: self.loger.info('蓝牙连接失败') self.screenshot('蓝牙连接失败') self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"重新连接\")").click() else: WebDriverWait(self.driver, 10).until( lambda x: x.find_element_by_id("mode_4_icon")).click() if self.check_ble_status(): self.loger.info('切换DIY模式后,蓝牙连接正常') # self.loger.info('----------------------------------') sleep(2) self.loger.info('再次物理点击颜色按键') self.relay_h.once_click() else: self.loger.info('切换DIY模式后,蓝牙断连') self.screenshot('切换DIY模式后,蓝牙断连') WebDriverWait(self.driver, 60, 0.5).until( lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"重新连接\")")).click() except Exception as e: self.loger.info(e) self.loger.info('模式切换失败') print('模式切换失败') #检查蓝牙状态 def check_ble_status(self): try: WebDriverWait( self.driver, 5, 0.5).until(lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"已连接\")")).is_displayed() except: return False else: return True #设备配网 #1、次数 2、是否跳过WiFi设置 def add_device(self, skip_wifi, times): result = [0, 0] m = 0 app_check = False com_check = False for i in range(times): # 选择设备类型 try: self.ch_device.chooes_device() m += 1 # 扫描蓝牙 while True: try: WebDriverWait(self.driver, 120, 1).until( lambda x: x.find_element_by_android_uiautomator( 'new UiSelector().text(\"%s\")' % self.device) ).is_displayed() except: self.loger.info('蓝牙扫描失败,重新扫描') self.screenshot('蓝牙扫描失败') try: WebDriverWait( self.driver, 60, 1).until(lambda x: x. find_element_by_android_uiautomator( "new UiSelector().text(\"重新扫描\")") ).is_displayed() except: self.loger.info('重新扫描按钮不可点击') else: self.loger.info('点击重新扫描按钮') self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"重新扫描\")").click() else: self.loger.info('蓝牙扫描成功') sleep(3) ele = self.driver.find_element_by_android_uiautomator( 'new UiSelector().text(\"%s\")' % self.device) sleep(1) ele.click() break # 进入配对界面,点击设备开关按键 try: WebDriverWait(self.driver, 60, 1).until( lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"配对\")")).is_displayed() except: self.loger.info('进入蓝牙配对界面失败') # 蓝牙没有连接成功时,重新连接,直到成功 while True: try: WebDriverWait( self.driver, 10, 1).until(lambda x: x. find_element_by_android_uiautomator( 'new UiSelector().text(\"%s\")' % self.device)).is_displayed() except: self.loger.info('连接成功') break else: self.loger.info('重新连接') self.driver.find_element_by_android_uiautomator( 'new UiSelector().text(\"%s\")' % self.device).click() else: self.loger.info('进入蓝牙配对界面成功') result = self.connect_ble_wifi(skip_wifi, result[0], result[1]) except Exception as e: print(e) self.loger.info(e) continue self.start = False self.sum = 0 if skip_wifi: self.sum = int(result[1]) else: self.sum = int(result[0]) print('[%s]' % self.device + '设备配网[%s]次,' % m + '成功了[%s]次,' % self.sum + ',配网成功率%s' % (self.sum / m * 100) + '%') self.loger.info('[%s]' % self.device + '设备配网[%s]次,' % m + '成功了[%s]次,' % self.sum + ',配网成功率%s' % (self.sum / m * 100) + '%') def connect_ble_wifi(self, skip_wifi, n, k): result_list = [] # 等待时间 wait_time = random.randint(1, 25) #随机生成一个[1,25]之间的整数,包括1、25 sleep(wait_time) print('等待了%s秒' % wait_time) self.loger.info('等待了%s秒' % wait_time) self.relay_h.once_click() # 蓝牙连接成功,点击完成 try: WebDriverWait( self.driver, 80, 1).until(lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"完成\")")).is_displayed() except: self.loger.info('蓝牙配对失败') self.screenshot('蓝牙配对失败') print('蓝牙配对失败') while True: try: WebDriverWait(self.driver, 80, 1).until( lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"完成\")")).is_displayed() except: self.loger.info('再次配对') self.driver.find_element_by_android_uiautomator( 'new UiSelector().text(\"%s\")' % self.device).click() sleep(2) self.relay_h.once_click() else: self.loger.info('再次配对成功') break else: # 点击完成 self.loger.info('蓝牙配对成功') print('蓝牙配对成功') self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"完成\")").click() # 进入WiFi配置界面 try: WebDriverWait( self.driver, 120, 1).until(lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"完成\")")).is_displayed() except: self.loger.info('wifi设置页面加载失败,跳过WiFi设置') print('wifi设置页面加载失败,跳过WiFi设置') self.screenshot('wifi设置页面加载失败') # 跳过WiFi设置 self.skip_wifiSetting() #进入设备详情页 # self.enter_gateway_detial() # 删除设备 self.delete_lempDevice() else: # 是否跳过WiFi设置 if skip_wifi: self.loger.info('跳过WiFi设置') print('跳过WiFi设置') # 跳过WiFi设置 self.skip_wifiSetting() k += 1 else: # 判断设备是否添加成功 self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"完成\")").click() try: WebDriverWait(self.driver, 180, 0.1).until( EC.presence_of_element_located( (By.XPATH, '//*[contains(@text,"连接成功")]'))) except: print('WiFi连接失败') self.loger.info('WiFi连接失败') self.screenshot('WiFi连接失败') # continue # wifi连接失败时点击跳过 self.skip_wifiSetting() else: n += 1 print('WiFi连接成功') self.loger.info('WiFi连接成功') print('第[%s]次' % n + '配网成功') self.loger.info('第[%s]次' % n + '配网成功') sleep(1) # 点击确定 # self.driver.find_element_by_android_uiautomator("new UiSelector().text(\"确定\")").click() result_list.clear() result_list.append(n) result_list.append(k) # 删除设备 self.delete_lempDevice() return result_list # 删除设备 def delete_lempDevice(self): # btn_settting = self.driver.find_element_by_id("btn_setting") try: WebDriverWait( self.driver, 10).until(lambda x: x.find_element_by_id("btn_setting")) except: print('没有设置按钮') self.loger.info('没有设置按钮') else: sleep(1) self.driver.find_element_by_id("btn_setting").click() sleep(2) # 向下滑动 self.swip.swipe_up(600) self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"删除设备\")").click() sleep(1) yes = self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"是\")") yes.click() sleep(1) print('删除设备') self.loger.info('删除设备') # 刷新首页 self.swip.swipe_down(600) #跳过WiFi设置 def skip_wifiSetting(self): sleep(1) self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"跳过\")").click() sleep(1) self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"确认\")").click() sleep(1) # # 点击确定 # self.driver.find_element_by_android_uiautomator("new UiSelector().text(\"确定\")").click() # 监测蓝牙配对日志 def com_bluetooth_adddevice_log_check(self, dataList): start = len(dataList) - 2500 end = len(dataList) str1 = dataList[start:end] str2 = str1.replace(" ", "").replace("\r", "").replace("\n", "") # 去掉空格、回车符、换行符 strlist = str2.split(':', 50) # 以冒号进行分割 # 打印串口日志 self.loger.info('配网日志:' + str(strlist)) print('配网日志:' + str(strlist)) sl = '33b200' if str(strlist).find(sl) >= 0: self.loger.info('本次配网日志校验成功') print('本次配网日志校验成功') return True else: print('本次配网日志校验失败') self.loger.info('本次配网日志校验失败') return False #监测蓝牙控制日志 def com_bluetooth_normal_log_check(self, dataList, mlen, option_type, sl=None): start = len(dataList) - mlen end = len(dataList) str1 = dataList[start:end] str2 = str1.replace(" ", "").replace("\r", "").replace("\n", "") # 去掉空格、回车符、换行符 strlist = str2.split(':', 50) # 以冒号进行分割 #打印串口日志 self.loger.info('本次操作后蓝牙日志:' + str(strlist)) print('本次操作后蓝牙日志:' + str(strlist)) if option_type == 'mode_switch': for i in range(len(strlist)): sl = strlist[i][len(strlist[i]) - 7:len(strlist[i])] if sl == 'blesend': print('blesend:' + strlist[i + 1]) self.loger.info('蓝牙回复结果:blesend:' + str(strlist[i + 1])) if strlist[i + 1][0] == '3' and strlist[ i + 1][4] == '0' and strlist[i + 1][5] == '0': self.loger.info('本次蓝牙下操作成功') print('本次蓝牙下操作成功') return True else: self.loger.info('本次蓝牙下操作失败') print('本次蓝牙下操作失败') return False else: check = False for i in range(len(strlist)): if str(strlist[i]).find(sl) >= 0: self.loger.info(strlist[i]) print(strlist[i]) check = True break else: check = False return check #监测普通WiFi日志 def com_wifi_normal_log_check(self, dataList, sl, option_type): if option_type == 'on_off_switch': start = len(dataList) - 3400 elif option_type == 'leve_switch': start = len(dataList) - 1400 else: start = len(dataList) - 2200 end = len(dataList) str1 = dataList[start:end] str2 = str1.replace(" ", "").replace("\r", "").replace("\n", "") # 去掉空格、回车符、换行符 strlist = str2.split(':', 50) # 以冒号进行分割 # 打印串口日志 self.loger.info('本次操作后日志:' + str(strlist)) for i in range(len(strlist)): if strlist[i].find(sl) >= 0: print('串口日志:' + strlist[i]) self.loger.info('串口日志:' + strlist[i]) return strlist[i] # 监测WiFi配网日志 def com_wifi_adddevice_log_check(self, dataList): start = len(dataList) - 8000 end = len(dataList) str1 = dataList[start:end] str2 = str1.replace(" ", "").replace("\r", "").replace("\n", "") # 去掉空格、回车符、换行符 strlist = str2.split(':', 80) # 以冒号进行分割 # 打印串口日志 self.loger.info('wifi联网日志:' + str(strlist)) print('wifi联网日志:' + str(strlist)) sl = 'IOT_BLE_WIFI_CONNECT_SUC' if str(strlist).find(sl) >= 0: print('本次WiFi连接成功') return True else: print('本次WiFi连接失败') return False # 接收串口数据 def recv(self): if self.start: print("开始接收数据:") else: print("测试完毕,停止接收数据:") while self.start: try: count = self.main_engine.inWaiting() # 获取串口缓冲区数据 if count != 0: data = self.main_engine.read( self.main_engine.in_waiting).decode( "gbk") # 读出串口数据,数据采用gbk编码 # print(time.strftime("%Y-%m-%d %H-%M-%S"), " --- recv --> ") # 打印一下子 time.sleep(0.1) # 延时0.1秒,免得CPU出问题 self.dataList += data data = data.replace("\r", "") self.loger.info(data) except Exception as e: print("异常报错:", e) # 截图 def screenshot(self, msg): # 截图保存 now = time.strftime("%Y-%m-%d %H-%M-%S") image_name = self.image_path + '/' + msg + " " + now + ".png" self.driver.save_screenshot(image_name) # # # if __name__ == '__main__': # platformVersion = '10' #安卓系统版本 # mobileName = 'R58M36MWZ1B' #手机名称 # device = 'H7130_C583' #设备名称 # times = 100 #测试次数 # Ret1 = False #日志打印串口是否创建成功标志 # Ret2 = False #控制串口是否创建成功标志 # com1 = 'com13' # bps = 115200 # timeout = 0.5 # com2 = "com12" # bps2 = 9600 # timex = 0.5 # bytesize = 8 # stopbits = 1 # ms = Humidifier2(platformVersion,mobileName,device,com1, bps, timeout,com2,bps2,bytesize,stopbits) # if(Ret1 and Ret2): # #功能线程运行 # # threading.Thread(target=ms.add_device,args=(times,)).start() # # threading.Thread(target=ms.leve_switch, args=(times,)).start() # threading.Thread(target=ms.on_off_switch, args=(times,)).start() # #日志线程运行 # threading.Thread(target=ms.recv,).start() # # ms.add_device(times) # else: # print("创建串口失败")
class WifiConnect(): def __init__(self, driver, deviceName, myloger, screen_x, screen_y): self.driver = driver self.device = deviceName # 日志 self.loger = myloger # 截图路径 self.image_path = setting.TEST_ERROR_Image self.swip = Swip(screen_x, screen_y, self.driver) #wifi连接 def wifi_connect(self, skip_wifi, n, k): result_list = [] # 进入WiFi配置界面 try: WebDriverWait( self.driver, 120, 1).until(lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"完成\")")).is_displayed() except: self.loger.info('wifi设置页面加载失败,跳过WiFi设置') print('wifi设置页面加载失败,跳过WiFi设置') self.screenshot('wifi设置页面加载失败') # 跳过WiFi设置 self.skip_wifiSetting() # 进入设备详情页 # self.enter_gateway_detial() # 删除设备 self.delete_lempDevice() else: # 是否跳过WiFi设置 if skip_wifi: self.loger.info('跳过WiFi设置') print('跳过WiFi设置') # 跳过WiFi设置 self.skip_wifiSetting() k += 1 else: # 判断设备是否添加成功 sleep(1) self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"完成\")").click() sleep(0.5) self.loger.info('点击了完成按钮') self.screenshot('点击了完成按钮') try: WebDriverWait(self.driver, 180, 0.1).until( EC.presence_of_element_located( (By.XPATH, '//*[contains(@text,"连接成功")]'))) except: print('WiFi连接失败') self.loger.info('WiFi连接失败') self.screenshot('WiFi连接失败') # continue # wifi连接失败时点击跳过 self.skip_wifiSetting() else: n += 1 print('WiFi连接成功') self.loger.info('WiFi连接成功') print('第[%s]次' % n + '配网成功') self.loger.info('第[%s]次' % n + '配网成功') sleep(1) # 点击确定 # self.driver.find_element_by_android_uiautomator("new UiSelector().text(\"确定\")").click() result_list.clear() result_list.append(n) result_list.append(k) # 删除设备 self.delete_lempDevice() return result_list # 删除设备 def delete_lempDevice(self): # btn_settting = self.driver.find_element_by_id("btn_setting") try: WebDriverWait( self.driver, 10).until(lambda x: x.find_element_by_id("btn_setting")) except: print('没有设置按钮') self.loger.info('没有设置按钮') else: sleep(1) self.driver.find_element_by_id("btn_setting").click() sleep(2) # 向下滑动 self.swip.swipe_up(600) self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"删除设备\")").click() sleep(1) yes = self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"是\")") yes.click() sleep(1) print('删除设备') self.loger.info('删除设备') # 刷新首页 self.swip.swipe_down(600) # 跳过WiFi设置 def skip_wifiSetting(self): sleep(1) self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"跳过\")").click() sleep(1) self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"确认\")").click() sleep(1) # # 点击确定 # self.driver.find_element_by_android_uiautomator("new UiSelector().text(\"确定\")").click() # 截图 def screenshot(self, msg): # 截图保存 now = time.strftime("%Y-%m-%d %H-%M-%S") image_name = self.image_path + '/' + msg + " " + now + ".png" self.driver.save_screenshot(image_name)
class DingDingMainHandle(): def __init__(self, driver): self.driver = driver # 获取屏幕分辨率 self.screen_x = self.driver.get_window_size()['width'] # 屏幕宽度--单位是px self.screen_y = self.driver.get_window_size()['height'] # 屏幕的高度-单位是px self.swip = Swip(self.screen_x, self.screen_y, self.driver) def learn(self): threading.Thread(target=self.learn_center, ).start() def learn_center(self): # 点击智岩科技 WebDriverWait(self.driver, 60, 0.5).until( EC.presence_of_element_located( (By.XPATH, '//android.widget.FrameLayout[2]/android.widget.ImageView[1]' ))).click() # 进入云课堂 WebDriverWait(self.driver, 60, 0.5).until( EC.presence_of_element_located(( By.XPATH, '//android.webkit.WebView/android.view.View[1]/android.view.View[3]/android.view.View[1]/android.view.View[1]/android.view.View[4]/android.view.View[1]/android.view.View[2]/android.view.View[7]/android.view.View[1]' ))).click() # 点击学习中心 sleep(10) x = 534 y = 2840 try: print('点击学习中心') self.driver.tap([(x, y)]) except Exception as e: print(e) #选择类型 WebDriverWait(self.driver, 60, 0.5).until( EC.presence_of_element_located(( By.XPATH, '//android.webkit.WebView/android.view.View[1]/android.view.View[1]/android.widget.Image[2]' ))).click() sleep(3) try: #视频 self.driver.tap([(720, 1380)]) #确定 sleep(1) self.driver.tap([(720, 2700)]) except Exception as e: print(e) print('开始播放所有视频') try: for j in range(4, 28, 6): self.click_screen(j) for i in range(50): sleep(8) self.swip.swipe_down(50) #返回视频列表 self.driver.find_element_by_id('back_icon').click() #第二页 print('滑到第二页') try: sleep(3) self.swip.swipe_up(800) sleep(2) self.swip.swipe_up(1000) except Exception as e: print(e) sleep(3) for j in range(7, 31, 6): self.click_screen(j) for i in range(50): sleep(8) self.swip.swipe_down(50) #返回视频列表 self.driver.find_element_by_id('back_icon').click() print('滑到第三页') sleep(3) try: self.swip.swipe_up(300) sleep(2) self.swip.swipe_down(260) sleep(3) self.swip.swipe_up(800) sleep(2) self.swip.swipe_up(1000) except Exception as e: print(e) sleep(1.5) for j in range(7, 31, 6): self.click_screen(j) for i in range(70): sleep(8) self.swip.swipe_down(50) #返回视频列表 self.driver.find_element_by_id('back_icon').click() print('滑到第四页') sleep(3) try: self.swip.swipe_up(300) sleep(2) self.swip.swipe_down(260) sleep(3) self.swip.swipe_up(800) sleep(2) self.swip.swipe_up(1000) except Exception as e: print(e) sleep(1.5) for j in range(7, 31, 6): self.click_screen(j) for i in range(70): sleep(8) self.swip.swipe_down(50) #返回视频列表 self.driver.find_element_by_id('back_icon').click() except Exception as e: print(e) # 点击播放 def click_screen(self, index): try: sleep(3) WebDriverWait(self.driver, 5, 0.5).until( EC.presence_of_element_located( (By.XPATH, '//android.webkit.WebView/android.view.View[%s]' % (index)))).click() # self.driver.find_element_by_xpath('//*[@resource-id="com.govee.home:id/rv_scenes"]/android.widget.RelativeLayout[%s]/android.widget.ImageView[1]'%(index + 1)).click() print('开始播放') sleep(2) except Exception as e: print(e)
class WIFISetHandle(): def __init__(self, driver, screen_x, screen_y, loger): self.wifiSet_p = WiFiSetPage(driver) self.driver = driver self.loger = loger self.swip = Swip(screen_x, screen_y, driver) self.search_scan_handle = Search_Sacn_Device_Handle(driver, loger) def connect_wifi(self, device_name): sleep(4) self.loger.info('开始添加H6199') # 搜索6199设备型号 self.search_scan_handle.chooes_device('H6199') # 扫描设备 self.search_scan_handle.scan_device(device_name) sleep(3) # 设置WIFI # self.wifi_set_handle.set_wifi() try: WebDriverWait( self.driver, 120, 1).until(lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"完成\")")).is_displayed() except: self.loger.info('检测摄像头第一次失败') self.load_fail('检测摄像头第一次失败') # 重试 sleep(1) ele = self.driver.find_element_by_id('iv_choose') sleep(1) ele.click() sleep(1) self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"重试\")").click() try: WebDriverWait( self.driver, 120, 1).until(lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"完成\")")).is_displayed() except: self.loger.info('检测摄像头第二次失败,本次添加结束') self.load_fail('检测摄像头第二次失败') self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"跳过\")").click() self.delete_lempDevice() return False else: # 继续下面操作 result = self.continue_connect(device_name) return result else: # 继续下面操作 result = self.continue_connect(device_name) return result # 摄像头检测成功后,继续下面操作 def continue_connect(self, device_name): self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"完成\")").click() self.loger.info('检测摄像头成功,进入WIFI设置页成功') # 校准设备 try: WebDriverWait(self.driver, 120, 1).until(EC.element_to_be_clickable((By.ID, 'done'))) except: self.loger.info('WIFI第一次连接失败') self.load_fail('WIFI第一次连接失败') # 重新连接 self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"完成\")").click() try: WebDriverWait(self.driver, 120, 1).until( EC.element_to_be_clickable((By.ID, 'done'))) except: sleep(3) self.loger.info('WIFI第二次连接失败,结束本次配网') self.load_fail('WIFI第二次连接失败') # 跳过 # self.driver.find_element_by_android_uiautomator("new UiSelector().text(\"跳过\")").click() #返回 self.goback_deleteDevice() self.loger.info('退出然后删除设备') return False else: sleep(3) # 检查WIFI是否连接成功 self.loger.info('WIFI连接成功') self.load_fail('WIFI连接成功') # 跳过 # self.driver.find_element_by_android_uiautomator("new UiSelector().text(\"跳过\")").click() #返回 self.goback_deleteDevice() self.loger.info('退出然后删除设备') return True else: # 检查WIFI是否连接成功 self.loger.info('WIFI连接成功') self.load_fail('WIFI连接成功') # 跳过 # self.driver.find_element_by_android_uiautomator("new UiSelector().text(\"跳过\")").click() # 返回 self.goback_deleteDevice() self.loger.info('退出然后删除设备') return True #返回主页,删除设备 def goback_deleteDevice(self): self.skip_wifiSet() self.delete_lempDevice() # 删除设备 def delete_lempDevice(self): try: WebDriverWait(self.driver, 120, 1).until( EC.element_to_be_clickable((By.ID, 'btn_setting'))) except: self.loger.info('没有设置按钮') else: self.driver.find_element_by_id('btn_setting').click() sleep(3) self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"删除设备\")").click() sleep(1) yes = self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"是\")") yes.click() sleep(1) # 刷新首页 self.swip.swipe_down(600) sleep(3) def check(self): try: WebDriverWait( self.driver, 120, 1).until(lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"删除设备\")")).is_displayed() except: self.load_fail('WiFi连接失败截图') btn_settting = self.driver.find_element_by_id("btn_back") sleep(1) btn_settting.click() return False else: self.load_fail('WiFi连接成功截图') return True #检测WiFi连接Toast def check_toast(self): # 用于生成xpath定位 相当于 "//*[@text='没有找到用户名或密码']" toast_message = "设备Wi-Fi连接成功" message = '//*[@text=\'{}\']'.format(toast_message) # 获取toast提示框内容 toast_element = WebDriverWait( self.driver, 60, 0.2).until(lambda x: x.find_element_by_xpath(message)) print(toast_element.text) #添加断言 # if toast_element.text == toast_message: # self.load_fail('WiFi连接成功截图') # return True # else: # self.load_fail('WiFi连接失败截图') # return False # 加载失败时截图 def load_fail(self, msg): # 截图保存 now = time.strftime("%Y-%m-%d %H-%M-%S") image_name = setting.TEST_ERROR_Image + '/' + msg + " " + now + ".png" self.driver.save_screenshot(image_name) def set_wifi(self, password=None): # 设置默认WIFI密码 if password == None: password = '******' self.chooesWifi() self.setWifiPassword(password) # WIFI设置完成点击完成 self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"完成\")").click() #设置要连接的WIFI def chooesWifi(self): ele = self.wifiSet_p.get_wifi_arrow_element() sleep(1) ele.click() # 连接WIFI ele3 = WebDriverWait( self.driver, 10, 0.5).until(lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"41\")")) if ele3: ele3.click() #如果没发现,向下滑动以下 else: self.swip.swipe_up(300) # 连接WIFI ele2 = WebDriverWait( self.driver, 10, 0.5).until(lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"41\")")) if ele2: ele2.click() else: self.loger.info('WIFI选择失败,未找到指定WIFI。') sleep(2) self.loger.info('WIFI选择成功。') #设置WIFI密码 def setWifiPassword(self, password): #密码元素 ele = self.wifiSet_p.get_password_element() sleep(1) ele.click() #调用自己封装的函数删除文本内容 self.clean_text(ele.get_attribute('text')) sleep(1) #输入WIFI密码 ele.send_keys(password) self.loger.info('WIFI密码设置成功。') #清空文本 def clean_text(self, text): self.driver.keyevent(123) # 123代表光标移动到末尾键 for i in range(0, len(text)): self.driver.keyevent(67) # 67退格键 def skip_wifiSet(self): sleep(4) ele = self.wifiSet_p.get_skip_element() sleep(1) ele.click()
class H5183MainHandle(): dataList = '' def __init__(self,driver,device, com, bps, timeout,com2,bps2,bytesize,stopbits,Ret1,Ret2): self.device = device self.driver = driver self.Ret1 = Ret1 self.Ret2 = Ret2 #日志 self.loger = logging.getLogger('H5183MainCase.sub.module') #多模块使用logging # 获取屏幕分辨率 self.screen_x = self.driver.get_window_size()['width'] # 屏幕宽度--单位是px self.screen_y = self.driver.get_window_size()['height'] # 屏幕的高度-单位是px self.swip = Swip(self.screen_x, self.screen_y, self.driver) #选择设备 self.ch_device = ChooesDevice(self.device,self.driver) #打印串口 self.port = com self.bps = bps self.timeout = timeout self.start = True self.stopbits = stopbits self.bytesize = bytesize #截图路径 self.image_path = setting.TEST_ERROR_Image # global Ret1 try: # 打开串口,并得到串口对象 self.main_engine = serial.Serial(self.port, self.bps, timeout=self.timeout,bytesize=self.bytesize,stopbits=self.stopbits) self.main_engine.flushInput() # 清空缓冲区 # 判断是否打开成功 if (self.main_engine.is_open): self.Ret1 = True except Exception as e: print("---异常---:", e) #控制串口 self.port2 = com2 self.bps2 = bps2 self.bytesize = bytesize self.stopbits = stopbits # global Ret2 try: # 打开串口,并得到串口对象 self.main_engine2= serial.Serial(self.port2,self.bps2,timeout=self.timeout,bytesize=self.bytesize,stopbits=self.stopbits) # 判断是否打开成功 if (self.main_engine2.is_open): self.Ret2 = True #初始化继电器类 self.relay_h = RelayHandle(self.main_engine2) except Exception as e: print("---异常---:", e) def ota(self,times): if(self.Ret1 and self.Ret2): #功能线程运行 threading.Thread(target=self.ota_handle,args=(times,)).start() #日志线程运行 threading.Thread(target=self.recv,).start() else: print("创建串口失败") # 接收串口数据 def recv(self): if self.start: print("开始接收数据:") else: print("测试完毕,停止接收数据:") while self.start: try: count = self.main_engine.inWaiting() # 获取串口缓冲区数据 if count != 0: data = self.main_engine.read(self.main_engine.in_waiting).decode("gbk") # 读出串口数据,数据采用gbk编码 # print(time.strftime("%Y-%m-%d %H-%M-%S"), " --- recv --> ") # 打印一下子 time.sleep(0.1) # 延时0.1秒,免得CPU出问题 self.dataList += data data = data.replace("\r", "") self.loger.info(data) except Exception as e: print("异常报错:", e) # 截图 def screenshot(self, msg): # 截图保存 now = time.strftime("%Y-%m-%d %H-%M-%S") image_name = setting.TEST_ERROR_Image + '/' + msg + " " + now + ".png" self.driver.save_screenshot(image_name) def openDevice(self): if (self.Ret1 and self.Ret2): # 长按设备开机键,开启设备 self.relay_h.long_click() # 日志线程运行 threading.Thread(target=self.recv, ).start() else: print("创建串口失败") #ota升级 def ota_handle(self,times): n = 0 m = 0 k = 0 p = 0 self.app_check = False self.com_log_check = False try: WebDriverWait(self.driver, 60, 1).until( lambda x: x.find_element_by_android_uiautomator( 'new UiSelector().text(\"%s\")' % self.device)).is_displayed() except: self.loger.info('没有找到该设备,无法进入设备详情页') print('没有找到该设备,无法进入设备详情页') else: sleep(2) self.driver.find_element_by_android_uiautomator( 'new UiSelector().text(\"%s\")' % self.device).click() sleep(2) # 进入设置页 btn_settting = self.driver.find_element_by_id("btn_setting") sleep(1) btn_settting.click() sleep(3) print('开始向上滑动') # 向上滑动 try: sleep(3) self.swip.swipe_up(600) sleep(3) self.swip.swipe_up(600) sleep(3) self.swip.swipe_up(400) sleep(3) self.swip.swipe_up(400) sleep(2) except Exception as e: print(e) for i in range(times): try: while True: self.driver.find_element_by_android_uiautomator("new UiSelector().text(\"固件版本\")").click() try: WebDriverWait(self.driver, 3, 0.5).until( EC.element_to_be_clickable((By.ID, 'btn_update'))).is_displayed() except: self.loger.info('没有检测到升级,等待') print('没有检测到升级,等待') p += 1 if p >10: p = 0 print('等了10次还没有检测到升级,返回app首页重新进入') self.driver.find_element_by_id('btn_back').click() sleep(2) self.driver.find_element_by_id('btn_back').click() WebDriverWait(self.driver, 60, 0.5).until( lambda x: x.find_element_by_android_uiautomator('new UiSelector().text(\"%s\")' % self.device)).click() sleep(2) self.driver.find_element_by_id("btn_setting").click() # 向上滑动 try: sleep(3) self.swip.swipe_up(600) sleep(3) self.swip.swipe_up(600) sleep(3) self.swip.swipe_up(400) sleep(3) self.swip.swipe_up(400) sleep(2) except Exception as e: print(e) else: m += 1 sleep(2) # 点击升级按钮 self.driver.find_element_by_id('btn_update').click() self.loger.info('开始升级') print('开始升级') p = 0 break try: # until_not是当某元素消失或什么条件不成立则继续执行 WebDriverWait(self.driver, 3, 0.1).until( EC.presence_of_element_located((By.XPATH, '//*[contains(@text,"设备升级")]'))) except: self.loger.info('点击升级按钮3秒内没有出现升级弹窗') print('点击升级按钮3秒内没有出现升级弹窗') else: self.loger.info('出现升级弹窗') print('出现升级弹窗') try: #until_not是当某元素消失或什么条件不成立则继续执行 WebDriverWait(self.driver, 8, 0.5).until_not( EC.presence_of_element_located((By.XPATH, '//*[contains(@text,"设备升级")]'))) except: try: WebDriverWait(self.driver, 120, 0.5).until_not( EC.presence_of_element_located((By.XPATH, '//*[contains(@text,"设备升级")]'))) except: print('2分钟设备都没有升级完') self.screenshot('2分钟设备都没有升级完') else: # 校验串口日志 # check_result = self.com_bluetooth_log_check(self.dataList, 'success') # if check_result: # print('串口端检测到ota升级成功') # self.loger.info('串口端检测到ota升级成功') # self.com_log_check = True # else: # print('串口端检测到ota升级失败') # self.loger.info('串口端检测到ota升级失败') # self.com_log_check = False #校验app ui try: # WebDriverWait(self.driver, 3, 0.1).until( # EC.presence_of_element_located((By.XPATH, '//*[contains(@text,"升级成功")]'))) WebDriverWait(self.driver, 3, 0.1).until( lambda x: x.find_element_by_android_uiautomator( 'new UiSelector().text(\"升级成功,请手动重启设备\")')).is_displayed() except: print('app端检测到ota升级失败') self.loger.info('app端检测到ota升级失败') self.app_check = False else: print('app端检测到ota升级成功') self.loger.info('app端检测到ota升级成功') self.app_check = True if self.app_check : n += 1 self.screenshot('第[%s]次OTA成功' %n) self.loger.info('第[%s]次OTA成功' %n) print('第[%s]次OTA成功' %n) sleep(2) # 点击完成 self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"知道了\")").click() #重启设备 self.relay_h.long_click() else: k += 1 self.screenshot('第[%s]次OTA失败' %k) self.loger.info('第[%s]次OTA失败' %k) print('第[%s]次OTA失败' %k) sleep(3) self.driver.find_element_by_id('btn_back').click() # 重启设备 sleep(2) self.relay_h.long_click() else: k += 1 print('开始升级失败') self.loger.info('开始升级失败') self.screenshot('开始升级失败') sleep(3) self.driver.find_element_by_id('btn_back').click() # 重启设备 sleep(2) self.relay_h.long_click() except Exception as e: print(e) continue self.loger.info('ota升级[%s]次' % m + ',成功了[%s]次!' % n+',失败了[%s]次!'%k+',成功率:'+str(n/m*100)+'%.') print('ota升级[%s]次' % m + ',成功了[%s]次!' % n + ',失败了[%s]次!' % k + ',成功率:' + str(n / m * 100) + '%.') # 监测串口日志 def com_bluetooth_log_check(self, dataList, sl): start = len(dataList) - 1500 end = len(dataList) str1 = dataList[start:end] str2 = str1.replace(" ", "").replace("\r", "").replace("\n", "") # 去掉空格、回车符、换行符 strlist = str2.split(':', 40) # 以冒号进行分割 # 打印串口日志 self.loger.info('蓝牙ota日志:' + str(strlist)) print('蓝牙ota日志:' + str(strlist)) if str(strlist).find(sl) >= 0: return True else: return False
class H7130MainHandle(): dataList = '' def __init__(self, driver, device, com, bps, timeout, com2, bps2, bytesize, stopbits, Ret1, Ret2): self.device = device #日志 self.user_log = UserLog(self.device) self.loger = self.user_log.get_logger() self.driver = driver self.Ret1 = Ret1 self.Ret2 = Ret2 # 获取屏幕分辨率 self.screen_x = self.driver.get_window_size()['width'] # 屏幕宽度--单位是px self.screen_y = self.driver.get_window_size()['height'] # 屏幕的高度-单位是px self.swip = Swip(self.screen_x, self.screen_y, self.driver) #打印串口 self.port = com self.bps = bps self.timeout = timeout self.start = True # global Ret1 try: # 打开串口,并得到串口对象 self.main_engine = serial.Serial(self.port, self.bps, timeout=self.timeout) self.main_engine.flushInput() # 清空缓冲区 # 判断是否打开成功 if (self.main_engine.is_open): self.Ret1 = True except Exception as e: print("---异常---:", e) #控制串口 self.port2 = com2 self.bps2 = bps2 self.bytesize = bytesize self.stopbits = stopbits # global Ret2 try: # 打开串口,并得到串口对象 self.main_engine2 = serial.Serial(self.port2, self.bps2, timeout=self.timeout, bytesize=self.bytesize, stopbits=self.stopbits) # 判断是否打开成功 if (self.main_engine2.is_open): self.Ret2 = True except Exception as e: print("---异常---:", e) def connect_H7130(self, times): if (self.Ret1 and self.Ret2): #功能线程运行 threading.Thread(target=self.add_device, args=(times, )).start() # threading.Thread(target=self.leve_switch, args=(times,)).start() # threading.Thread(target=self.on_off_switch, args=(times,)).start() #日志线程运行 threading.Thread(target=self.recv, ).start() else: print("创建串口失败") # 选择要添加的设备类型 def chooes_device(self): sleep(4) # 点击+号 # ele = self.driver.find_element_by_xpath('//android.widget.ImageView[2]') ele = self.driver.find_element_by_id('iv_add') sleep(2) ele.click() sleep(3) self.loger.info('开始添加设备') # 点击搜索 WebDriverWait(self.driver, 30, 1).until(EC.element_to_be_clickable( (By.ID, 'tv_search'))).click() sleep(2) # 切换输入法,方便后续调用输入法的搜索按钮 self.driver.activate_ime_engine( 'com.sec.android.inputmethod/.SamsungKeypad') # 搜索框输入device_type self.driver.press_keycode(14) sleep(0.5) self.driver.press_keycode(8) sleep(0.5) self.driver.press_keycode(10) sleep(0.5) self.driver.press_keycode(7) WebDriverWait(self.driver, 30, 1).until(EC.element_to_be_clickable( (By.ID, 'sku_icon'))).click() #设备配网 def add_device(self, times): n = 0 #成功次数 m = 0 #配网执行次数 k = 0 #WiFi列表加载失败次数 app_check = False com_check = False for i in range(times): #选择设备类型 try: self.chooes_device() m += 1 #连接蓝牙 while True: try: WebDriverWait(self.driver, 120, 1).until( lambda x: x.find_element_by_android_uiautomator( 'new UiSelector().text(\"%s\")' % self.device) ).is_displayed() except: self.loger.info('蓝牙扫描失败,重新扫描') self.screenshot('蓝牙扫描失败') try: WebDriverWait( self.driver, 60, 1).until(lambda x: x. find_element_by_android_uiautomator( "new UiSelector().text(\"重新扫描\")") ).is_displayed() except: self.loger.info('重新扫描按钮不可点击') else: self.loger.info('点击重新扫描按钮') self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"重新扫描\")").click() else: self.loger.info('蓝牙扫描成功') sleep(3) ele = self.driver.find_element_by_android_uiautomator( 'new UiSelector().text(\"%s\")' % self.device) sleep(1) ele.click() break # 进入配对界面,点击设备开关按键 try: WebDriverWait(self.driver, 60, 1).until( lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"配对\")")).is_displayed() except: self.loger.info('进入蓝牙配对界面失败') else: sleep(2) self.once_click() # 蓝牙连接成功,点击完成 try: WebDriverWait(self.driver, 120, 1).until( lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"完成\")")).is_displayed( ) except: self.loger.info('蓝牙配对失败') self.screenshot('蓝牙配对失败') # 校验串口日志 self.com_bluetooth_log_check(self.dataList) else: # 校验串口日志 check_result = self.com_bluetooth_log_check( self.dataList) if check_result: self.loger.info('串口蓝牙日志校验成功') # 点击完成 self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"完成\")").click() # 进入WiFi配置界面 try: # WebDriverWait(self.driver, 120, 1).until( # lambda x: x.find_element_by_android_uiautomator( # "new UiSelector().text(\"完成\")")).is_displayed() WebDriverWait(self.driver, 120, 1).until( lambda x: x. find_element_by_android_uiautomator( "new UiSelector().text(\"完成\")" )).is_clickable() except: self.loger.info('wifi设置页面加载失败') self.screenshot('wifi设置页面加载失败') k += 1 self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"跳过\")").click() sleep(2) self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"确认\")").click() #删除设备 self.delete_lempDevice() else: self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"完成\")").click() # 校验串口日志,等4秒 sleep(4) check_result_2 = self.com_wifi_log_check( self.dataList) # 判断设备是否配网成功 # try: # WebDriverWait(self.driver, 120, 1).until( # EC.element_to_be_clickable((By.ID, 'iv_switch'))) try: WebDriverWait(self.driver, 180, 0.1).until( EC.presence_of_element_located( (By.XPATH, '//*[contains(@text,"连接成功")]'))) except: self.loger.info('APP端Toast提示失败') self.loger.info('设备配网失败') continue else: self.loger.info('APP端Toast提示成功') add_result = True if check_result_2: self.loger.info('串口WiFi日志校验成功') com_check = True else: self.loger.info('串口WiFi日志校验失败') com_check = False if add_result or com_check: n += 1 self.loger.info('设备配网成功') else: self.loger.info('设备配网失败') # 删除设备 self.delete_lempDevice() else: self.loger.info('串口蓝牙日志校验失败') except Exception as e: print(e) continue self.start = False self.loger.info('[%s]' % self.device + '设备配网[%s]次,' % m + '成功了[%s]次,' % n + ',配网成功率%s' % (n / m * 100) + '%') self.loger.info('wifi设置页面加载失败次数:%s' % k) #删除设备 def delete_lempDevice(self): sleep(4) btn_settting = self.driver.find_element_by_id("btn_setting") sleep(1) btn_settting.click() sleep(2) #向下滑动 self.swip.swipe_up(600) self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"删除设备\")").click() sleep(1) yes = self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"是\")") yes.click() sleep(1) #刷新首页 self.swip.swipe_down(600) try: WebDriverWait( self.driver, 5, 1 ).until(lambda x: x.find_element_by_android_uiautomator( 'new UiSelector().text(\"%s\")' % self.device)).is_displayed() except: self.loger.info('删除设备成功') self.loger.info( '分割线--------------------------------------------------分割线') else: self.loger.info('删除设备失败') #发数据 def Send_data(self, data): self.main_engine2.write(data) #长按4秒 def long_click(self): #发送开 self.Send_data(bytes.fromhex('A0 01 01 A2')) sleep(4) #发送关 self.Send_data(bytes.fromhex('A0 01 00 A1')) #短按 def once_click(self): #发送开 self.Send_data(bytes.fromhex('A0 01 01 A2')) sleep(0.5) #发送关 self.Send_data(bytes.fromhex('A0 01 00 A1')) # 监测串口日志 def com_bluetooth_log_check(self, dataList): start = len(dataList) - 1000 end = len(dataList) str1 = dataList[start:end] str2 = str1.replace(" ", "").replace("\r", "").replace("\n", "") # 去掉空格、回车符、换行符 strlist = str2.split(':', 20) # 以冒号进行分割 # 打印串口日志 self.loger.info('蓝牙配对日志:' + str(strlist)) print('蓝牙配对日志:' + str(strlist)) sl = 'ble_msg_get_wifi_mac_addressmacstr' if str(strlist).find(sl) >= 0: print('本次蓝牙配对成功') return True else: print('本次蓝牙配对失败') return False # 档位切换日志检测 def com_log_check(self, dataList, sl, option_type): if option_type == 'on_off_switch': start = len(dataList) - 3400 elif option_type == 'leve_switch': start = len(dataList) - 1400 else: start = len(dataList) - 2200 end = len(dataList) str1 = dataList[start:end] str2 = str1.replace(" ", "").replace("\r", "").replace("\n", "") # 去掉空格、回车符、换行符 strlist = str2.split(':', 50) # 以冒号进行分割 # 打印串口日志 self.loger.info('本次操作后日志:' + str(strlist)) for i in range(len(strlist)): if strlist[i].find(sl) >= 0: print('串口日志:' + strlist[i]) self.loger.info('串口日志:' + strlist[i]) return strlist[i] # 监测串口日志 def com_wifi_log_check(self, dataList): start = len(dataList) - 8000 end = len(dataList) str1 = dataList[start:end] str2 = str1.replace(" ", "").replace("\r", "").replace("\n", "") # 去掉空格、回车符、换行符 strlist = str2.split(':', 80) # 以冒号进行分割 # 打印串口日志 self.loger.info('wifi联网日志:' + str(strlist)) print('wifi联网日志:' + str(strlist)) sl = 'IOT_BLE_WIFI_CONNECT_SUC' if str(strlist).find(sl) >= 0: print('本次WiFi连接成功') return True else: print('本次WiFi连接失败') return False # 接收串口数据 def recv(self): if self.start: print("开始接收数据:") else: print("测试完毕,停止接收数据:") while self.start: try: count = self.main_engine.inWaiting() # 获取串口缓冲区数据 if count != 0: data = self.main_engine.read( self.main_engine.in_waiting).decode( "gbk") # 读出串口数据,数据采用gbk编码 # print(time.strftime("%Y-%m-%d %H-%M-%S"), " --- recv --> ") # 打印一下子 time.sleep(0.1) # 延时0.1秒,免得CPU出问题 self.dataList += data except Exception as e: print("异常报错:", e) # 截图 def screenshot(self, msg): # 截图保存 now = time.strftime("%Y-%m-%d %H-%M-%S") image_name = LOG_DIR + '/' + msg + " " + now + ".png" self.driver.save_screenshot(image_name) # 进入设备详情页 def enter_device(self): try: WebDriverWait( self.driver, 120, 1 ).until(lambda x: x.find_element_by_android_uiautomator( 'new UiSelector().text(\"%s\")' % self.device)).is_displayed() except: self.loger.info('没有找到该设备,无法进入设备详情页') else: self.driver.find_element_by_android_uiautomator( 'new UiSelector().text(\"%s\")' % self.device).click() # 档位切换 def leve_switch(self, times): # 高档按钮:high_gear_container # 中档按钮:mid_gear_container # 低档按钮:low_gear_container # 进入设备详情页 self.enter_device() try: WebDriverWait(self.driver, 120, 1).until( EC.element_to_be_clickable((By.ID, 'iv_switch'))) except: self.loger.info('设备详情页加载失败') else: self.loger.info('设备详情页加载成功') sleep(5) for i in range(times): try: try: WebDriverWait(self.driver, 30, 1).until( EC.element_to_be_clickable( (By.ID, 'high_gear_container'))) except: print('高档位按钮不可点击') continue else: self.driver.find_element_by_id( 'high_gear_container').click() # 串口校验 sleep(1) result = self.com_log_check(self.dataList, 'AA1002000103', 'leve_switch') if len(result) > 0: self.loger.info('高档位切换成功') print('高档位切换成功') else: self.loger.info('高档位切换失败') print('高档位切换失败') sleep(3) try: WebDriverWait(self.driver, 30, 1).until( EC.element_to_be_clickable( (By.ID, 'mid_gear_container'))) except: print('中档位按钮不可点击') continue else: self.driver.find_element_by_id( 'mid_gear_container').click() sleep(1) result = self.com_log_check(self.dataList, 'AA1002000102', 'leve_switch') if len(result) > 0: self.loger.info('中档位切换成功') print('中档位切换成功') else: self.loger.info('中档位切换失败') print('中档位切换失败') sleep(3) try: WebDriverWait(self.driver, 30, 1).until( EC.element_to_be_clickable( (By.ID, 'low_gear_container'))) except: print('低档位按钮不可点击') continue else: self.driver.find_element_by_id( 'low_gear_container').click() sleep(1) result = self.com_log_check(self.dataList, 'AA1002000101', 'leve_switch') if len(result) > 0: self.loger.info('低档位切换成功') print('低档位切换成功') else: self.loger.info('低档位切换失败') print('低档位切换失败') sleep(3) # 摇头 self.rotate((i + 1) & 1) sleep(2) # 童锁 self.child_lock((i + 1) & 1) sleep(2) # 关闭设备 try: self.on_off_switch() sleep(3) # 开启设备 self.on_off_switch() sleep(3) except Exception as e: print(e) continue print('第%s次循环操作结束' % (i + 1)) self.loger.info('第%s次循环操作结束' % (i + 1)) except Exception as e: print(e) continue # 测试完毕,停止串口打印 self.start = False # 开关切换 def on_off_switch(self, times=None): # # 进入设备详情页 # self.enter_device() # try: # WebDriverWait(self.driver, 120, 1).until( # EC.element_to_be_clickable((By.ID, 'iv_switch'))) # except: # self.loger.info('设备详情页加载失败') # else: # self.loger.info('设备详情页加载成功') # for i in range(times): # 开关按钮:iv_switch try: WebDriverWait( self.driver, 5, 1).until(lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"高档\")")).is_displayed() except: print('设备当前状态是关,打开设备') self.loger.info('设备当前状态是关,打开设备') try: self.driver.find_element_by_id('iv_switch').click() sleep(3) result = self.com_log_check(self.dataList, 'AA1001000101', 'on_off_switch') if len(result) > 0: self.loger.info('设备开成功') print('设备开成功') sleep(3) else: self.loger.info('设备开失败') print('设备开失败') except Exception as e: print(e) # continue else: self.loger.info('设备当前状态是开,关闭设备') print('设备当前状态是开,关闭设备') try: self.driver.find_element_by_id('iv_switch').click() sleep(3) result = self.com_log_check(self.dataList, 'AA1001000100', 'on_off_switch') if len(result) > 0: self.loger.info('设备关成功') print('设备关成功') sleep(3) else: self.loger.info('设备关失败') print('设备关失败') except Exception as e: print(e) # continue # 摇头 def rotate(self, m): # 摇头:iv_rotate if m == 0: try: self.driver.find_element_by_id('iv_rotate').click() sleep(3) result = self.com_log_check(self.dataList, 'AA1003000100', 'rotate') if len(result) > 0: self.loger.info('设备关闭摇头成功') print('设备关闭摇头成功') sleep(3) else: self.loger.info('设备关闭摇头失败') print('设备关闭摇头失败') except Exception as e: print(e) else: try: self.driver.find_element_by_id('iv_rotate').click() sleep(3) result = self.com_log_check(self.dataList, 'AA1003000101', 'rotate') if len(result) > 0: self.loger.info('设备打开摇头成功') print('设备打开摇头成功') sleep(3) else: self.loger.info('设备打开摇头失败') print('设备打开摇头失败') except Exception as e: print(e) # 童锁 def child_lock(self, m): # 童锁按钮:iv_child_lock if m == 0: try: self.driver.find_element_by_id('iv_child_lock').click() sleep(3) result = self.com_log_check(self.dataList, 'AA1005000100', 'child_lock') if len(result) > 0: self.loger.info('设备关闭童锁成功') print('设备关闭童锁成功') sleep(3) else: self.loger.info('设备关闭童锁失败') print('设备关闭童锁失败') except Exception as e: print(e) else: try: self.driver.find_element_by_id('iv_child_lock').click() sleep(3) result = self.com_log_check(self.dataList, 'AA1005000101', 'child_lock') if len(result) > 0: self.loger.info('设备打开童锁成功') print('设备打开童锁成功') sleep(3) else: self.loger.info('设备打开童锁失败') print('设备打开童锁失败') except Exception as e: print(e) # # # if __name__ == '__main__': # platformVersion = '10' #安卓系统版本 # mobileName = 'R58M36MWZ1B' #手机名称 # device = 'H7130_C583' #设备名称 # times = 100 #测试次数 # Ret1 = False #日志打印串口是否创建成功标志 # Ret2 = False #控制串口是否创建成功标志 # com1 = 'com13' # bps = 115200 # timeout = 0.5 # com2 = "com12" # bps2 = 9600 # timex = 0.5 # bytesize = 8 # stopbits = 1 # ms = Humidifier2(platformVersion,mobileName,device,com1, bps, timeout,com2,bps2,bytesize,stopbits) # if(Ret1 and Ret2): # #功能线程运行 # # threading.Thread(target=ms.add_device,args=(times,)).start() # # threading.Thread(target=ms.leve_switch, args=(times,)).start() # threading.Thread(target=ms.on_off_switch, args=(times,)).start() # #日志线程运行 # threading.Thread(target=ms.recv,).start() # # ms.add_device(times) # else: # print("创建串口失败")
class H6075MainHandle(): dataList = '' def __init__(self, driver, device, com, bps, timeout, Ret1, Ret2=None, com2=None, bps2=None, bytesize=None, stopbits=None): self.device = device #日志 self.user_log = UserLog(self.device) self.loger = self.user_log.get_logger() self.driver = driver self.Ret1 = Ret1 self.Ret2 = Ret2 # 获取屏幕分辨率 self.screen_x = self.driver.get_window_size()['width'] # 屏幕宽度--单位是px self.screen_y = self.driver.get_window_size()['height'] # 屏幕的高度-单位是px self.swip = Swip(self.screen_x, self.screen_y, self.driver) #串口日志关键字 self.com_keyword = [ 'PT_PROTOCOL_MODE_SENCE_READ', 'PT_PROTOCOL_MODE_SENCE_RELAX', 'PT_PROTOCOL_MODE_SENCE_NIGHT', 'PT_PROTOCOL_MODE_SENCE_CANDLELIGHT', 'PT_PROTOCOL_MODE_SENCE_SHIMMER', 'PT_PROTOCOL_MODE_SENCE_BREATH', 'PT_PROTOCOL_MODE_SENCE_GLINT', 'PT_PROTOCOL_MODE_SENCE_BLINK' ] #模式 self.mode = ['阅读', '休闲', '夜灯', '烛光', '微光', '呼吸', '闪烁', '眨眼'] #打印串口 self.port = com self.bps = bps self.timeout = timeout self.start = True # global Ret1 try: # 打开串口,并得到串口对象 self.main_engine = serial.Serial(self.port, self.bps, timeout=self.timeout) self.main_engine.flushInput() # 清空缓冲区 # 判断是否打开成功 if (self.main_engine.is_open): self.Ret1 = True except Exception as e: print("---异常---:", e) # #控制串口 # self.port2 = com2 # self.bps2 = bps2 # self.bytesize = bytesize # self.stopbits = stopbits # # global Ret2 # try: # # 打开串口,并得到串口对象 # self.main_engine2= serial.Serial(self.port2,self.bps2,timeout=self.timeout,bytesize=self.bytesize,stopbits=self.stopbits) # # 判断是否打开成功 # if (self.main_engine2.is_open): # self.Ret2 = True # except Exception as e: # print("---异常---:", e) def connect_H6075(self, times): if (self.Ret1 and self.Ret2): #功能线程运行 threading.Thread(target=self.add_device, args=(times, )).start() # threading.Thread(target=self.leve_switch, args=(times,)).start() # threading.Thread(target=self.on_off_switch, args=(times,)).start() #日志线程运行 threading.Thread(target=self.recv, ).start() else: print("创建串口失败") # 选择要添加的设备类型 def chooes_device(self): sleep(4) # 点击+号 # ele = self.driver.find_element_by_xpath('//android.widget.ImageView[2]') ele = self.driver.find_element_by_id('iv_add') sleep(2) ele.click() sleep(3) self.loger.info('开始添加设备') # 点击搜索 WebDriverWait(self.driver, 30, 1).until(EC.element_to_be_clickable( (By.ID, 'tv_search'))).click() sleep(2) # 切换输入法,方便后续调用输入法的搜索按钮 self.driver.activate_ime_engine( 'com.sec.android.inputmethod/.SamsungKeypad') # 搜索框输入device_type self.driver.press_keycode(14) sleep(0.5) self.driver.press_keycode(8) sleep(0.5) self.driver.press_keycode(10) sleep(0.5) self.driver.press_keycode(7) WebDriverWait(self.driver, 30, 1).until(EC.element_to_be_clickable( (By.ID, 'sku_icon'))).click() #设备配网 def add_device(self, times): n = 0 #成功次数 m = 0 #配网执行次数 k = 0 #WiFi列表加载失败次数 app_check = False com_check = False for i in range(times): #选择设备类型 try: self.chooes_device() m += 1 #连接蓝牙 while True: try: WebDriverWait(self.driver, 120, 1).until( lambda x: x.find_element_by_android_uiautomator( 'new UiSelector().text(\"%s\")' % self.device) ).is_displayed() except: self.loger.info('蓝牙扫描失败,重新扫描') self.screenshot('蓝牙扫描失败') try: WebDriverWait( self.driver, 60, 1).until(lambda x: x. find_element_by_android_uiautomator( "new UiSelector().text(\"重新扫描\")") ).is_displayed() except: self.loger.info('重新扫描按钮不可点击') else: self.loger.info('点击重新扫描按钮') self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"重新扫描\")").click() else: self.loger.info('蓝牙扫描成功') sleep(3) ele = self.driver.find_element_by_android_uiautomator( 'new UiSelector().text(\"%s\")' % self.device) sleep(1) ele.click() break # 进入配对界面,点击设备开关按键 try: WebDriverWait(self.driver, 60, 1).until( lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"配对\")")).is_displayed() except: self.loger.info('进入蓝牙配对界面失败') else: sleep(2) self.once_click() # 蓝牙连接成功,点击完成 try: WebDriverWait(self.driver, 120, 1).until( lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"完成\")")).is_displayed( ) except: self.loger.info('蓝牙配对失败') self.screenshot('蓝牙配对失败') # 校验串口日志 self.com_bluetooth_log_check(self.dataList) else: # 校验串口日志 check_result = self.com_bluetooth_log_check( self.dataList) if check_result: self.loger.info('串口蓝牙日志校验成功') # 点击完成 self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"完成\")").click() # 进入WiFi配置界面 try: # WebDriverWait(self.driver, 120, 1).until( # lambda x: x.find_element_by_android_uiautomator( # "new UiSelector().text(\"完成\")")).is_displayed() WebDriverWait(self.driver, 120, 1).until( lambda x: x. find_element_by_android_uiautomator( "new UiSelector().text(\"完成\")" )).is_clickable() except: self.loger.info('wifi设置页面加载失败') self.screenshot('wifi设置页面加载失败') k += 1 self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"跳过\")").click() sleep(2) self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"确认\")").click() #删除设备 self.delete_lempDevice() else: self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"完成\")").click() # 校验串口日志,等4秒 sleep(4) check_result_2 = self.com_wifi_log_check( self.dataList) # 判断设备是否配网成功 # try: # WebDriverWait(self.driver, 120, 1).until( # EC.element_to_be_clickable((By.ID, 'iv_switch'))) try: WebDriverWait(self.driver, 180, 0.1).until( EC.presence_of_element_located( (By.XPATH, '//*[contains(@text,"连接成功")]'))) except: self.loger.info('APP端Toast提示失败') self.loger.info('设备配网失败') continue else: self.loger.info('APP端Toast提示成功') add_result = True if check_result_2: self.loger.info('串口WiFi日志校验成功') com_check = True else: self.loger.info('串口WiFi日志校验失败') com_check = False if add_result or com_check: n += 1 self.loger.info('设备配网成功') else: self.loger.info('设备配网失败') # 删除设备 self.delete_lempDevice() else: self.loger.info('串口蓝牙日志校验失败') except Exception as e: print(e) continue self.start = False self.loger.info('[%s]' % self.device + '设备配网[%s]次,' % m + '成功了[%s]次,' % n + ',配网成功率%s' % (n / m * 100) + '%') self.loger.info('wifi设置页面加载失败次数:%s' % k) #删除设备 def delete_lempDevice(self): sleep(4) btn_settting = self.driver.find_element_by_id("btn_setting") sleep(1) btn_settting.click() sleep(2) #向下滑动 self.swip.swipe_up(600) self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"删除设备\")").click() sleep(1) yes = self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"是\")") yes.click() sleep(1) #刷新首页 self.swip.swipe_down(600) try: WebDriverWait( self.driver, 5, 1 ).until(lambda x: x.find_element_by_android_uiautomator( 'new UiSelector().text(\"%s\")' % self.device)).is_displayed() except: self.loger.info('删除设备成功') self.loger.info( '分割线--------------------------------------------------分割线') else: self.loger.info('删除设备失败') # #发数据 # def Send_data(self,data): # self.main_engine2.write(data) # #长按4秒 # def long_click(self): # #发送开 # self.Send_data(bytes.fromhex('A0 01 01 A2')) # sleep(4) # #发送关 # self.Send_data(bytes.fromhex('A0 01 00 A1')) # # #短按 # def once_click(self): # #发送开 # self.Send_data(bytes.fromhex('A0 01 01 A2')) # sleep(0.5) # #发送关 # self.Send_data(bytes.fromhex('A0 01 00 A1')) # 监测串口日志 def com_bluetooth_log_check(self, dataList, sl): start = len(dataList) - 1000 end = len(dataList) str1 = dataList[start:end] str2 = str1.replace(" ", "").replace("\r", "").replace("\n", "") # 去掉空格、回车符、换行符 strlist = str2.split(':', 20) # 以冒号进行分割 # 打印串口日志 self.loger.info('蓝牙配对日志:' + str(strlist)) print('蓝牙配对日志:' + str(strlist)) if str(strlist).find(sl) >= 0: print('本次蓝牙配对成功') return True else: print('本次蓝牙配对失败') return False # 档位切换日志检测 def com_log_check(self, dataList, sl, option_type=None): if option_type != None: if option_type == 'on_off_switch': start = len(dataList) - 3400 elif option_type == 'leve_switch': start = len(dataList) - 1400 else: start = len(dataList) - 2200 else: start = len(dataList) - 3000 end = len(dataList) str1 = dataList[start:end] str2 = str1.replace(" ", "").replace("\r", "").replace("\n", "") # 去掉空格、回车符、换行符 strlist = str2.split(':', 50) # 以冒号进行分割 # 打印串口日志 self.loger.info('本次操作后日志:' + str(strlist)) for i in range(len(strlist)): if strlist[i].find(sl) >= 0: print('串口日志:' + strlist[i]) self.loger.info('串口日志:' + strlist[i]) return True # 监测串口日志 def com_wifi_log_check(self, dataList): start = len(dataList) - 8000 end = len(dataList) str1 = dataList[start:end] str2 = str1.replace(" ", "").replace("\r", "").replace("\n", "") # 去掉空格、回车符、换行符 strlist = str2.split(':', 80) # 以冒号进行分割 # 打印串口日志 self.loger.info('wifi联网日志:' + str(strlist)) print('wifi联网日志:' + str(strlist)) sl = 'IOT_BLE_WIFI_CONNECT_SUC' if str(strlist).find(sl) >= 0: print('本次WiFi连接成功') return True else: print('本次WiFi连接失败') return False # 接收串口数据 def recv(self): if self.start: print("开始接收数据:") else: print("测试完毕,停止接收数据:") while self.start: try: count = self.main_engine.inWaiting() # 获取串口缓冲区数据 if count != 0: data = self.main_engine.read( self.main_engine.in_waiting).decode( "gbk") # 读出串口数据,数据采用gbk编码 # print(time.strftime("%Y-%m-%d %H-%M-%S"), " --- recv --> ") # 打印一下子 time.sleep(0.1) # 延时0.1秒,免得CPU出问题 self.dataList += data except Exception as e: print("异常报错:", e) # 截图 def screenshot(self, msg): # 截图保存 now = time.strftime("%Y-%m-%d %H-%M-%S") image_name = LOG_DIR + '/' + msg + " " + now + ".png" self.driver.save_screenshot(image_name) #模式切换 def mode_switch(self, times): if (self.Ret1): #功能线程运行 threading.Thread(target=self.mode_switch_handle, args=(times, )).start() #日志线程运行 threading.Thread(target=self.recv, ).start() else: print("创建串口失败") # 进入设备详情页 def enter_device(self): try: WebDriverWait( self.driver, 120, 1 ).until(lambda x: x.find_element_by_android_uiautomator( 'new UiSelector().text(\"%s\")' % self.device)).is_displayed() except: self.loger.info('没有找到该设备,无法进入设备详情页') else: self.driver.find_element_by_android_uiautomator( 'new UiSelector().text(\"%s\")' % self.device).click() # 模式切换 def mode_switch_handle(self, times): #进入设备详情页 self.enter_device() n = 0 try: WebDriverWait( self.driver, 120, 1).until(lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"亮度\")")).is_displayed() except: print('设备详情页加载失败') else: for i in range(times): for i in range(8): # 开始模式循环操作 self.driver.find_element_by_xpath( '//*[@resource-id="com.govee.home:id/scenes_list"]/android.widget.RelativeLayout[%s]' % (i + 1) + '/android.widget.ImageView[1]').click() if self.com_keyword[i] == 'PT_PROTOCOL_MODE_SENCE_READ': sleep(3) else: sleep(1) # 监测串口日志 check_result = self.com_log_check(self.dataList, self.com_keyword[i]) if check_result == True: n += 1 print(self.mode[i] + '切换成功') self.loger.info(self.mode[i] + '切换成功') try: WebDriverWait(self.driver, 60, 0.2).until( EC.element_to_be_clickable(( By.XPATH, '//*[@resource-id="com.govee.home:id/scenes_list"]/android.widget.RelativeLayout[%s]' % (i + 1) + '/android.widget.ImageView[1]'))) except: self.loger.info(self.mode[i] + '切换后设备断开连接') self.screenshot(self.mode[i] + '切换后设备断开连接') self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"重新连接\")").click() else: print(self.mode[i] + '切换后设备连接正常') self.loger.info(self.mode[i] + '切换后设备连接正常') else: print(self.mode[i] + '切换失败') self.loger.info(self.mode[i] + '切换失败') sleep(3) self.start = False self.loger.info('模式切换[%s]次' % times + ',成功了[%s]次!!!!!!!!' % n)
class DeviceConnectHandle(): def __init__(self, driver, screen_x, screen_y, loger): global list_diviceType_51, list_diviceMac_51 self.driver = driver self.myDevice_p = MyDevicePage(driver) self.cameraSet_p = CameraSetPage(driver) self.lampSet_p = LampSetPage(driver) self.calibration = CalibrationPage(driver) self.lampstandard_p = LampstandardPage(driver) self.loger = loger # self.press_keycode_handle = PressKeyCodeHandle(driver) self.search_scan_handle = Search_Sacn_Device_Handle(driver, loger) self.wifi_set_handle = WIFISetHandle(driver, screen_x, screen_y, loger) self.swip = Swip(screen_x, screen_y, driver) #连接H5183烤肉温度计 def connect_H5183(self): self.search_scan_handle.chooes_device('H5183') #连接H5102温湿度计 def coonect_H5102(self): self.search_scan_handle.chooes_device('H5102') sleep(2) #连接H6056,灯柱 def connect_sku6056(self): # 添加前判断是设备已经添加 sleep(3) ele = self.myDevice_p.get_tvLamp_element() self.loger.info('已添加H6056,删除掉') if ele != None: sleep(1) # 删除设备 ele.click() sleep(1) self.delete_lempDevice() # 搜索6056设备型号 self.loger.info('开始添加H6056') self.search_scan_handle.chooes_device('H6056') self.search_scan_handle.scan_device('H6056_3142') sleep(8) #设置WIFI # self.wifi_set_handle.set_wifi() # WIFI设置完成点击完成 ele = WebDriverWait( self.driver, 30, 0.5).until(lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"完成\")")) if ele: ele.click() # else: # self.loger.info('WIFI连接失败') #灯柱位置设置 if WebDriverWait( self.driver, 40, 0.5).until(lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"灯柱位置\")")): self.loger.info('WIFI连接成功') ele = self.lampstandard_p.get_iv_switch_1_element() sleep(1) ele.click() sleep(1) ele.click() ele2 = self.lampstandard_p.get_iv_switch_2_element() sleep(1) ele2.click() sleep(1) ele2.click() ele3 = self.lampstandard_p.get_tv_swap_element() sleep(1) ele3.click() sleep(1) ele4 = self.lampstandard_p.get_tv_done_element() sleep(1) ele4.click() if WebDriverWait( self.driver, 40, 0.5).until(lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"H6056_3142\")")): # self.delete_lempDevice() return True else: return False else: self.loger.info('WIFI连接失败') # 连接H6054灯柱 def connect_sku6054(self): self.loger.info('开始添加H6054') # 搜索6199设备型号 self.search_scan_handle.chooes_device('H6054') # 扫描设备 self.search_scan_handle.scan_device('H6054_3142') sleep(8) # 灯柱位置设置 if WebDriverWait( self.driver, 40, 0.5).until(lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"灯柱位置\")")): # self.loger.info('WIFI连接成功') ele = self.lampstandard_p.get_iv_switch_1_element() sleep(1) ele.click() sleep(1) ele.click() ele2 = self.lampstandard_p.get_iv_switch_2_element() sleep(1) ele2.click() sleep(1) ele2.click() ele3 = self.lampstandard_p.get_tv_swap_element() sleep(1) ele3.click() sleep(1) ele4 = self.lampstandard_p.get_tv_done_element() sleep(1) ele4.click() # 设置WIFI # self.wifi_set_handle.set_wifi() if WebDriverWait( self.driver, 30, 1).until(lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"完成\")")).is_displayed(): self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"完成\")").click() WebDriverWait( self.driver, 30, 1).until(lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"准备好了\")")).click() sleep(2) # 摄像头安装位置,默认上方 self.cameraSet('up') # 操作示意图 sleep(1) self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"知道了\")").click() # WebDriverWait(self.driver, 60, 0.5).until(EC.visibility_of_element_located((By.ID,'pic_show'))) # 判断图片是否出现 sleep(2) self.refresh_picture() else: self.loger.info('灯柱位置加载失败') # 连接H6199TV灯带 def connect_H6199(self, device_name): sleep(6) self.loger.info('开始添加H6199') #搜索6199设备型号 self.search_scan_handle.chooes_device('H6199') #扫描设备 self.search_scan_handle.scan_device(device_name) sleep(3) #设置WIFI # self.wifi_set_handle.set_wifi() try: WebDriverWait( self.driver, 120, 1).until(lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"完成\")")).is_displayed() except: self.loger.info('检测摄像头第一次失败') self.load_fail('检测摄像头第一次失败') #重试 sleep(1) ele = self.driver.find_element_by_id('iv_choose') sleep(1) ele.click() sleep(1) self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"重试\")").click() try: WebDriverWait( self.driver, 120, 1).until(lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"完成\")")).is_displayed() except: self.loger.info('检测摄像头第二次失败,本次添加结束') self.load_fail('检测摄像头第二次失败') self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"跳过\")").click() self.delete_lempDevice() return False else: #继续下面操作 result = self.continue_connect(device_name) return result else: # 继续下面操作 result = self.continue_connect(device_name) return result #摄像头检测成功后,继续下面操作 def continue_connect(self, device_name): self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"完成\")").click() self.loger.info('检测摄像头成功,进入WIFI设置页成功') #校准设备 if self.device_check(): #摄像头安装位置,默认上方 self.cameraSet('up') #灯带安装方向,默认顺时针 self.lampSet() #操作示意图 sleep(2) self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"知道了\")").click() # WebDriverWait(self.driver, 60, 0.5).until(EC.visibility_of_element_located((By.ID,'pic_show'))) #判断图片是否出现 sleep(2) #如果图片加载成功 if self.check_picture(): #校准:图片位置[58, 356][1382, 1101] sleep(4) self.jiaozhun() self.loger.info('校准') sleep(4) #下一步 # WebDriverWait(self.driver, 30, 1).until(EC.element_to_be_clickable((By.ID, 'pic_calibration_done'))) self.driver.find_element_by_id('pic_calibration_done').click() self.loger.info('下一步') # WebDriverWait(self.driver, 20, 1).until(EC.element_to_be_clickable((By.ID, 'pic_calibration_done'))).click() # 提交 sleep(3) # self.driver.find_element_by_android_uiautomator("new UiSelector().text(\"提交\")").click() # WebDriverWait(self.driver, 40, 1).until(EC.element_to_be_clickable((By.ID, 'pic_calibration_done'))) self.driver.find_element_by_id('pic_calibration_done').click() self.loger.info('提交') if WebDriverWait(self.driver, 40, 1).until( lambda x: x.find_element_by_android_uiautomator( 'new UiSelector().text(\"%s\")' % device_name) ).is_displayed(): #删除设备 self.delete_lempDevice() self.loger.info('删除设备') return True else: return False else: self.loger.info('图片无法加载出来,本次测试结束') back = self.driver.find_element_by_id('back') sleep(1) back.click() sleep(2) # 刷新首页 self.swip.swipe_down(600) WebDriverWait( self.driver, 40, 1).until(lambda x: x.find_element_by_android_uiautomator( 'new UiSelector().text(\"%s\")' % device_name)).click( ) #删除设备 self.delete_lempDevice() return False else: return False #删除设备 def delete_lempDevice(self): sleep(4) btn_settting = self.driver.find_element_by_id("btn_setting") sleep(1) btn_settting.click() sleep(1) self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"删除设备\")").click() sleep(1) yes = self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"是\")") yes.click() sleep(1) #刷新首页 self.swip.swipe_down(600) self.driver.implicitly_wait(5) def refresh_picture(self): n = 0 for i in range(100): if WebDriverWait(self.driver, 60, 1).until( EC.element_to_be_clickable((By.ID, 'pic_show'))): # ele2 = self.driver.find_element_by_android_uiautomator("new UiSelector().text(\"刷新\")") n += 1 print('第[%s]次图片加载成功' % n) self.loger.info('第' + str(n) + '次图片加载成功') self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"刷新\")").click() else: print('第[%s]次图片加载失败' % n) self.loger.info('第' + str(n) + '次图片加载失败') self.load_fail('第' + str(n) + '次图片加载失败') self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"刷新\")").click() #检查图片 def check_picture(self): try: # WebDriverWait(self.driver, 120, 1).until(EC.element_to_be_clickable((By.ID,'pic_calibration_done'))) WebDriverWait(self.driver, 120, 1).until( EC.element_to_be_clickable((By.ID, 'pic_show'))) except: print('第一次图片加载失败') self.loger.info('第一次图片加载失败') self.load_fail('第一次图片加载失败') #再次点击刷新 self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"刷新\")").click() try: WebDriverWait(self.driver, 120, 1).until( EC.element_to_be_clickable((By.ID, 'pic_show'))) except: self.loger.info('第二次图片加载失败') self.load_fail('第二次图片加载失败') return False else: self.loger.info('第二次图片加载成功') return True else: self.loger.info('第一次图片加载成功') sleep(2) return True #加载失败时截图 def load_fail(self, msg): # 截图保存 now = time.strftime("%Y-%m-%d %H-%M-%S") image_name = setting.TEST_ERROR_Image + '/' + msg + " " + now + ".png" self.driver.save_screenshot(image_name) #校准滑动坐标点 def jiaozhun(self): #左一 self.swip.swipe_up(70, 58, 500) #左二 self.swip.swipe_down(90, 400, 1000) #左四 self.swip.swipe_up(80, 1000, 1100) #左五 self.swip.swipe_down(90, 1400, 800) sleep(2) # toast弹窗检查-wifi连接 def check_toast(self): # 用于生成xpath定位 相当于 "//*[@text='没有找到用户名或密码']" toast_message = "设备Wi-Fi连接成功" message = '//*[@text=\'{}\']'.format(toast_message) # 获取toast提示框内容 toast_element = WebDriverWait( self.driver, 30, 0.5).until(lambda x: x.find_element_by_xpath(message)) print(toast_element.text) # 添加断言 assert toast_element.text == "设备Wi-Fi连接成功" self.loger.info(toast_element.text) #灯带安装方向,默认顺时针 def lampSet(self, direction=None): self.driver.implicitly_wait(5) if direction == None: direction = 'right' if direction == 'right': lamp_right = self.lampSet_p.get_lampRight_element() sleep(1) lamp_right.click() else: lamp_left = self.lampSet_p.get_lampLeft_element() sleep(1) lamp_left.click() # 点击确认 sleep(2) ele = self.lampSet_p.get_done_element() sleep(1) ele.click() sleep(2) self.loger.info('灯带方向设置成功') #摄像头安装位置,默认上方 def cameraSet(self, direction=None): self.driver.implicitly_wait(5) if direction == None: direction = 'up' if direction == 'up': camera_up = self.cameraSet_p.get_cameraUp_element() sleep(1) camera_up.click() else: camera_down = self.cameraSet_p.get_cameraDown_element() sleep(1) camera_down.click() #点击确认 sleep(2) ele = self.cameraSet_p.get_done_element() sleep(1) ele.click() sleep(2) self.loger.info('摄像头安装位置设置成功') #校准设备 def device_check(self): try: WebDriverWait( self.driver, 120, 1).until(lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"准备好了\")")).is_displayed() except: self.loger.info('WIFI第一次连接失败') #重新连接 self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"完成\")").click() try: WebDriverWait( self.driver, 120, 1).until(lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"准备好了\")")).is_displayed() except: self.loger.info('WIFI第二次连接失败,结束本次配网') # 跳过 self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"跳过\")").click() sleep(3) #确认 self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"确认\")").click() self.delete_lempDevice() return False else: # 检查WIFI是否连接成功 self.loger.info('WIFI连接成功') WebDriverWait( self.driver, 20, 1).until(lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"准备好了\")")).click() return True else: # 检查WIFI是否连接成功 self.loger.info('WIFI连接成功') WebDriverWait( self.driver, 20, 1).until(lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"准备好了\")")).click() return True # 连接H5182烤肉温度计 def connect_H5182(self): self.search_scan_handle.chooes_device('H5182') sleep(2) #操作已确认 self.myDevice_p.get_addClick().click() #查看扫描结果 WebDriverWait( self.driver, 20, 0.5).until(lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"H5182_2D1B\")")).click() # 显示等待 ele = WebDriverWait( self.driver, 20, 0.5).until(lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"完成\")")) if ele != None: self.loger.info("连接成功") ele.click() else: self.loger.info("连接失败") print("-----------------点击完成-------------------") sleep(5) #self.delete_H5182() #删除H5182烤肉温度计 def delete_H5182(self): sleep(8) ele = self.myDevice_p.get_meat_thermometer() ele.click() #进入设置界面 sleep(8) btn_settting = self.driver.find_element_by_id("btn_setting") sleep(2) btn_settting.click() #滑到底端 sleep(4) self.swip.swipe_up(800) sleep(2) self.swip.swipe_up(800) sleep(2) ele4 = self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"删除设备\")") ele4.click() sleep(2) yes = self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"是\")") yes.click() sleep(4) #连接H5103温湿度计 def connect_H5103(self): self.search_scan_handle.chooes_device('H5103') sleep(2)
class Thermo_HygrometerBoundHandle(): def __init__(self,driver,loger): self.driver = driver self.loger = loger # 获取屏幕分辨率 self.screen_x = self.driver.get_window_size()['width'] # 屏幕宽度--单位是px self.screen_y = self.driver.get_window_size()['height'] # 屏幕的高度-单位是px self.swip = Swip(self.screen_x, self.screen_y, self.driver) # 截图路径 self.image_path = setting.TEST_ERROR_Image #测试结果 self.report = setting.TEST_REPORT #设备配网 def bound_thermo_hygrometer(self,device,times,thermo_hygrometer_list): #功能线程运行 threading.Thread(target=self.bound,name= 'bound', args=(device,times,thermo_hygrometer_list)).start() # 进入设备详情页 def enter_device(self,device): try: WebDriverWait(self.driver, 120, 1).until( lambda x: x.find_element_by_android_uiautomator( 'new UiSelector().text(\"%s\")' % device)).is_displayed() except: self.loger.info('没有找到该设备,无法进入设备详情页') else: self.driver.find_element_by_android_uiautomator('new UiSelector().text(\"%s\")' % device).click() #绑定温湿度计 def bound(self,device,times,thermo_hygrometer_list): #开始循环绑定 if len(thermo_hygrometer_list) != 0: # for d in range(len(self.device)): # 进入设备详情页 self.enter_device(device) try: WebDriverWait(self.driver, 30, 0.5).until( EC.presence_of_element_located((By.ID, 'iv_switch'))) except: self.loger.info('设备详情页加载失败') print('设备详情页加载失败') else: self.loger.info('设备详情页加载成功') print('设备详情页加载成功') sleep(2) self.swip.swipe_up(600) sleep(2) self.swip.swipe_up(600) try: WebDriverWait(self.driver, 3, 0.5).until( lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"温度单位""\")")).is_displayed() except: # 绑定温湿度计 self.bound_handle(device,times,thermo_hygrometer_list) else: self.loger.info('已经添加过了温湿度计,删除掉') print('已经添加过了温湿度计,删除掉') # 删除温湿度计设备 self.unbound() #绑定温湿度计 self.bound_handle(device,times,thermo_hygrometer_list) # 退出详情页 WebDriverWait(self.driver, 5, 0.1).until( EC.presence_of_element_located((By.ID, 'btn_back'))).click() else: print('温湿度计待测设备为空') def bound_handle(self,device,times,thermo_hygrometer_list): now = time.strftime("%Y-%m-%d %H-%M-%S") # 文件路径 path = os.path.join(self.report, device + "温湿度计绑定兼容性测试" + now + ".xls") # 创建表格 self.creat_excel(path, device, times, thermo_hygrometer_list) #开始绑定温湿度计主循环 for t in range(len(thermo_hygrometer_list)): thermo_hygrometer = thermo_hygrometer_list[t] f = 0 p = 0 wrt_data = [] wrt_data.append(thermo_hygrometer_list[t]) for i in range(times): try: # 开始操作---添加 sleep(1) self.driver.find_element_by_android_uiautomator( 'new UiSelector().text(\"绑定温湿度计\")').click() # 等待 try: WebDriverWait(self.driver, 20, 0.5).until( lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"%s\")" % thermo_hygrometer)).is_displayed() except: self.swip.swipe_up(600) try: WebDriverWait(self.driver, 20, 0.5).until( lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"%s\")" % thermo_hygrometer)).is_displayed() except: self.loger.info('没有找到这个温湿度计') print('没有找到这个温湿度计') else: # 勾选 self.driver.find_element_by_android_uiautomator( 'new UiSelector().text(\"%s\")' % thermo_hygrometer).click() # 确定 WebDriverWait(self.driver, 10, 1).until( EC.presence_of_element_located((By.ID, 'btn_ok'))).click() time_start = time.time() # 开始计时 # 上滑 sleep(2) self.swip.swipe_up(600) try: WebDriverWait(self.driver, 300, 0.5).until_not( EC.presence_of_element_located((By.XPATH, '//*[contains(@text,"连接中")]'))) except: print('5分钟还一直连接中') self.screenshot('5分钟还一直连接中') else: try: WebDriverWait(self.driver, 3, 0.5).until( lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"温度单位\")")).is_displayed() except: f += 1 self.loger.info('绑定失败') self.screenshot('绑定失败') else: time_end = time.time() # 结束计时 sum_t = time_end - time_start # 运行所花时间 wrt_data.append('{:.2f}'.format(sum_t)) p += 1 print('温湿度计[%s]第[%s]次添加成功,耗时[%s]秒' % (thermo_hygrometer, p, '{:.2f}'.format(sum_t))) self.loger.info( '温湿度计[%s]第[%s]次添加成功,耗时[%s]秒' % (thermo_hygrometer, p, '{:.2f}'.format(sum_t))) # 删除温湿度计设备 self.unbound() else: # 勾选 self.driver.find_element_by_android_uiautomator( 'new UiSelector().text(\"%s\")' % thermo_hygrometer).click() # 确定 WebDriverWait(self.driver, 10, 1).until( EC.presence_of_element_located((By.ID, 'btn_ok'))).click() time_start = time.time() # 开始计时 # 上滑 sleep(2) self.swip.swipe_up(600) try: WebDriverWait(self.driver, 300, 0.5).until_not( EC.presence_of_element_located((By.XPATH, '//*[contains(@text,"连接中")]'))) except: print('5分钟还一直连接中') self.screenshot('5分钟还一直连接中') else: try: WebDriverWait(self.driver, 3, 0.5).until( lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"温度单位\")")).is_displayed() except: f += 1 self.loger.info('绑定失败') self.screenshot('绑定失败') else: time_end = time.time() # 结束计时 sum_t = time_end - time_start # 运行所花时间 wrt_data.append('{:.2f}'.format(sum_t)) p += 1 print('温湿度计[%s]第[%s]次添加成功,耗时[%s]秒' % (thermo_hygrometer, p, '{:.2f}'.format(sum_t))) self.loger.info( '温湿度计[%s]第[%s]次添加成功,耗时[%s]秒' % (thermo_hygrometer, p, '{:.2f}'.format(sum_t))) # 删除温湿度计设备 self.unbound() except Exception as e: print(e) continue print('温湿度计[%s]总共绑定[%s]次,成功[%s]次,失败[%s]次' % (thermo_hygrometer, times, p, f)) self.loger.info('温湿度计[%s]总共绑定[%s]次,成功[%s]次,失败[%s]次' % (thermo_hygrometer, times, p, f)) # 写入数据 self.excel(path, device, t, wrt_data) # 删除温湿度计 def unbound(self): try: WebDriverWait(self.driver, 10, 0.5).until( EC.presence_of_element_located((By.ID, 'tv_bind_temperature_title'))).click() WebDriverWait(self.driver, 10, 0.5).until( EC.presence_of_element_located((By.ID, 'btn_delete'))).click() WebDriverWait(self.driver, 10, 0.5).until( EC.presence_of_element_located((By.ID, 'btn_done'))).click() except Exception as e: print(e) #创建表格 def creat_excel(self,path,deviceName,times,thermo_hygrometer_list): # 创建空表格 columns = [] columns.append('温湿度计设备') for i in range(times): columns.append('第' + str(i + 1) + '次绑定') df = pd.DataFrame(data=np.zeros(shape=(len(thermo_hygrometer_list), (times + 1))), columns=columns) # 保存数据 # for d in range(len(self.device)): df.to_excel(path, sheet_name=deviceName, index=False, header=True) # 写入数据 def excel(self,path,deviceName,row,wrt_data): data = pd.read_excel(path, sheet_name=0, # 读取哪⼀个Excel中⼯作表,默认第⼀个 header=0, ) # 使⽤第⼀⾏数据作为列索引 data.loc[row] = wrt_data pd.DataFrame(data).to_excel(path, sheet_name=deviceName, index=False, header=True) # 截图 def screenshot(self, msg): # 截图保存 now = time.strftime("%Y-%m-%d %H-%M-%S") image_name = self.image_path + '/' + msg + " " + now + ".png" self.driver.save_screenshot(image_name)
class H6076MainHandle(): dataList = '' def __init__(self, driver, device, com, bps, timeout, com2, bps2, bytesize, stopbits, Ret1, Ret2, loger): self.device = device self.driver = driver self.Ret1 = Ret1 self.Ret2 = Ret2 #日志 self.loger = loger # 获取屏幕分辨率 self.screen_x = self.driver.get_window_size()['width'] # 屏幕宽度--单位是px self.screen_y = self.driver.get_window_size()['height'] # 屏幕的高度-单位是px self.swip = Swip(self.screen_x, self.screen_y, self.driver) #打印串口 self.port = com self.bps = bps self.timeout = timeout self.start = True #色温 self.color_temp = [0.05, 0.15, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8] #亮度 self.brightness = [0.2, 0.4, 0.5, 0.6, 0.7, 0.75, 0.8, 0.9, 0.95] #灵敏度 self.sensitivity = [0.2, 0.4, 0.5, 0.6, 0.7, 0.75, 0.8, 0.9, 0.95] # global Ret1 try: # 打开串口,并得到串口对象 self.main_engine = serial.Serial(self.port, self.bps, timeout=self.timeout) self.main_engine.flushInput() # 清空缓冲区 # 判断是否打开成功 if (self.main_engine.is_open): self.Ret1 = True except Exception as e: print("---异常---:", e) #控制串口 self.port2 = com2 self.bps2 = bps2 self.bytesize = bytesize self.stopbits = stopbits # global Ret2 try: # 打开串口,并得到串口对象 self.main_engine2 = serial.Serial(self.port2, self.bps2, timeout=self.timeout, bytesize=self.bytesize, stopbits=self.stopbits) # 判断是否打开成功 if (self.main_engine2.is_open): self.Ret2 = True #初始化继电器类 self.relay_h = RelayHandle(self.main_engine2) except Exception as e: print("---异常---:", e) #设备配网 def connect_H6076(self, skip_wifi, times): if (self.Ret1 and self.Ret2): #功能线程运行 threading.Thread(target=self.add_device, args=( skip_wifi, times, )).start() #日志线程运行 threading.Thread(target=self.recv, ).start() else: print("创建串口失败") #模式切换 def mode_switch(self, times): if (self.Ret1 and self.Ret2): #功能线程运行 threading.Thread(target=self.mode_switch_handle, args=(times, )).start() #日志线程运行 threading.Thread(target=self.recv, ).start() else: print("创建串口失败") #开关切换 def on_off_switch(self, times=None): if (self.Ret1 and self.Ret2): #功能线程运行 threading.Thread(target=self.on_off_switch_handle, args=(times, )).start() #日志线程运行 threading.Thread(target=self.recv, ).start() else: print("创建串口失败") #颜色切换 def color_switch(self, times): if (self.Ret1 and self.Ret2): #功能线程运行 threading.Thread(target=self.color_switch_handle, args=(times, )).start() #日志线程运行 threading.Thread(target=self.recv, ).start() else: print("创建串口失败") #亮度切换 def brightness_switch(self, times): if (self.Ret1 and self.Ret2): #功能线程运行 threading.Thread(target=self.brightness_switch_handle, args=(times, )).start() #日志线程运行 threading.Thread(target=self.recv, ).start() else: print("创建串口失败") #灵敏度切换 def sensitivity_switch(self, times): if (self.Ret1 and self.Ret2): #功能线程运行 threading.Thread(target=self.sensitivity_switch_handle, args=(times, )).start() #日志线程运行 threading.Thread(target=self.recv, ).start() else: print("创建串口失败") # 进入设备详情页 def enter_device(self): try: WebDriverWait( self.driver, 120, 1 ).until(lambda x: x.find_element_by_android_uiautomator( 'new UiSelector().text(\"%s\")' % self.device)).is_displayed() except: self.loger.info('没有找到该设备,无法进入设备详情页') else: sleep(2) self.driver.find_element_by_android_uiautomator( 'new UiSelector().text(\"%s\")' % self.device).click() #反复进入设备详情页 def enter_out_device_handle(self, times=None): n = 0 sleep(3) if times != None: for i in range(times): # 开关按钮:iv_switch sleep(3) try: #进入设备主页 self.enter_device() try: WebDriverWait(self.driver, 60, 1).until( EC.element_to_be_clickable((By.ID, 'btn_switch'))) except: self.loger.info('设备详情页加载失败[%s]' % (i + 1)) print('设备详情页加载失败[%s]' % (i + 1)) else: n += 1 print('设备详情页加载成功[%s]' % (i + 1)) self.loger.info('设备详情页加载成功[%s]' % (i + 1)) sleep(3) #退出 self.driver.find_element_by_id('btn_back').click() except Exception as e: print(e) continue self.loger.info('反复进入设备详情页[%s]次' % times + ',成功[%s]次' % n + ',成功率%s' % (n / times * 100) + '%') print('反复进入设备详情页[%s]次' % times + ',成功[%s]次' % n + ',成功率%s' % (n / times * 100) + '%') print('测试结束') self.start = False else: print('请输入times测试次数') # 开关切换 def on_off_switch_handle(self, times=None): #进入设备详情页 self.enter_device() n = 0 m = 0 sleep(3) if times != None: for i in range(times): # 开关按钮:iv_switch try: WebDriverWait( self.driver, 5, 1 ).until(lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"设备已关闭\")")).is_displayed() except: self.loger.info('设备当前状态是开,关闭设备') print('设备当前状态是开,关闭设备') try: sleep(2) self.driver.find_element_by_id('btn_switch').click() sleep(1) #蓝牙模式 # result = self.com_bluetooth_normal_log_check(self.dataList,2500,'on_off_switch','330100') #WiFi模式 result = self.com_bluetooth_normal_log_check( self.dataList, 490, 'on_off_switch', 'c10d0000') if result: n += 1 self.loger.info('设备关成功[%s]次' % n) print('设备关成功[%s]次' % n) sleep(2) else: self.loger.info('设备关失败') print('设备关失败') sleep(2) except Exception as e: print(e) continue else: print('设备当前状态是关,打开设备') self.loger.info('设备当前状态是关,打开设备') try: sleep(2) self.driver.find_element_by_id('btn_switch').click() sleep(1) # 蓝牙模式 # result = self.com_bluetooth_normal_log_check(self.dataList,2500,'on_off_switch','330100') # WiFi模式 result = self.com_bluetooth_normal_log_check( self.dataList, 490, 'on_off_switch', 'c10d0001') if result: m += 1 self.loger.info('设备开成功[%s]次' % m) print('设备开成功[%s]次' % m) sleep(2) else: self.loger.info('设备开失败') print('设备开失败') sleep(2) except Exception as e: print(e) continue self.loger.info('设备开%s次' % format(times / 2, '.0f') + ',成功%s次' % n + ',成功率%s' % format(n / (times / 2) * 100, '.2f') + '%') print('设备开%s次' % format(times / 2, '.0f') + ',成功%s次' % n + ',成功率%s' % format(n / (times / 2) * 100, '.2f') + '%') self.loger.info('设备关%s次' % format(times / 2, '.0f') + ',成功%s次' % n + ',成功率%s' % format(n / (times / 2) * 100, '.2f') + '%') print('设备关%s次' % format(times / 2, '.0f') + ',成功%s次' % n + ',成功率%s' % format(n / (times / 2) * 100, '.2f') + '%') print('测试结束') self.start = False else: print('请输入times测试次数') #颜色切换 def color_switch_handle(self, times): # 进入设备详情页 self.enter_device() n = 0 m = 0 q = 0 p = 0 try: WebDriverWait( self.driver, 60, 1).until(lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"场景\")")).is_displayed() except: print('设备详情页加载失败') else: #切换到颜色模式 sleep(3) try: self.driver.find_element_by_id('mode_2_icon').click() except Exception as e: print(e) sleep(3) # 上滑到颜色 self.swip.swipe_up(800) sleep(2) self.swip.swipe_up(800) #色温滑动条 ele = self.driver.find_element_by_id('color_tem_seekbar') # 点击全选 self.driver.find_element_by_id('select_all').click() sleep(1) #点击取消全选 self.driver.find_element_by_id('select_all').click() sleep(1) if times != None: for i in range(times): for j in range(7): #选择颜色分段 self.driver.find_element_by_id('checkbox_bulb%s' % (j + 1)).click() sleep(1) #点击颜色 self.driver.find_element_by_id('color_%s' % (j + 1)).click() sleep(1) #校验串口日志 check_com_result = self.com_bluetooth_normal_log_check( self.dataList, 420, 'color_switch', '330500') if check_com_result: n += 1 self.loger.info('第[%s]次颜色切换成功' % n) print('第[%s]次颜色切换成功' % n) else: m += 1 self.loger.info('第[%s]次颜色切换失败' % m) print('第[%s]次颜色切换失败' % m) sleep(3) #色温切换 self.move_seekBar(ele, self.color_temp[j]) sleep(1) #判断串口日志 check_com_result_2 = self.com_bluetooth_normal_log_check( self.dataList, 420, 'color_switch', '330500') if check_com_result_2: p += 1 self.loger.info('第[%s]次色温切换成功' % p) print('第[%s]次色温切换成功' % p) else: q += 1 self.loger.info('第[%s]次色温切换失败' % q) print('第[%s]次色温切换失败' % q) #切换一次等三秒 sleep(3) # 点击取消全选 self.driver.find_element_by_id('select_all').click() sleep(1) self.loger.info('颜色切换总共:%s次' % (times * 7) + ',成功:%s次' % n + ',失败:%s次' % m + ',成功率:%s' % (n / (times * 7) * 100) + '%') print('颜色切换总共:%s次' % (times * 7) + ',成功:%s次' % n + ',失败:%s次' % m + ',成功率:%s' % (n / (times * 7) * 100) + '%') self.loger.info('色温切换总共:%s次' % (times * 7) + ',成功:%s次' % p + ',失败:%s次' % q + ',成功率:%s' % (p / (times * 7) * 100) + '%') print('色温切换总共:%s次' % (times * 7) + ',成功:%s次' % p + ',失败:%s次' % q + ',成功率:%s' % (p / (times * 7) * 100) + '%') print('测试结束') self.start = False #亮度切换 def brightness_switch_handle(self, times): n = 0 m = 0 # 进入设备详情页 self.enter_device() try: WebDriverWait( self.driver, 60, 1).until(lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"场景\")")).is_displayed() except: print('设备详情页加载失败') else: sleep(3) # 亮度滑动条 ele = self.driver.find_element_by_id('brightness_progress') if times != None: for i in range(times): for j in range(9): # 亮度切换 try: self.move_seekBar(ele, self.brightness[j]) sleep(3) #串口检测 check_com_result = self.com_bluetooth_normal_log_check( self.dataList, 180, 'brightness', '550102000000c2') if check_com_result: n += 1 self.loger.info('第[%s]次亮度切换成功' % n) print('第[%s]次亮度切换成功' % n) else: m += 1 self.loger.info('第[%s]次亮度切换失败' % m) print('第[%s]次亮度切换失败' % m) except Exception as e: print(e) continue sleep(2) self.loger.info('亮度切换总共:%s次' % (times * 9) + ',成功:%s次' % n + ',失败:%s次' % m + ',成功率:%s' % (n / (times * 9) * 100) + '%') print('亮度切换总共:%s次' % (times * 9) + ',成功:%s次' % n + ',失败:%s次' % m + ',成功率:%s' % (n / (times * 9) * 100) + '%') print('测试结束') self.start = False #灵敏度切换 def sensitivity_switch_handle(self, times): n = 0 m = 0 # 进入设备详情页 self.enter_device() try: WebDriverWait( self.driver, 60, 1).until(lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"场景\")")).is_displayed() except: print('设备详情页加载失败') else: sleep(3) #切换到颜色场景 self.driver.find_element_by_id('mode_1_icon').click() sleep(3) # 上滑到灵敏度 self.swip.swipe_up(600) sleep(2) # 灵敏度滑动条 ele = self.driver.find_element_by_id('sensitivity_progress') if times != None: for i in range(times): for j in range(9): # 亮度切换 try: self.move_seekBar(ele, self.sensitivity[j]) sleep(3) # 串口检测 check_com_result = self.com_bluetooth_normal_log_check( self.dataList, 410, 'sensitivity', '330500') if check_com_result: n += 1 self.loger.info('第[%s]次灵敏度切换成功' % n) print('第[%s]次灵敏度切换成功' % n) else: m += 1 self.loger.info('第[%s]次灵敏度切换失败' % m) print('第[%s]次灵敏度切换失败' % m) except Exception as e: print(e) continue sleep(2) self.loger.info('灵敏度切换总共:%s次' % (times * 9) + ',成功:%s次' % n + ',失败:%s次' % m + ',成功率:%s' % (n / (times * 9) * 100) + '%') print('灵敏度切换总共:%s次' % (times * 9) + ',成功:%s次' % n + ',失败:%s次' % m + ',成功率:%s' % (n / (times * 9) * 100) + '%') print('测试结束') self.start = False #场景切换 def mode_switch_handle(self, times): self.enter_device() sleep(5) n = 0 # 开关按钮:iv_switch try: WebDriverWait( self.driver, 5, 1).until(lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"设备已关闭\")")).is_displayed() except: self.loger.info('设备当前状态是开') print('设备当前状态是开') else: print('设备当前状态是关,打开设备') self.loger.info('设备当前状态是关,打开设备') try: sleep(2) self.driver.find_element_by_id('btn_switch').click() sleep(1) except Exception as e: print(e) try: WebDriverWait( self.driver, 60, 1).until(lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"场景\")")).is_displayed() except: print('设备详情页加载失败') else: for i in range(times): sleep(4) # 音乐模式 try: try: WebDriverWait(self.driver, 60, 1).until( lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"音乐\")")).is_displayed( ) except Exception as e: print(e) except: self.loger.info('蓝牙连接失败') self.screenshot('蓝牙连接失败') self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"重新连接\")").click() else: sleep(2) try: self.driver.find_element_by_id('mode_1_icon').click() except Exception as e: print(e) continue sleep(1) #检测串口日志 check_com_result = self.com_bluetooth_normal_log_check( self.dataList, 300, 'mode_switch') if check_com_result: n += 1 if self.check_ble_status(): self.loger.info('切换音乐模式后,蓝牙连接正常') else: self.loger.info('切换音乐模式后,蓝牙断连') self.screenshot('切换音乐模式后,蓝牙断连') WebDriverWait(self.driver, 60, 1).until( lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"重新连接\")")).click() sleep(3) # 颜色模式 try: WebDriverWait(self.driver, 60, 1).until( lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"颜色\")")).is_displayed() except: self.loger.info('蓝牙连接失败') self.screenshot('蓝牙连接失败') self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"重新连接\")").click() else: try: self.driver.find_element_by_id('mode_2_icon').click() except Exception as e: print(e) continue # 检测串口日志 sleep(2) check_com_result = self.com_bluetooth_normal_log_check( self.dataList, 1140, 'mode_switch') if check_com_result: n += 1 if self.check_ble_status(): self.loger.info('切换颜色模式后,蓝牙连接正常') else: self.loger.info('切换颜色模式后,蓝牙断连') self.screenshot('切换颜色模式后,蓝牙断连') WebDriverWait(self.driver, 60, 1).until( lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"重新连接\")")).click() sleep(3) # 场景模式 try: WebDriverWait(self.driver, 60, 1).until( lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"场景\")")).is_displayed() except: self.loger.info('蓝牙连接失败') self.screenshot('蓝牙连接失败') self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"重新连接\")").click() else: try: self.driver.find_element_by_id('mode_3_icon').click() except Exception as e: print(e) continue sleep(2) # 检测串口日志 check_com_result = self.com_bluetooth_normal_log_check( self.dataList, 200, 'mode_switch') if check_com_result: n += 1 if self.check_ble_status(): self.loger.info('切换场景模式后,蓝牙连接正常') else: self.loger.info('切换场景模式后,蓝牙断连') self.screenshot('切换场景模式后,蓝牙断连') WebDriverWait(self.driver, 60, 1).until( lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"重新连接\")")).click() # DIY模式 sleep(3) try: WebDriverWait(self.driver, 60, 1).until( lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"DIY\")")).is_displayed() except: self.loger.info('蓝牙连接失败') self.screenshot('蓝牙连接失败') self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"重新连接\")").click() else: try: self.driver.find_element_by_id('mode_4_icon').click() except Exception as e: print(e) continue sleep(2) # 检测串口日志 check_com_result = self.com_bluetooth_normal_log_check( self.dataList, 666, 'mode_switch') if check_com_result: n += 1 if self.check_ble_status(): self.loger.info('切换DIY模式后,蓝牙连接正常') # self.loger.info('----------------------------------') else: self.loger.info('切换DIY模式后,蓝牙断连') self.screenshot('切换DIY模式后,蓝牙断连') WebDriverWait(self.driver, 60, 1).until( lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"重新连接\")")).click() self.loger.info('第[%s]次循环结束' % (i + 1)) print('第[%s]次循环结束' % (i + 1)) self.start = False self.loger.info('模式切换共[%s]次' % (times * 4) + ',成功了[%s]次' % n) print('模式切换共[%s]次' % (times * 4) + ',成功了[%s]次' % n) #检查蓝牙状态 def check_ble_status(self): try: WebDriverWait( self.driver, 5, 0.5).until(lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"已连接\")")).is_displayed() except: return False else: return True #滑动温度进度条 def move_seekBar(self, element, percent): # 精度不是很准确,需自己调,当percent为1时可能不会移动到100%,可以尝试1.5或2 # 获取拖动条的宽 width = element.size.get("width") # 获取坐标 x = element.location.get("x") y = element.location.get("y") self.driver.swipe(int(width * 0.95 + x), y, int((width + x) * percent), y, 3000) # 选择要添加的设备类型 def chooes_device(self): # 点击+号 WebDriverWait(self.driver, 60, 1).until(EC.element_to_be_clickable( (By.ID, 'iv_add'))).click() sleep(3) self.loger.info('开始添加设备') # 点击搜索 WebDriverWait(self.driver, 30, 1).until(EC.element_to_be_clickable( (By.ID, 'tv_search'))).click() sleep(2) # 切换输入法,方便后续调用输入法的搜索按钮 self.driver.activate_ime_engine( 'com.sec.android.inputmethod/.SamsungKeypad') # 搜索框输入device_type self.driver.press_keycode(13) sleep(0.5) self.driver.press_keycode(7) sleep(0.5) self.driver.press_keycode(14) sleep(0.5) self.driver.press_keycode(13) WebDriverWait(self.driver, 30, 1).until(EC.element_to_be_clickable( (By.ID, 'sku_icon'))).click() #设备配网 #1、次数 2、是否跳过WiFi设置 def add_device(self, skip_wifi, times): n = 0 # 成功次数 m = 0 # 配网执行次数 k = 0 # WiFi列表加载失败次数 app_check = False com_check = False for i in range(times): # 选择设备类型 try: self.chooes_device() m += 1 while True: try: WebDriverWait(self.driver, 60, 1).until( lambda x: x.find_element_by_android_uiautomator( 'new UiSelector().text(\"%s\")' % self.device) ).is_displayed() except: self.loger.info('蓝牙扫描失败,重新扫描') self.screenshot('蓝牙扫描失败') try: WebDriverWait( self.driver, 60, 1).until(lambda x: x. find_element_by_android_uiautomator( "new UiSelector().text(\"重新扫描\")") ).is_displayed() except: self.loger.info('重新扫描按钮不可点击') else: self.loger.info('点击重新扫描按钮') self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"重新扫描\")").click() else: self.loger.info('蓝牙扫描成功') sleep(3) ele = self.driver.find_element_by_android_uiautomator( 'new UiSelector().text(\"%s\")' % self.device) sleep(1) ele.click() break # 进入配对界面,点击设备开关按键 try: WebDriverWait(self.driver, 60, 1).until( lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"配对\")")).is_displayed() except: self.loger.info('进入蓝牙配对界面失败') else: sleep(2) # 设备按键 self.relay_h.once_click() # 蓝牙连接成功,点击完成 try: WebDriverWait(self.driver, 120, 1).until( lambda x: x.find_element_by_android_uiautomator( "new UiSelector().text(\"完成\")")).is_displayed( ) except: self.loger.info('蓝牙配对超时') self.screenshot('蓝牙配对超时') else: self.loger.info('蓝牙配对成功') # 点击完成 self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"完成\")").click() # 进入WiFi配置界面 sleep(2) try: WebDriverWait( self.driver, 120, 1).until(lambda x: x. find_element_by_android_uiautomator( "new UiSelector().text(\"完成\")") ).is_displayed() except: self.loger.info('wifi设置页面加载失败') self.screenshot('wifi设置页面加载失败') # 跳过WiFi设置 self.skip_wifiSetting() # 删除设备 self.delete_lempDevice() else: self.loger.info('wifi设置页面加载成功') self.screenshot('wifi设置页面加载成功') # 是否跳过WiFi设置 if skip_wifi: self.loger.info('跳过WiFi设置') # 跳过WiFi设置 self.skip_wifiSetting() # 判断设备是否添加成功 try: WebDriverWait(self.driver, 60, 1).until( lambda x: x. find_element_by_android_uiautomator( "new UiSelector().text(\"场景\")" )).is_displayed() except: print('添加设备失败') self.loger.info('添加设备失败') else: n += 1 print('第[%s]次添加设备成功' % n) self.loger.info('第[%s]次添加设备成功' % n) # 删除设备 self.delete_lempDevice() else: sleep(2) self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"完成\")").click() # 校验配网串口日志,等4秒 # sleep(1) check_result_2 = self.com_bluetooth_adddevice_log_check( self.dataList) # 判断设备是否配网成功 try: WebDriverWait(self.driver, 180, 0.1).until( EC.presence_of_element_located( (By.XPATH, '//*[contains(@text,"连接成功")]'))) except: self.loger.info('APP端Toast提示失败') print('APP端Toast提示失败') self.loger.info('设备配网失败') print('设备配网失败') # 删除设备 self.delete_lempDevice() else: self.loger.info('APP端Toast提示成功') print('APP端Toast提示成功') if check_result_2: n += 1 self.loger.info('第[%s]次设备配网成功' % n) print('第[%s]次设备配网成功' % n) else: self.loger.info('第[%s]次设备配网失败' % n) print('第[%s]次设备配网失败' % n) # 删除设备 self.delete_lempDevice() except Exception as e: print(e) continue self.start = False self.loger.info('[%s]' % self.device + '设备配网[%s]次,' % m + '成功了[%s]次,' % n + '配网成功率%s' % (n / m * 100) + '%') print('[%s]' % self.device + '设备配网[%s]次,' % m + '成功了[%s]次,' % n + '配网成功率%s' % (n / m * 100) + '%') self.loger.info('wifi设置页面加载失败次数:%s' % k) print('wifi设置页面加载失败次数:%s' % k) #删除设备 def delete_lempDevice(self): sleep(4) btn_settting = self.driver.find_element_by_id("btn_setting") sleep(1) btn_settting.click() sleep(2) #向下滑动 self.swip.swipe_up(600) self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"删除设备\")").click() sleep(1) yes = self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"是\")") yes.click() sleep(1) #刷新首页 self.swip.swipe_down(600) try: WebDriverWait( self.driver, 5, 1 ).until(lambda x: x.find_element_by_android_uiautomator( 'new UiSelector().text(\"%s\")' % self.device)).is_displayed() except: self.loger.info('删除设备成功') self.loger.info( '分割线--------------------------------------------------分割线') else: self.loger.info('删除设备失败') #跳过WiFi设置 def skip_wifiSetting(self): sleep(2) self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"跳过\")").click() sleep(2) self.driver.find_element_by_android_uiautomator( "new UiSelector().text(\"确认\")").click() # 监测蓝牙配对日志 def com_bluetooth_adddevice_log_check(self, dataList): start = len(dataList) - 2500 end = len(dataList) str1 = dataList[start:end] str2 = str1.replace(" ", "").replace("\r", "").replace("\n", "") # 去掉空格、回车符、换行符 strlist = str2.split(':', 50) # 以冒号进行分割 # 打印串口日志 self.loger.info('配网日志:' + str(strlist)) print('配网日志:' + str(strlist)) sl = '33b200' if str(strlist).find(sl) >= 0: self.loger.info('本次配网日志校验成功') print('本次配网日志校验成功') return True else: print('本次配网日志校验失败') self.loger.info('本次配网日志校验失败') return False #监测蓝牙控制日志 def com_bluetooth_normal_log_check(self, dataList, mlen, option_type, sl=None): start = len(dataList) - mlen end = len(dataList) str1 = dataList[start:end] str2 = str1.replace(" ", "").replace("\r", "").replace("\n", "") # 去掉空格、回车符、换行符 strlist = str2.split(':', 50) # 以冒号进行分割 #打印串口日志 self.loger.info('本次操作后蓝牙日志:' + str(strlist)) print('本次操作后蓝牙日志:' + str(strlist)) if option_type == 'mode_switch': for i in range(len(strlist)): sl = strlist[i][len(strlist[i]) - 7:len(strlist[i])] if sl == 'blesend': print('blesend:' + strlist[i + 1]) self.loger.info('蓝牙回复结果:blesend:' + str(strlist[i + 1])) if strlist[i + 1][0] == '3' and strlist[ i + 1][4] == '0' and strlist[i + 1][5] == '0': self.loger.info('本次蓝牙下操作成功') print('本次蓝牙下操作成功') return True else: self.loger.info('本次蓝牙下操作失败') print('本次蓝牙下操作失败') return False else: check = False for i in range(len(strlist)): if str(strlist[i]).find(sl) >= 0: self.loger.info(strlist[i]) print(strlist[i]) check = True break else: check = False return check #监测普通WiFi日志 def com_wifi_normal_log_check(self, dataList, sl, option_type): if option_type == 'on_off_switch': start = len(dataList) - 3400 elif option_type == 'leve_switch': start = len(dataList) - 1400 else: start = len(dataList) - 2200 end = len(dataList) str1 = dataList[start:end] str2 = str1.replace(" ", "").replace("\r", "").replace("\n", "") # 去掉空格、回车符、换行符 strlist = str2.split(':', 50) # 以冒号进行分割 # 打印串口日志 self.loger.info('本次操作后日志:' + str(strlist)) for i in range(len(strlist)): if strlist[i].find(sl) >= 0: print('串口日志:' + strlist[i]) self.loger.info('串口日志:' + strlist[i]) return strlist[i] # 监测WiFi配网日志 def com_wifi_adddevice_log_check(self, dataList): start = len(dataList) - 8000 end = len(dataList) str1 = dataList[start:end] str2 = str1.replace(" ", "").replace("\r", "").replace("\n", "") # 去掉空格、回车符、换行符 strlist = str2.split(':', 80) # 以冒号进行分割 # 打印串口日志 self.loger.info('wifi联网日志:' + str(strlist)) print('wifi联网日志:' + str(strlist)) sl = 'IOT_BLE_WIFI_CONNECT_SUC' if str(strlist).find(sl) >= 0: print('本次WiFi连接成功') return True else: print('本次WiFi连接失败') return False # 接收串口数据 def recv(self): if self.start: print("开始接收数据:") else: print("测试完毕,停止接收数据:") while self.start: try: count = self.main_engine.inWaiting() # 获取串口缓冲区数据 if count != 0: data = self.main_engine.read( self.main_engine.in_waiting).decode( "gbk") # 读出串口数据,数据采用gbk编码 # print(time.strftime("%Y-%m-%d %H-%M-%S"), " --- recv --> ") # 打印一下子 time.sleep(0.1) # 延时0.1秒,免得CPU出问题 self.dataList += data except Exception as e: print("异常报错:", e) # 截图 def screenshot(self, msg): # 截图保存 now = time.strftime("%Y-%m-%d %H-%M-%S") image_name = LOG_DIR + '/' + msg + " " + now + ".png" self.driver.save_screenshot(image_name) # # # if __name__ == '__main__': # platformVersion = '10' #安卓系统版本 # mobileName = 'R58M36MWZ1B' #手机名称 # device = 'H7130_C583' #设备名称 # times = 100 #测试次数 # Ret1 = False #日志打印串口是否创建成功标志 # Ret2 = False #控制串口是否创建成功标志 # com1 = 'com13' # bps = 115200 # timeout = 0.5 # com2 = "com12" # bps2 = 9600 # timex = 0.5 # bytesize = 8 # stopbits = 1 # ms = Humidifier2(platformVersion,mobileName,device,com1, bps, timeout,com2,bps2,bytesize,stopbits) # if(Ret1 and Ret2): # #功能线程运行 # # threading.Thread(target=ms.add_device,args=(times,)).start() # # threading.Thread(target=ms.leve_switch, args=(times,)).start() # threading.Thread(target=ms.on_off_switch, args=(times,)).start() # #日志线程运行 # threading.Thread(target=ms.recv,).start() # # ms.add_device(times) # else: # print("创建串口失败")