def find_element(self, locator, condition='presence', retries=1): """ 通过等待条件定位元素 :param locator: 定义元组。例:(By.ID, '//*[@id="kw"]') :param condition: 等待条件 :param retries: 重试次数,默认1 :return: node or None """ error_info = None for times in range(retries + 1): try: LOGGER.debug(f'定位元素:{locator}') if condition == 'visibility': # 等待节点可见 node = self.wait.until( EC.visibility_of_element_located(locator)) else: # 等待节点加载出来 node = self.wait.until( EC.presence_of_element_located(locator)) break except Exception as ex: error_info = f'定位 {locator} 失败,错误信息:{ex}' LOGGER.error(error_info) if times < retries: LOGGER.warning(f'正在重试,当前重试次数:{times},总数:{retries}') time.sleep(1) else: raise Exception(error_info) return node
def send_mail(self, sender, receiver, subject, body, attachments=[]): """ 发送邮件 QQ邮箱 smtp_password 获取方式:进入QQ邮箱-设置-账户-开启服务-开启POP3/SMTP服务,然后点击生成授权码 :param sender: 发件人 :param receiver: 收件人 :param subject: 邮件主旨 :param body: 邮件正文(超文本格式) :param attachments: 附件列表 :return: """ mail_obj = self.build_mail_object(sender, receiver, subject, body, attachments) try: # 登陆smtp服务器 sftp_obj = smtplib.SMTP(self.smtp_server, self.smtp_port) sftp_obj.login(user=self.smtp_user, password=self.smtp_password) # 发送邮件 sftp_obj.sendmail(sender, receiver, mail_obj.as_string()) LOGGER.debug(f'发送邮件成功,发件人:{sender},收件人:{receiver},主旨:{subject}') # 退出登陆 sftp_obj.quit() except Exception as ex: LOGGER.error( f'发送邮件失败,错误原因:{ex},发件人:{sender},收件人:{receiver},主旨:{subject}')
def wrapper(*args, **kwargs): browser_driver = kwargs.get('browser') # 获取driver func_name = func.__name__ # 获取测试函数名 start_time = datetime.datetime.now() LOGGER.debug(f'用例({func_name})执行时间:{start_time}') try: func(*args, **kwargs) # 执行测试函数 except Exception as ex: LOGGER.error(f'用例({func_name})执行失败,错误信息:{ex}') # 截图保存 browser_driver.screenshot(storage_path=MODULE_DIR['failure_screenshot'], picture_name=func_name) end_time = datetime.datetime.now() LOGGER.debug(f'用例({func_name})结束时间:{start_time},耗时:{(end_time - start_time).total_seconds()}s')
def build_mail_object(sender, receiver, subject, body, attachments=[]): """ 构建邮件对象(主旨、正文、附件) :param sender: 发件人 :param receiver: 收件人 :param subject: 邮件主旨 :param body: 邮件正文(超文本格式) :param attachments: 附件列表 :return: mail obj """ # 构造邮件头信息 mail_obj = MIMEMultipart('mixed') mail_obj['From'] = sender mail_obj['To'] = ','.join(receiver) if isinstance(receiver, list) else receiver mail_obj['subject'] = Header(subject, 'utf-8') # 构造邮件正文(使用超文本格式) html_obj = MIMEText(body, 'html', 'utf-8') mail_obj.attach(html_obj) if attachments: # 添加附件到邮件中 for each_att in attachments: if not os.path.exists(each_att): LOGGER.error( f'添加附件失败,请检查文件:({each_att}),发件人:{sender},收件人:{receiver},主旨:{subject}' ) continue att_file = open(each_att, 'rb') file_obj = MIMEText(att_file.read(), 'base64', 'utf-8') att_file.close() file_obj["Content-Type"] = 'application/octet-stream' file_obj["Content-Disposition"] = 'attachment;filename="{}"' \ .format(Header(os.path.basename(each_att).encode('utf-8'), 'utf-8')) mail_obj.attach(file_obj) LOGGER.debug( f'添加附件成功,文件:({each_att}),发件人:{sender},收件人:{receiver},主旨:{subject}' ) return mail_obj
def wrapper(*args, **kwargs): func_name = func.__name__ # 获取测试函数名 LOGGER.debug(f'开始执行用例:{func_name}') LOGGER.debug('测试数据' + '*' * 50) for each in args[1:]: # 打印所有测试数据 LOGGER.debug(each) LOGGER.debug('*' * 58) start_time = datetime.datetime.now() LOGGER.debug(f'执行时间:{start_time}') try: func(*args, **kwargs) # 执行测试函数 except Exception as ex: LOGGER.error(f'执行失败,错误信息:{ex}') end_time = datetime.datetime.now() LOGGER.debug( f'结束时间:{start_time},耗时:{(end_time - start_time).total_seconds()}s' )