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的网页自动化封装分层思想
标签:exe,description,lackey,pattern,self,param,path,软件 From: https://www.cnblogs.com/chevron123/p/18487025