1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 # Copyright (c) 2022 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 
10 from re import finditer
11 import os
12 import time
13 
14 import easyocr
15 
16 from PIL import Image
17 from selenium.webdriver.common.keys import Keys
18 from selenium.webdriver.support.wait import WebDriverWait
19 from selenium.webdriver.support import expected_conditions as EC
20 from selenium.webdriver.common.action_chains import ActionChains
21 
22 
23 class Base:
24 
25     def __init__(self, driver):
26         self.driver = driver
27         self.timeout = 10
28         self.t = 0.5
29 
30     def find_element(self, locator):
31         if not isinstance(locator, tuple):
32             print('The type of the locator parameter is tuple')
33         else:
34             ele = WebDriverWait(self.driver, self.timeout, self.t).until(
35                 EC.presence_of_element_located(locator))
36             return ele
37 
38     # Get URL address
39     @staticmethod
40     def getfiledir():
41         os.chdir('../../')
42         cur_file = os.getcwd()
43         path = cur_file.replace('\\', '/')
44         url = "file:///{}/index.html".format(path)
45         os.chdir('test/testsuits')
46         return url
47 
48     # Enter text information
49     def sendkeys(self, locator, text=''):
50         ele = self.find_element(locator)
51         ele.clear()
52         ele.send_keys(text)
53 
54     def click(self, locator):
55         ele = self.find_element(locator)
56         ele.click()
57 
58     def get_attribute(self, locator, name):
59         try:
60             element = self.find_element(locator)
61             return element.get_attribute(name)
62         except BaseException:
63             return ""
64         finally:
65             pass
66 
67     def click_canvas_point(self, el, x, y):
68         actions = ActionChains(self.driver)
69         actions.move_to_element_with_offset(el, x, y).click().perform()
70 
71     def click_canvas_by_text(self, text):
72         # text:Click on the text
73         el = self.find_element(('id', 'visual_area'))
74         loc = self.get_canvas_point_position('./Program_pictures/quanping.png',
75                                              text)
76         actions = ActionChains(self.driver)
77         actions.move_to_element_with_offset(el, loc[0],
78                                             loc[1]).click().perform()
79 
80     def canvas_copy(self):
81         actions = ActionChains(self.driver)
82         actions.key_down(Keys.CONTROL).send_keys('c').key_up(
83             Keys.CONTROL).perform()
84 
85     def canvas_paste(self):
86         actions = ActionChains(self.driver)
87         actions.key_down(Keys.CONTROL).send_keys('v').key_up(
88             Keys.CONTROL).perform()
89 
90     def get_canvas_point_position(self, path, text):
91         # path:Storage address of screenshot   text:The text content to look up
92         self.driver.get_screenshot_as_file(path)
93         reader = easyocr.Reader(['ch_sim', 'en'], verbose=False)
94         result = reader.readtext(path)
95         print("############", result)
96         loc_x_min = loc_x_max = loc_y_min = loc_y_max = 0
97         for i in result:
98             # The text in the recognition figure is prone to case error.
99             # Compare the two texts in lower case to avoid the problem
100             if i[1].lower() == text.lower():
101                 loc_x_min = i[0][0][0]
102                 loc_x_max = i[0][1][0]
103                 loc_y_min = i[0][0][1]
104                 loc_y_max = i[0][2][1]
105                 break
106         loc_x = (loc_x_min + loc_x_max) / 2
107         loc_y = (loc_y_min + loc_y_max) / 2
108         return (loc_x, loc_y)
109 
110     def get_obtain_point_position(self, path, text):
111         # path:Storage address of screenshot   text:The text content to look up
112         self.driver.get_screenshot_as_file(path)
113         reader = easyocr.Reader(['ch_sim', 'en'], verbose=False)
114         result = reader.readtext(path)
115         print("************************", result)
116         loc_x_min = loc_x_max = loc_y_min = loc_y_max = 0
117         for i in result:
118             # The text in the recognition figure is prone to case error.
119             # Compare the two texts in lower case to avoid the problem
120             if text in i[1].lower():
121                 loc_x_min = i[0][0][0]
122                 loc_x_max = i[0][1][0]
123                 loc_y_min = i[0][0][1]
124                 loc_y_max = i[0][2][1]
125                 break
126         loc_x = (loc_x_min + loc_x_max) / 2
127         loc_y = (loc_y_min + loc_y_max) / 2
128         return (loc_x, loc_y)
129 
130     def add_node(self, text):
131         # text:Name of the child node to be added
132         el = self.find_element(('id', 'visual_area'))
133         loc = self.get_canvas_point_position('./Program_pictures/quanping.png',
134                                              text)
135         self.click_canvas_point(el, loc[0], loc[1])
136         time.sleep(1)
137         self.click(('id', 'add_child_node'))
138         loc1 = self.get_canvas_point_position('./Program_pictures/addnode.png',
139                                               'node')
140         self.click_canvas_point(el, loc1[0], loc1[1])
141         return loc1
142 
143     def add_attr(self, text):
144         # text:Name of the child attr to be added
145         el = self.find_element(('id', 'visual_area'))
146         loc = self.get_canvas_point_position('./Program_pictures/quanping.png',
147                                              text)
148         self.click_canvas_point(el, loc[0], loc[1])
149         self.click(('id', 'add_child_attr'))
150         self.driver.get_screenshot_as_file('./Program_pictures/addattr.png')
151         reader = easyocr.Reader(['ch_sim', 'en'], verbose=False)
152         result = reader.readtext('./Program_pictures/addattr.png')
153         loc_x_min = loc_x_max = loc_y_min = loc_y_max = 0
154         for i in result:
155             # Query the location of the node whose initial letter is A (there is no node whose initial letter
156             # is A on the current page, but the name of the node whose new attribute is attr_1).
157             if 'a' == i[1].lower()[0]:
158                 loc_x_min = i[0][0][0]
159                 loc_x_max = i[0][1][0]
160                 loc_y_min = i[0][0][1]
161                 loc_y_max = i[0][2][1]
162                 break
163         loc_x = (loc_x_min + loc_x_max) / 2
164         loc_y = (loc_y_min + loc_y_max) / 2
165         self.click_canvas_point(el, loc_x, loc_y)
166         return (loc_x, loc_y)
167 
168     def node_in_kuang(self, path, text):
169         # path:Storage address of screenshot   text:The text content to look up
170         self.driver.get_screenshot_as_file(path)
171         reader = easyocr.Reader(['ch_sim', 'en'], verbose=False)
172         result = reader.readtext(path)
173         flag = False
174         for i in result:
175             if i[1].lower() == text:
176                 flag = True
177                 break
178         return flag
179 
180     def kuang_contain_text(self, path, text):
181         # path:Storage address of screenshot   text:The text content to look up
182         self.driver.get_screenshot_as_file(path)
183         reader = easyocr.Reader(['ch_sim', 'en'], verbose=False)
184         result = reader.readtext(path)
185         flag = False
186         for i in result:
187             if text in i[1]:
188                 flag = True
189                 break
190         return flag
191 
192     def text_count(self, path, text):
193         # path:Storage address of screenshot   text:The text content to look up
194         self.driver.get_screenshot_as_file(path)
195         size = self.driver.get_window_size()
196         size.get('width')
197         Image.open(path).crop(
198             (0.1 * size.get('width'), 0, 0.6 * size.get('width'),
199              0.4 * size.get('height'))).save(path)
200         reader = easyocr.Reader(['ch_sim', 'en'], verbose=False)
201         result = reader.readtext(path)
202         count = 0
203         for i in result:
204             if text in i[1]:
205                 count = count + 1
206         return count
207