def test_baidu_search(self): u'''百度搜索''' driver = self.driver logger.startLine(self.__class__.__name__, get_current_function_name()) try: print('打开网页') driver.get(self.base_url) print('输入搜索内容') driver.find_element_by_id('kw').clear() driver.find_element_by_id('kw').send_keys('uniquefu') print('点击搜索') driver.find_element_by_id('su').click() time.sleep(3) print('检查点校验成功') logger.info(u'执行成功') except Exception as e: print('用例执行失败:' + repr(e)) logger.error(driver, u'用例执行失败:' + repr(e)) raise str(e) finally: logger.endLine(self.__class__.__name__, get_current_function_name())
def setUp(self): try: self.driver = webdriver.Firefox() except Exception as e: logger.error('打开浏览器失败' + e) # 添加智能等待 self.driver.implicitly_wait(30) self.base_url = 'https://www.baidu.com/'
def upload(self, file, filename): try: file_path = UPLOAD_PATH + filename file.save(file_path) self.success = True self.file_path = file_path except Exception as e: logger.error('Upload error, message is ' + str(e)) self.success = False finally: return self
def add_facial(user_id, signature, session): try: new_facial = db.Facial(UserId=user_id, facial_features=signature[0]) session.add(new_facial) session.commit() logger.info('Add facial features.(user_id is ' + str(user_id) + ')') return {'success': True} except Exception as e: logger.error('add_facial error is ' + str(e)) return {'success': False}
def registered_base64(self): try: filename = str(uuid.uuid1()) + '.jpg' self.init() if 'fileContent' not in request.json: self.success = False self.error = '缺少fileContent参数' return if 'userId' not in request.json: self.success = False self.error = '缺少userId参数' return # 文件保存 file_content = re.sub('^data:image/\\w+;base64,', '', request.json['fileContent']) self.fileHelper.upload_base64(file_content=file_content, file_name=filename) if self.fileHelper.success: # 获取特征码 self.facialHelper.get_signature( self.fileHelper.file_path, request.json['rotating'] if 'rotating' in request.json else False) # 面部信息注册 if self.facialHelper.success: if len(self.facialHelper.signature) > 0: return Facial.add_facial(request.json['userId'], self.facialHelper.signature, self.session) else: logger.warning('未识别到有有效人脸信息!') self.success = False self.error = '未识别到有有效人脸信息' else: self.success = False self.error = self.facialHelper.error return else: self.success = False self.error = '文件异常!' except Exception as e: logger.error('registered_base64 error is ' + str(e)) self.success = False self.error = '面部信息注册异常!' finally: result = { 'success': self.success, } if not self.success: result['error'] = self.error return result
def upload_base64(self, file_content, file_name): try: file_path = UPLOAD_PATH + file_name file = open(file_path, 'wb') file.write( base64.b64decode( re.sub('^data:image/\\w+;base64,', '', file_content))) file.close() self.success = True self.file_path = file_path return self except Exception as e: logger.error('Upload error, message is ' + str(e)) self.success = False return self
def find_facial(user_id, session): try: count = session.query( db.Facial).filter(db.Facial.UserId == user_id).count() if (count > 0): return { 'success': True, } else: return {'success': False} pass except Exception as e: logger.error('验证面部信息异常。' + str(e)) return {'success': False, 'error': '验证面部信息异常。' + str(e)}
def facial_information_comparison(self, code, user_id, session): try: through_user = [] users = session.query(db.Facial) \ .filter(True if user_id is True else db.Facial.UserId.in_(user_id.split(',') if type(user_id) != int else [user_id])) \ .all() for u in users: if u.UserId not in through_user: result = face_recognition.compare_faces( code, u.facial_features, 0.4) for r in result: if r: through_user.append(u.UserId) self.success = True self.through_user = through_user except Exception as e: logger.error('Abnormal face recognition,error info is' + str(e)) self.success = False self.error = '人脸信息对比异常.' finally: return self
def test_baidu_set(self): u'''百度设置''' driver = self.driver logger.startLine(self.__class__.__name__, get_current_function_name()) try: print('打开网页') driver.get(self.base_url) # 进入搜索设置页面 print('进入搜索设置页面') elment = driver.find_element_by_link_text('设置') ActionChains(driver).move_to_element(elment).perform() driver.find_element_by_link_text('搜索设置').click() time.sleep(3) # 修改设置 print('修改设置') driver.find_element_by_id('s1_2').click() time.sleep(3) # 保存设置 print('保存设置') driver.find_element_by_link_text('保存设置').click() time.sleep(3) # 获取网页上的警告信息并接收警告信息 print('确认保存设置') alert = driver.switch_to_alert().accept() time.sleep(3) print('检查点校验成功') logger.info(u'执行成功') except Exception as e: logger.error(driver, u'用例执行失败:' + str(e)) print(repr(e)) raise e finally: logger.endLine(self.__class__.__name__, get_current_function_name())
def delete(file_path): try: os.remove(file_path) except Exception as e: logger.error('文件删除异常!' + str(e))
def auth(self, choose): try: filename = str(uuid.uuid1()) + '.jpg' self.init() parameter = request.form # True 代表所有 user_id = True if choose == 'base64': parameter = request.json if 'fileContent' not in parameter: return {'success': False, 'error': '缺少fileContent参数'} if 'userId' in parameter: user_id = parameter['userId'] self.fileHelper.upload_base64(parameter['fileContent'], filename) else: if 'file' not in request.files: return {'success': False, 'error': '请使用file参数上传文件'} if 'userId' in parameter: user_id = parameter['userId'] self.fileHelper.upload(request.files['file'], filename) if self.fileHelper.success: # 获取文件中图像的特征码 self.facialHelper.get_signature( self.fileHelper.file_path, parameter['rotating'] if 'rotating' in parameter else False) if self.facialHelper.success: # 特征码比对 self.facialHelper.facial_information_comparison( self.facialHelper.signature, user_id, self.session) if self.facialHelper.success: logger.info('Face recognition passes.userId is ' + str(self.facialHelper.through_user)) self.success = True else: # 特征码比对异常 self.success = False self.error = self.facialHelper.error logger.info('facial_information_comparison error is ' + self.facialHelper.error) else: # 获取特征码异常 self.success = False self.error = self.facialHelper.error logger.info('get_signature error is ' + self.facialHelper.error) else: # 转换文件异常 self.success = False self.error = '上传文件异常' logger.info('get_signature error is ' + self.fileHelper.error) through_user = self.facialHelper.through_user result = { 'success': self.success, 'through_user': through_user if (',' in str(user_id) or user_id is True) else len(through_user) > 0 } if not self.success: result['error'] = self.error return result except Exception as e: logger.error('人脸识别异常,' + str(e)) return {'success': False, 'error': '人脸识别异常,' + str(e)}