1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3 4# Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd. 5# 6# HDF is dual licensed: you can use it either under the terms of 7# the GPL, or the BSD license, at your option. 8# See the LICENSE file in the root of this repository for complete details. 9 10import os 11import shutil 12from http.server import SimpleHTTPRequestHandler 13import socketserver 14import threading 15from selenium import webdriver 16from selenium.webdriver.common.action_chains import ActionChains 17import time 18import easyocr 19import nltk 20import cv2 21import tempfile 22import pytest 23 24webPath = os.path.realpath("../dist") 25 26 27class MyHttpRequestHandler(SimpleHTTPRequestHandler): 28 def __init__(self, *args, **kwargs): 29 super().__init__(*args, directory=webPath, **kwargs) 30 31 32httpd = socketserver.TCPServer(("", 9999), MyHttpRequestHandler) 33driver = webdriver.Chrome() 34actions = ActionChains(driver) 35reader = easyocr.Reader(['ch_sim', 'en'], verbose=False) 36windowWidth = -1 37windowHeight = -1 38tempImageFile = os.path.join(tempfile.gettempdir(), "test.png") 39 40 41def cut_image(image, x, y, w, h): 42 x = int(x) 43 y = int(y) 44 return image[y:y+h, x:x+w] 45 46 47oldx = 0 48oldy = 0 49 50 51def click_on_page(x, y): 52 global oldx, oldy 53 actions.move_by_offset(x-oldx, y-oldy).click().perform() 54 oldx = x 55 oldy = y 56 time.sleep(1) 57 58 59def setup(): 60 os.chdir("..") 61 62 print("setup : 编译项目") 63 os.system("npm run dist") 64 65 print("setup : 拷贝测试文件") 66 shutil.copy("examples/log_loop.txt", "dist/test.txt") 67 68 print("setup : 启动web服务") 69 threading.Thread(target=httpd.serve_forever).start() 70 71 print("setup : selenium打开测试页面") 72 driver.implicitly_wait(10) 73 driver.get("http://127.0.0.1:9999") 74 global windowWidth, windowHeight 75 windowWidth = driver.execute_script("return document.body.clientWidth") 76 windowHeight = driver.execute_script("return document.body.clientHeight") 77 78 79def teardown(): 80 print("teardown : 关闭selenium") 81 driver.close() 82 83 print("teardown : 关闭web服务") 84 httpd.shutdown() 85 86 87def test_loading(): # 验证载入中画面 88 driver.get_screenshot_as_file(tempImageFile) 89 image = cv2.imread(tempImageFile) 90 result = reader.readtext(cut_image(image, windowWidth/2-100, windowHeight/2-20, 200, 40)) 91 assert result[0][1][:7] == "Loading" 92 93 94def test_start(): # 验证主画面显示 95 time.sleep(5) 96 driver.get_screenshot_as_file(tempImageFile) 97 image = cv2.imread(tempImageFile) 98 result = reader.readtext(cut_image(image, 10, 100, 60, 20)) 99 assert result[0][1] == "隐藏选中" 100 101 102def find_string_in_result(s, result): 103 dis = 999 104 p = -1 105 for i, r in enumerate(result): 106 d = nltk.edit_distance(r[1], s) 107 if d < dis: 108 dis = d 109 p = i 110 if dis < len(s)/2: 111 return result[p] 112 return False 113 114 115def test_selectfunc(): # 点击优化类型切换下拉菜单,选择优化类型 116 click_on_page(420, 50) 117 click_on_page(420, 150) 118 119 driver.get_screenshot_as_file(tempImageFile) 120 image = cv2.imread(tempImageFile) 121 result = reader.readtext(image) 122 ret = find_string_in_result("0,CIRCUIT_ROOT", result) 123 assert ret != False 124 125 126def test_hide(): # 点击state和root按钮,隐藏0,CIRCUIT_ROOT 127 click_on_page(40, 80) 128 click_on_page(350, 80) 129 130 driver.get_screenshot_as_file(tempImageFile) 131 image = cv2.imread(tempImageFile) 132 result = reader.readtext(image) 133 ret = find_string_in_result("0,CIRCUIT_ROOT", result) 134 assert ret == False 135 136 137def test_wait(): 138 time.sleep(10) 139