WebUI自动化中截图的使用
2023-02-26 09:47:58 时间
前言
做UI自动化时,有时候可能因为需要把某个步骤的界面显示截图保存,这里我用到的是webdriver的截图功能。
环境搭建
这里说的UI自动化指的是 web的UI自动化,使用的是Python+Selenium+webdriver写的,用Unittest框架来组织用例和脚本。具体环境搭建可以参考之前的环境搭建详细文章:
https://blog.csdn.net/NoamaNelson/article/details/102971936
封装截图功能creenShot.py
driver:指的是使用哪个浏览器的驱动,我后边使用的是Chrome
# coding=utf-8
import time
from selenium import webdriver
def sav_creenshot(driver):
now=time.strftime("%Y-%m-%d-%H_%M_%S",time.localtime(time.time())) # 截图保存的文件名格式
pic_path = "./"+now+'_screen.png' # 截图保存的路径
# print(pic_path)
driver.save_screenshot(pic_path) # 调用Driver的截图保存功能
示例test_baidu.py
写一个百度搜索hello的实例,test_baidu.py
# coding=utf-8
from selenium import webdriver
import time
import unittest
import logging
from creenShot import sav_creenshot # 导入刚刚封装的截图方法
class Testbaidu(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome() # 指定浏览器驱动
self.url = "https://www.baidu.com/" # URL地址
self.driver.maximize_window() # 最大化窗口
self.driver.get(self.url) # 获取访问网址
self.log = logging.getLogger() # 记录日志
def tearDown(self):
self.driver.close() # 关闭浏览器
def test_search(self):
# 搜索框输入“hello”
self.log.info("输入框输入hello")
self.driver.find_element_by_id("kw").send_keys("hello")
self.driver.find_element_by_id("su").click()
time.sleep(2)
sav_creenshot(self.driver) # 截图
self.assertIn("hello", self.driver.page_source)
self.log.info("搜索成功")
if __name__ == "__main__":
unittest.main()
相关文章
- Jgit的使用笔记
- 利用Github Action实现Tornadofx/JavaFx打包
- 叹息!GitHub Trending 即将成为历史!
- 微软软了?开源社区讨论炸锅,GitHub CEO 亲自来答
- GitHub Trending 列表频现重复项,前后端都没去重?
- Photoshop Elements 2021版本软件安装教程(mac+windows全版本都有)
- (ps全版本)Photoshop 2020的安装与破解教程(mac+windows全版本都有)
- (ps全版本)Photoshop cc2018的安装与破解教程(mac+windows全版本,包括2023
- 环境搭建:Oracle GoldenGate 大数据迁移到 Redshift/Flat file/Flume/Kafka测试流程
- 每个开发人员都要掌握的:最小 Linux 基础课
- 来撸羊毛了!Windows 环境下 Hexo 博客搭建,并部署到 GitHub Pages
- 超实用!手把手入门 MongoDB:这些坑点请一定远离
- 【GitHub日报】22-10-09 zustand、neovim、webtorrent、express 等4款App今日上新
- 【GitHub日报】22-10-10 brew、minio、vite、seaweedfs、dbeaver 等8款App今日上新
- 【GitHub日报】22-10-11 cobra、grafana、vue、ToolJet、redwood 等13款App今日上新
- Photoshop 2018 下载及安装教程(mac+windows全版本都有,包括最新的2023)
- Photoshop 2017 下载及安装教程(mac+windows全版本都有,包括最新的2023)
- Photoshop 2020 下载及安装教程(mac+windows全版本都有,包括最新的2023)
- Photoshop 2023 资源免费下载(mac+windows全版本都有,包括最新的2023)
- 最新版本Photoshop CC2018软件安装教程(mac+windows全版本都有,包括2023