前言
实习工作记录
一、pyautogui
用于自动化实现的python库
import pyautogui
# 鼠标移动到屏幕中与图片匹配的位置,置信度为0.6,返回鼠标位置(x,y)
goto_pos = pyautogui.locateCenterOnScreen( "photo/mtstart.png", confidence=0.6)
pyautogui.moveTo(goto_pos, duration=1) # 鼠标移动到指定位置,持续时间为1s
pyautogui.click() # 鼠标单击
pyautogui.doubleClick() # 鼠标双击
pyautogui.typewrite(str) # 输入字符串
screeshot = pyautogui.screenshot() # 截屏
pyautogui.locate(image, screeshot, confidence=0.8) # 在screeshot中匹配image,置信度为0.8
pyautogui.moveRel(x,y) # 鼠标相对于当前位置移动(x,y),右下为正,左上为负
pyautogui.keyDown('a') # 按下键"a"不释放
pyautogui.keyUp('a') # 释放键"a"
pyautogui.press('esc') # 按一下"Esc"键(按下并释放)
# 使用组合快捷键,注意顺序
pyautogui.hotkey('win', 'm') # 返回桌面
# 实现拖拽功能
pyautogui.mouseDown(button = 'left', x = x, y = y) # 在(x,y)位置按下鼠标左键
pyautogui.moveTo(x = x-100, y = y, duration = 0.5)
pyautogui.mouseUp(button = 'left') # 松开鼠标左键
time.sleep(1) # 暂停1s
二、日志输出
import logging
import datetime
class Log():
def __init__(self, log_file=None, log_name=None, log_level=logging.DEBUG):
self.logger = logging.getLogger(log_name)
self.logger.setLevel(log_level)
sss = datetime.now()
self.time_string = sss.strftime("%Y-%m-%d %H_%M_%S")
log_file = "report/"+log_file+self.time_string+".log"
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
if log_file:
file_handler = logging.FileHandler(log_file)
file_handler.setLevel(log_level)
file_handler.setFormatter(formatter)
self.logger.addHandler(file_handler)
console_handler = logging.StreamHandler()
console_handler.setLevel(log_level)
console_handler.setFormatter(formatter)
self.logger.addHandler(console_handler)
def AUTOTEST_DEBUG(self, msg):
self.logger.debug(msg)
def AUTOTEST_INFO(self, msg):
self.logger.info(msg)
def AUTOTEST_WARNING(self, msg):
self.logger.warning(msg)
def AUTOTEST_ERROR(self, msg):
self.logger.error(msg)
def AUTOTEST_CRITICAL(self, msg):
self.logger.critical(msg)
from lib_debug import Log
test_log = Log("report",log_name=__name__)
test_log.AUTOTEST_DEBUG(str)
test_log.AUTOTEST_INFO(str)
三、allure
1、下载安装
安装JDK、allure
新建系统变量:
- 变量名:JAVA_HOME
- 值:JDK主目录的地址
在系统变量path
中新建:
- %JAVA_HOME%\bin
- allure主目录的地址\bin
在cmd
中运行:
java -version
allure --version
pip install allure-pytest // 用于pytest调用allure
2、代码使用
import allure
@allure.feature(str)
@allure.story(str)
@allure.title(str)
@pytest.mark.demo
allure.step(str) # 添加步骤描述
allure.attach.file("screen/%s.png" % mt.picturename, attachment_type=allure.attachment_type.PNG) # 在测试报告中添加附件
# 将allure生成的报告写入文件夹
pytest.main(['-s','--alluredir','report\%s\\tmp' % report_name])
os.system(r'allure generate report\%s\\tmp -o report\%s\\report --clean' % (report_name,report_name))
四、pypi
网址:https://pypi.org/
查询python
第三方库的相关信息
五、pip
查询库存在的所有版本
pip install==
查询已安装的库的相关信息
pip show 名字
在指定版本的python中安装/卸载
C:\Python38\python -m pip
镜像源
-i https://pypi.tuna.tsinghua.edu.cn/simple
-i https://mirrors.huaweicloud.com/repository/pypi/simple