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