• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

细节有惊喜Web自动化框架UI自动截图和画面回放实现

武飞扬头像
自由家
帮助1

目录

 前言:

Web自动化测试框架基本结构及原理

UI自动截图实现方法

基于Selenium截图实现UI自动截图的过程如下:

基于Selenium截图的代码实现如下:

基于爬虫截图实现UI自动截图的流程如下:

基于爬虫截图的代码实现如下:

画面回放实现方法

基于PIL模块回放的实现过程如下:

基于PIL模块回放的代码实现如下:

基于OpenCV模块回放的实现过程如下:

基于OpenCV模块回放的代码实现如下:

 总结:


 前言:

在现今互联网快速发展的时代,网站的出现已经成为了人民日益增长的需求,而在这个过程中,自动化测试也变得越来越重要。随着Web前端技术快速发展,Web自动化测试框架成为了测试领域的重要工具之一。而在使用Web自动化测试框架的过程中,UI自动截图及画面回放成为了框架中一个重要的环节。

本文将从介绍Web自动化框架的基本结构和原理为起点,详细讲解UI自动截图及画面回放的实现方法。最后,我们将以Python语言作为实现语言编写代码,为读者实际演示如何通过Web自动化框架封装实现UI自动截图及画面回放的具体细节。

Web自动化测试框架基本结构及原理

在Web自动化测试框架中,主要包含两部分:WebDriver以及语言绑定。其中,WebDriver是一个协议,用于描述网站的行为及操作。而语言绑定则是将WebDriver协议与不同的编程语言进行绑定,从而实现对WebDriver进行自动化测试的目的。

在进行Web自动化测试时,需要使用WebDriver实现对WebUI界面的自动化操作。而要实现UI自动截图及画面回放,需要实现对WebUI界面的状态进行监听,保存该状态信息并根据该信息进行回放操作。

UI自动截图实现方法

UI自动截图所要做的就是在自动执行WebUI操作期间,及时对UI状态进行截图。这样可以帮助测试人员更好地理解测试过程,更快地定位UI异常信息。常见的UI自动截图实现方法有两种:基于Selenium截图;基于爬虫截图。

基于Selenium截图实现UI自动截图的过程如下:

1.初始化 webdriver 对象,并访问想要截图的页面;
2.获取页面的宽度和高度;
3.进行屏幕截图,并保存到指定的本地文件目录中。

基于Selenium截图的代码实现如下:

  1.  
    from selenium import webdriver
  2.  
    from PIL import Image
  3.  
     
  4.  
    browser = webdriver.Chrome()
  5.  
    browser.get('http://www.example.com/')
  6.  
    browser.maximize_window()
  7.  
     
  8.  
    # Get width and height of the webpage
  9.  
    width, height = browser.execute_script("return [window.innerWidth, window.innerHeight]")
  10.  
     
  11.  
    # Take screenshot of the webpage
  12.  
    screenshot = browser.get_screenshot_as_png()
  13.  
    screenshot = Image.open(BytesIO(screenshot))
  14.  
     
  15.  
    # Crop the screenshot to desired height and width
  16.  
    left = 0
  17.  
    top = 0
  18.  
    right = width
  19.  
    bottom = height
  20.  
    screenshot = screenshot.crop((left, top, right, bottom))
  21.  
     
  22.  
    # Save screenshot to a file
  23.  
    screenshot.save('/path/to/screenshot.png')
学新通

基于爬虫截图实现UI自动截图的流程如下:

1.使用爬虫技术爬取想要截图的网站信息;
2.通过解析网站页面,获取需要截图的元素位置;
3.对获取到的元素进行截图;
4.将截图保存在指定的本地文件目录中。

基于爬虫截图的代码实现如下:

  1.  
    import requests
  2.  
    from PIL import Image
  3.  
    from io import BytesIO
  4.  
    from bs4 import BeautifulSoup
  5.  
     
  6.  
    url = 'http://www.example.com/'
  7.  
    html = requests.get(url)
  8.  
     
  9.  
    soup = BeautifulSoup(html.content, 'html.parser')
  10.  
    # Get the element to take screenshot of
  11.  
    element = soup.find('div', id='element_id')
  12.  
     
  13.  
    # Find the position of element on page
  14.  
    location = element.location
  15.  
    size = element.size
  16.  
     
  17.  
    # Take screenshot of the element only
  18.  
    screenshot = Image.open(BytesIO(html.content))
  19.  
    left = location['x']
  20.  
    top = location['y']
  21.  
    right = left size['width']
  22.  
    bottom = top size['height']
  23.  
    screenshot = screenshot.crop
  24.  
    ((left, top, right, bottom))
  25.  
     
  26.  
    # Save the screenshot to a file
  27.  
    screenshot.save('/path/to/screenshot.png')
学新通

画面回放实现方法

画面回放是指在执行自动化测试时,将之前的截图进行合理的处理,重新播放在测试环境中。画面回放可以帮助测试人员更好地理解测试过程,更快地定位UI异常信息。常见的画面回放实现方法有两种:基于PIL模块回放;基于OpenCV模块回放。

基于PIL模块回放的实现过程如下:

1.加载之前保存的UI截图;
2.遍历所有UI截图,显示在测试环境中。

基于PIL模块回放的代码实现如下:

  1.  
    from PIL import Image, ImageDraw
  2.  
    import os
  3.  
     
  4.  
    # Get all the screenshots in the directory
  5.  
    screenshots_dir = '/path/to/screenshots'
  6.  
    screenshots = sorted(os.listdir(screenshots_dir))
  7.  
     
  8.  
    # Create an image object for each screenshot and display it
  9.  
    for screenshot in screenshots:
  10.  
        screenshot_path = os.path.join(screenshots_dir, screenshot)
  11.  
        with Image.open(screenshot_path) as img:
  12.  
            draw = ImageDraw.Draw(img)
  13.  
            img.show()

基于OpenCV模块回放的实现过程如下:

1.遍历之前保存的所有UI截图;
2.使用OpenCV的imshow函数进行UI截图的播放。

基于OpenCV模块回放的代码实现如下:

  1.  
    import cv2
  2.  
    import os
  3.  
     
  4.  
    # Get all the screenshot images in the directory
  5.  
    screenshots_dir = '/path/to/screenshots'
  6.  
    screenshots = sorted(os.listdir(screenshots_dir))
  7.  
     
  8.  
    # Display each screenshot using OpenCV
  9.  
    for screenshot in screenshots:
  10.  
        screenshot_path = os.path.join(screenshots_dir, screenshot)
  11.  
        img = cv2.imread(screenshot_path)
  12.  
        cv2.imshow('UI screenshot', img)
  13.  
        cv2.waitKey(1000)  # Delay in milliseconds between screenshots

 总结:

综上所述,本文从Web自动化框架的基本结构和原理入手,详细地介绍了UI自动截图及画面回放的实现方法。同时,本文还以Python语言作为实现语言,提供相应的代码实现,希望能够帮助读者更好地理解和使用Web自动化测试框架。

学新通

 作为一位过来人也是希望大家少走一些弯路,在这里我给大家分享一些自动化测试前进之路的必须品,如果你用得到的话可以直接拿走,希望能对你带来帮助。(WEB自动化测试、app自动化测试、接口自动化测试、持续集成、自动化测试开发、大厂面试真题、简历模板等等),相信能使你更好的进步!

获取方式:留言【自动化测试】即可

【自动化测试交流】:574737577(备注ccc)学新通http://qm.qq.com/cgi-bin/qm/qr?_wv=1027&k=MyTLBK9pZ74qgHUVVZfITmBhScUS5qPC&authKey=hUGxEWvPxbiSTszm1V9wE6Z/FpVNEdf+zEe4UXSvDPN8LPV5WcLAO+Q0RLX5tKCR&noverify=0&group_code=574737577

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgbgagf
系列文章
更多 icon
同类精品
更多 icon
继续加载