桌面軟體/exe程式軟體自動化大寶劍--lackey之二次封裝以及selenium模仿

快乐的平头哥發表於2024-10-20
  1 # lackey 二次封裝
  2 
  3 class lackeyAtion(object):
  4 
  5     #初始化,有需要再加
  6     def __init__(self):
  7         self.lackey = lackey.Screen(0)
  8         self.lackey.setAutoWaitTimeout(30)
  9 
 10     #截圖需要儲存路徑,截圖的名字
 11     def screen_shot(self,path,name):
 12         try:
 13             path = path.decode('utf-8')
 14             name = name.decode('utf-8')
 15             self.lackey.saveScreenCapture(path,name)
 16         except:
 17             logging.exception("screeshoot fail!!!")
 18             raise
 19 
 20     #等待顯現
 21     def wait_appear(self,pattern,description,path):
 22         try:
 23             if self.lackey.wait(pattern):
 24                 logging.info("is appear{}".format(description))
 25                 return True
 26         except:
 27             logging.exception("is not appear{}".format(description))
 28             self.screen_shot(path,description)
 29             raise
 30 
 31 
 32     #查詢匹配 依賴flag_loc規則
 33     def find_match(self,pattern,description,path,name=None,flag_loc=None):
 34         """
 35         :param pattern:  要找區域
 36         :param description: 描述
 37         :param path: 圖片儲存的位置
 38         :param name: 參照描述
 39         :param flag_loc: 參照區域
 40         :return:
 41         """
 42         """根據flag_loc值判斷是否需要參照區域"""
 43         if flag_loc and name is None:
 44             try:
 45                 if self.lackey.find(pattern):
 46                     logging.info("is find  {}".format(description))
 47                     return True
 48                 else:
 49                     logging.info("is not find  {}".format(description))
 50                     return False
 51             except:
 52                 logging.exception("find {} fail !!!".format(description))
 53                 self.screen_shot(path,description)
 54                 raise
 55         else:
 56             """否則先等待參照區域顯現"""
 57             try:
 58                 if self.wait_appear(pattern=flag_loc,description=name,path=path):
 59                     try:
 60                         if self.lackey.find(pattern):
 61                              return True
 62                         else:
 63                             logging.info("is not find  {}".format(description))
 64                     except:
 65                         logging.exception("find {} fail !!!".format(description))
 66                         self.screen_shot(path, description)
 67             except:
 68                 logging.exception("wait {} goal fail!!!".format(name))
 69                 self.screen_shot(path, name)
 70                 raise
 71 
 72     # 點選 依賴是否需要time等待
 73     def click_left(self,pattern,description,path,timeFlag):
 74         """
 75         :param pattern:  要找區域
 76         :param description: 要找區域描述
 77         :param path: 圖片儲存位置
 78         :param timeFlag: 給等待時間
 79         :return:
 80         """
 81 
 82         if timeFlag is None:
 83             try:
 84                 self.lackey.click(pattern)
 85             except:
 86                 logging.exception("click {} fail!!!".format(description))
 87                 self.screen_shot(path,description)
 88                 raise
 89         else:
 90             try:
 91                 time.sleep(timeFlag)
 92                 self.lackey.click(pattern)
 93             except:
 94                 logging.exception("click {} fail!!!".format(description))
 95                 self.screen_shot(path,description)
 96                 raise
 97     # 按照偏移量進行點選操作
 98     def click_targetOffset(self,pattern,description,path,timeFlag,x,y):
 99         """
100         :param pattern:  要找區域
101         :param description: 要找區域描述
102         :param path: 圖片儲存位置
103         :param timeFlag: 給等待時間
104         :param positon: 座標x,y
105         :return:
106         """
107         if timeFlag is None:
108             try:
109                 self.lackey.click(lackey.Pattern(pattern).targetOffset(x,y))
110             except:
111                 logging.exception("click {} fail!!!".format(description))
112                 self.screen_shot(path,description)
113                 raise
114         else:
115             try:
116                 time.sleep(timeFlag)
117                 self.lackey.click(lackey.Pattern(pattern).targetOffset(x,y))
118             except:
119                 logging.exception("click {} fail!!!".format(description))
120                 self.screen_shot(path,description)
121                 raise
122 
123     # 雙擊 依賴是否需要time等待
124     def doubleClick(self,pattern,description,path,timeFlag):
125         """
126         :param pattern:  要找區域
127         :param description: 要找區域描述
128         :param path: 圖片儲存位置
129         :param timeFlag: 給等待時間
130         :return:
131         """
132         if timeFlag is None:
133             try:
134                 self.lackey.doubleClick(pattern)
135             except:
136                 logging.exception("dobuleclick {} fail!!!".format(description))
137                 self.screen_shot(path,description)
138                 raise
139         else:
140             try:
141                 time.sleep(timeFlag)
142                 self.lackey.doubleClick(pattern)
143             except:
144 
145                 logging.exception("dobuleclick {} fail!!!".format(description))
146                 self.screen_shot(path,description)
147                 raise
148 
149     # 按照偏移量進行雙擊操作
150     def doubleClick_targetOffset(self,pattern,description,path,timeFlag,x,y):
151         """
152         :param pattern:  要找區域
153         :param description: 要找區域描述
154         :param path: 圖片儲存位置
155         :param timeFlag: 給等待時間
156         :param positon: 座標x,y
157         :return:
158         """
159         if timeFlag is None:
160             try:
161                 self.lackey.doubleClick(lackey.Pattern(pattern).targetOffset(x,y))
162             except:
163                 logging.exception("dobuleclick {} fail!!!".format(description))
164                 self.screen_shot(path,description)
165                 raise
166         else:
167             try:
168                 time.sleep(timeFlag)
169                 self.lackey.doubleClick(lackey.Pattern(pattern).targetOffset(x,y))
170             except:
171                 logging.exception("dobuleclick {} fail!!!".format(description))
172                 self.screen_shot(path,description)
173                 raise
174 
175     # 右鍵
176     def click_right(self,pattern,description,path,timeFlag):
177         """
178         :param pattern:  要找區域
179         :param description: 要找區域描述
180         :param path: 圖片儲存位置
181         :param name: 參照描述
182         :param timeFlag: 給等待時間
183         :return:
184         """
185         if timeFlag is None:
186             try:
187                 self.lackey.rightClick(pattern)
188             except:
189                 logging.exception("click {} fail!!!".format(description))
190                 self.screen_shot(path,description)
191                 raise
192         else:
193             try:
194                 time.sleep(timeFlag)
195                 self.lackey.rightClick(pattern)
196             except:
197 
198                 logging.exception("click {} fail!!!".format(description))
199                 self.screen_shot(path,description)
200                 raise
201 
202     # 拖拽 依賴是否需要time等待
203     def drag_appear(self,description,path,name,dragFrom,timeFlag,dragTo=None):
204         """
205         :param description: 要找區域描述
206         :param path: 圖片儲存位置
207         :param name: 圖片描述
208         :param dragFrom: 拖動起始位置
209         :param dragTo: 拽到終止位置
210         :param timeFlag: 給等待時間
211         :return:
212         """
213 
214         if timeFlag is None:
215             try:
216                 self.lackey.dragDrop(dragFrom,dragTo)
217             except:
218                 logging.exception("drag {} fail!!!".format(description))
219                 self.screen_shot(path,name)
220                 raise
221         else:
222             try:
223                 time.sleep(timeFlag)
224                 self.lackey.dragDrop(dragFrom,dragTo)
225             except:
226 
227                 logging.exception("drag {} fail!!!".format(description))
228                 self.screen_shot(path,name)
229                 raise
230     def input(self,description,path,content):
231         try:
232             self.lackey.type(content)
233             logging.info("input {}".format(content))
234         except:
235             logging.exception("input {} fail!!!".format(description))
236             self.screen_shot(path,description)
237             raise
238 
239 
240     # 下拉選單,選單區域-選單選項
241     def selector(self,pattern,name1,loc,description,path,steps,scroll_num):
242         """
243         :param pattern: 選單區域
244         :param name1: 選單區域描述
245         :param loc: 選單選項
246         :param description: 選單選項描述
247         :param path: 圖片儲存位置
248         :param steps: 滾動步數
249         :param scrop_num: 滾動次數
250         :return:
251         """
252 
253         try:
254             self.lackey.click(pattern)
255             try:
256                 if self.wait_appear(pattern=loc,description=description,path=path):
257                     try:
258                         self.lackey.click(loc)
259                     except:
260                         logging.exception("click {} fail!!!".format(description))
261                         self.screen_shot(path,description)
262                         raise
263                 else:
264                     for i in range(scroll_num):
265                         self.lackey.wheel(1, steps)
266                         if self.wait_appear(pattern=loc,description=description,path=path):
267                             try:
268                                 self.lackey.click(loc)
269                                 break
270                             except:
271                                 logging.exception("click {} fail!!!".format(description))
272                                 self.screen_shot(path,description)
273                                 raise
274                         logging.info("finding {}".format(description))
275             except:
276                 logging.exception("wait {}is not appear}".format(description))
277                 self.screen_shot(path,description)
278                 raise
279         except:
280             logging.exception("click{} fail!!!".format(name1))
281             self.screen_shot(path,name1)
282             raise
283 
284     #上傳檔案
285     def upload(self, pattern, file_path, path, description, x,y,positon=None):
286         """
287         :param pattern: 上傳區域/參照區域
288         :param file_path: 檔案路徑
289         :param path: 圖片儲存位置
290         :param description: 描述
291         :param positon: 決定是否偏移量座標 x,y
292         :return:
293         """
294         if positon is None:
295             try:
296                 self.lackey.click(pattern)
297 
298                 self.lackey.type(file_path)
299                 time.sleep(1.5)
300                 self.lackey.type("{ENTER}{ENTER}{ENTER}")
301 
302             except:
303                 logging.exception("upload {} fail !!")
304                 self.screen_shot(path, description)
305                 raise
306         else:
307             try:
308                 self.lackey.click(lackey.Pattern(pattern).targetOffset(x,y))
309                 self.lackey.type(file_path)
310                 time.sleep(1.5)
311                 self.lackey.type("{ENTER}{ENTER}{ENTER}")
312             except:
313                 logging.exception("upload {} fail !!")
314                 self.screen_shot(path, description)
315                 raise

以上是一些基本操作,不足之處後續部落格會繼續完善

這裡附上模仿selenium的網頁自動化封裝分層思想

相關文章