Best Python code snippet using uiautomator
window.py
Source:window.py
1"""2 project............: Handgurke3 description........: ICB client4 date...............: 06/20195 copyright..........: Sebastian Fedrau6 Permission is hereby granted, free of charge, to any person obtaining7 a copy of this software and associated documentation files (the8 "Software"), to deal in the Software without restriction, including9 without limitation the rights to use, copy, modify, merge, publish,10 distribute, sublicense, and/or sell copies of the Software, and to11 permit persons to whom the Software is furnished to do so, subject to12 the following conditions:13 The above copyright notice and this permission notice shall be14 included in all copies or substantial portions of the Software.15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,16 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.18 IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR19 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,20 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR21 OTHER DEALINGS IN THE SOFTWARE.22"""23import curses24from textwrap import wrap25from datetime import datetime26import ui27class ViewModel:28 def __init__(self):29 self.__title = (False, "")30 self.__time = (False, "")31 self.__text = (False, "")32 self.__messages = []33 self.__message_count = 034 @property35 def title(self):36 return self.__title[1]37 @title.setter38 def title(self, value):39 if value != self.__title[1]:40 self.__title = (True, value)41 @property42 def title_changed(self):43 return self.__title[0]44 @property45 def time(self):46 return self.__time[1]47 @time.setter48 def time(self, value):49 if value != self.__time[1]:50 self.__time = (True, value)51 @property52 def time_changed(self):53 return self.__time[0]54 @property55 def text(self):56 return self.__text[1]57 @text.setter58 def text(self, value):59 if value != self.__text[1]:60 self.__text = (True, value)61 @property62 def text_changed(self):63 return self.__text[0]64 @property65 def messages(self):66 return self.__messages67 def append_message(self, timestamp, message_type, fields):68 self.__messages.append((timestamp, message_type, fields))69 @property70 def messages_changed(self):71 return self.__message_count != len(self.__messages)72 @property73 def changed(self):74 return self.title_changed or self.time_changed or self.text_changed or self.messages_changed75 def sync(self):76 self.__title = (False, self.__title[1])77 self.__time = (False, self.__time[1])78 self.__text = (False, self.__text[1])79 self.__message_count = len(self.__messages)80class Window:81 def __init__(self, stdscr, model: ViewModel):82 self.__model = model83 self.__stdscr = stdscr84 self.__draw_screen = True85 self.__text_pos = 086 self.__text_offset = 087 self.__display_lines = 088 self.__next_line = 089 self.__scroll_to = 090 @property91 def model(self):92 return self.__model93 def send_key(self, ch):94 if isinstance(ch, int):95 if ch == curses.KEY_RESIZE:96 self.__draw_screen = True97 elif ch in ["\b", 127, curses.KEY_BACKSPACE]:98 self.__backspace__()99 elif ch == curses.KEY_DC:100 self.__delete_char__()101 elif ch == curses.KEY_LEFT:102 self.__move_left__()103 elif ch == curses.KEY_RIGHT:104 self.__move_right__()105 elif ch == curses.KEY_HOME:106 self.__move_home__()107 elif ch == curses.KEY_END:108 self.__move_end__()109 elif ch == curses.KEY_PPAGE:110 self.__scroll_up__()111 elif ch == curses.KEY_NPAGE:112 self.__scroll_down__()113 else:114 if ch == "\u007f":115 self.__backspace__()116 else:117 key_name = curses.keyname(ord(ch)).decode("UTF-8")118 if len(key_name) == 1 or (len(key_name) == 3 and key_name.startswith("M-")):119 self.__insert_char__(ch)120 elif key_name == "^A":121 self.__move_home__()122 elif key_name == "^E":123 self.__move_end__()124 elif key_name == "^W":125 self.__delete_word__()126 def __backspace__(self):127 index = self.__text_offset + self.__text_pos128 if index > 0:129 self.__model.text = "%s%s" % (self.__model.text[:index - 1], self.__model.text[index:])130 if self.__text_pos == self.__x - 1 and self.__text_offset < 0:131 self.__text_offset += 1132 else:133 self.__text_pos -= 1134 self.__refresh_bottom__(force=True)135 self.__model.sync()136 def __delete_char__(self):137 index = self.__text_offset + self.__text_pos138 if index < len(self.__model.text):139 self.__model.text = "%s%s" % (self.__model.text[:index], self.__model.text[index + 1:])140 def __insert_char__(self, ch):141 text = self.__model.text142 split = self.__text_offset + self.__text_pos143 self.__model.text = "%s%s%s" % (text[:split], ch, text[split:])144 if self.__text_pos == self.__x - 1:145 self.__text_offset += 1146 else:147 self.__text_pos += 1148 self.__refresh_bottom__(force=True)149 self.__model.sync()150 def __delete_word__(self):151 index = self.__text_offset + self.__text_pos152 if index > 0:153 text = self.__model.text154 match = text.rfind(" ", 0, index - 1)155 if match == 0:156 self.__model.text = ""157 self.__text_pos = 0158 self.__text_offset = 0159 else:160 self.__model.text = "%s%s" % (text[:match + 1], text[index:])161 diff = len(text) - len(self.__model.text)162 self.__text_pos -= diff163 if len(self.__model.text) < self.__x - 1:164 self.__text_pos += self.__text_offset165 self.__text_offset = 0166 self.__refresh_bottom__(force=True)167 self.__model.sync()168 def __move_left__(self):169 if self.__text_pos > 0:170 self.__text_pos -= 1171 self.__bottom.move(0, self.__text_pos)172 self.__bottom.refresh()173 elif self.__text_offset > 0:174 self.__text_offset -= 1175 self.__refresh_bottom__(force=True)176 def __move_right__(self):177 if self.__text_pos + self.__text_offset < len(self.__model.text):178 if self.__text_pos < self.__x - 1:179 self.__text_pos += 1180 self.__bottom.move(0, self.__text_pos)181 self.__bottom.refresh()182 else:183 self.__text_offset += 1184 self.__refresh_bottom__(force=True)185 def __move_home__(self):186 self.__text_pos = 0187 self.__text_offset = 0188 self.__bottom.move(0, 0)189 self.__refresh_bottom__(force=True)190 def __move_end__(self):191 self.__text_pos = len(self.__model.text)192 if self.__text_pos > self.__x:193 self.__text_offset = self.__text_pos - self.__x + 1194 self.__text_pos = self.__x - 1195 else:196 self.__text_offset = 0197 self.__bottom.move(0, self.__text_pos)198 self.__refresh_bottom__(force=True)199 def __scroll_up__(self):200 if self.__scroll_to < self.__display_lines - (self.__y - 2):201 self.__scroll_to += 1202 self.__refresh_lines__(force=True)203 self.__refresh_bottom__(force=True)204 def __scroll_down__(self):205 if self.__scroll_to > 0:206 self.__scroll_to -= 1207 self.__refresh_lines__(force=True)208 self.__refresh_bottom__(force=True)209 def clear(self):210 self.__stdscr.clear()211 self.__stdscr.refresh()212 def refresh(self):213 try:214 force = self.__draw_screen215 if self.__create_screen__():216 refreshed = self.__refresh_top__(force)217 218 if self.__refresh_lines__(force):219 refreshed = True220 self.__refresh_bottom__(force or refreshed)221 self.__model.sync()222 else:223 self.clear()224 except:225 self.__draw_screen = True226 def __create_screen__(self):227 drawn = True228 if self.__draw_screen:229 self.__stdscr.clear()230 self.__stdscr.refresh()231 y, x = self.__stdscr.getmaxyx()232 self.__y = y233 self.__x = x234 if self.__x >= 20 and self.__y >= 10:235 self.__top = curses.newwin(1, self.__x, 0, 0)236 self.__top.bkgd(' ', curses.color_pair(ui.COLORS_TITLE_BAR))237 self.__lines = curses.newpad(20, self.__x)238 self.__lines.bkgd(' ', curses.color_pair(ui.COLORS_MESSAGE))239 self.__display_lines = 0240 self.__next_line = 0241 self.__scroll_to = 0242 self.__bottom = curses.newwin(1, self.__x, self.__y - 1, 0)243 self.__bottom.bkgd(' ', curses.color_pair(ui.COLORS_INPUT))244 self.__draw_screen = False245 else:246 drawn = False247 return drawn248 def __refresh_top__(self, force):249 refreshed = False250 if self.__model.title_changed or self.__model.time_changed or force:251 refreshed = True252 self.__top.clear()253 title = self.__model.title254 if len(title) > self.__x - 16:255 title = "%s..." % title[:self.__x - 10]256 fmt = "%-" + str(self.__x - 6) + "s%5s"257 title = fmt % (title, self.__model.time)258 self.__top.addstr(0, 0, title)259 self.__top.refresh()260 return refreshed261 def __refresh_lines__(self, force):262 refreshed = False263 if self.__model.messages_changed or force:264 refreshed = True265 max_y, max_x = self.__lines.getmaxyx()266 old_lines = self.__display_lines267 for timestamp, message_type, fields in self.__model.messages[self.__next_line:]:268 if self.__display_lines >= max_y:269 max_y *= 2270 self.__lines.resize(max_y, max_x)271 padding = self.__write_prefix__(self.__display_lines, timestamp, message_type, fields)272 first_line = True273 colors = ui.COLORS_MESSAGE274 if message_type == "i":275 colors = ui.COLORS_OUTPUT276 for l in self.__convert_message__(self.__x - padding, message_type, fields):277 if self.__display_lines >= max_y:278 max_y *= 2279 self.__lines.resize(max_y, max_x)280 if first_line: 281 self.__lines.addstr(l, curses.color_pair(colors))282 first_line = False283 else:284 self.__lines.addstr(self.__display_lines, 0, " " * padding, ui.COLORS_MESSAGE)285 self.__lines.addstr(l, curses.color_pair(colors))286 self.__display_lines += 1287 self.__next_line += 1288 if self.__scroll_to == 0:289 scroll_to = self.__display_lines - self.__y + 2290 self.__lines.refresh(scroll_to, 0, 1, 0, self.__y - 2, self.__x)291 else:292 self.__scroll_to += self.__display_lines - old_lines293 scroll_to = self.__display_lines - self.__y + 2 - self.__scroll_to294 self.__lines.refresh(scroll_to, 0, 1, 0, self.__y - 2, self.__x)295 return refreshed296 def __write_prefix__(self, row, timestamp, message_type, fields):297 length = 9298 time = timestamp.strftime("%H:%M:%S")299 self.__lines.addstr(row, 0, time, curses.color_pair(ui.COLORS_TIMESTAMP))300 self.__lines.addstr(" ", curses.color_pair(ui.COLORS_MESSAGE))301 if message_type == "b":302 length += len(fields[0]) + 3303 self.__lines.addstr("<%s>" % fields[0], curses.color_pair(ui.COLORS_NICK))304 self.__lines.addstr(" ", curses.color_pair(ui.COLORS_MESSAGE))305 elif message_type == "c":306 length += len(fields[0]) + 3307 self.__lines.addstr("*%s*" % fields[0], curses.color_pair(ui.COLORS_PERSONAL) | curses.A_BOLD)308 self.__lines.addstr(" ", curses.color_pair(ui.COLORS_MESSAGE))309 elif message_type == "d":310 length += len(fields[0]) + 3311 self.__lines.addstr("[%s]" % fields[0], curses.color_pair(ui.COLORS_STATUS))312 self.__lines.addstr(" ", curses.color_pair(ui.COLORS_MESSAGE))313 elif message_type == "e":314 length += 6315 self.__lines.addstr("*ERR*", curses.color_pair(ui.COLORS_ERROR))316 self.__lines.addstr(" ", curses.color_pair(ui.COLORS_MESSAGE))317 elif message_type == "f":318 length += len(fields[0]) + 3319 self.__lines.addstr("[%s]" % fields[0], curses.color_pair(ui.COLORS_IMPORTANT))320 self.__lines.addstr(" ", curses.color_pair(ui.COLORS_MESSAGE))321 elif message_type == "k":322 length += 7323 self.__lines.addstr("*BEEP*", curses.color_pair(ui.COLORS_PERSONAL) | curses.A_BOLD)324 self.__lines.addstr(" ", curses.color_pair(ui.COLORS_MESSAGE))325 return length326 def __convert_message__(self, max_length, message_type, fields):327 lines = []328 if message_type in "bcdf":329 lines = wrap(fields[1], max_length)330 elif message_type == "e":331 lines = wrap(fields[0], max_length)332 elif message_type == "k":333 lines = ["%s beeps you." % fields[0]]334 elif message_type == "i":335 if fields[0] == "co":336 lines = wrap(fields[1], max_length)337 elif fields[0] == "wl":338 status = fields[8]339 if status:340 status = " %s" % status341 l = " %1s %-16s %4s %-8s %s@%s%s" % ("*" if fields[1] else "",342 fields[2],343 self.__idle_str__(int(fields[3])),344 datetime.fromtimestamp(int(fields[5])).strftime("%X"),345 fields[6],346 fields[7],347 status)348 lines = wrap(l, max_length)349 return lines350 @staticmethod351 def __idle_str__(elapsed):352 total_seconds = int(elapsed)353 total_minutes = int(total_seconds / 60)354 total_hours = int(total_minutes / 60)355 minutes = total_minutes - (total_hours * 60)356 parts = []357 if total_hours > 23:358 days = int(total_hours / 24)359 parts.append("%dd" % days)360 hours = total_hours - (days * 24)361 if hours > 0:362 parts.append("%dh" % hours)363 if minutes > 0:364 parts.append("%dm" % minutes)365 elif total_hours > 0:366 parts.append("%dh" % total_hours)367 if minutes > 0:368 parts.append("%dm" % minutes)369 elif total_minutes > 0:370 parts.append("%dm" % minutes)371 else:372 parts.append("%ds" % total_seconds)373 return "".join(parts)374 def __refresh_bottom__(self, force):375 if self.__model.text_changed or force:376 if self.__text_pos + self.__text_offset > len(self.__model.text):377 self.__text_offset = 0378 self.__text_pos = len(self.__model.text)379 text = self.__model.text[self.__text_offset:]380 text = text[:self.__x - 1]381 text_len = len(text)382 text = text + " " * (self.__x - len(text) - 1)383 self.__bottom.addstr(0, 0, text)384 self.__bottom.move(0, self.__text_pos)...
settings_activity_page.py
Source:settings_activity_page.py
...12 return list_of_elems13 def settings_elements_present(self):14 checkable_list = self._get_checkable_elements_list()15 return checkable_list is not []16 def __scroll_to(self, element):17 """Auxiliary: scrolling down till the @element appears"""18 MAX_SCROLLS_ALLOWED = 6 # to prevent infinite loop in case we can't find the @element19 x, y = TestUtils.get_screen_dimensions(self.driver)20 # speeding up a little:21 self.driver.implicitly_wait(2)22 # scrolling down till the INFO button appears:23 found = False24 scroll_number = 025 while not found:26 try:27 el_we_seek = self.driver.find_element_by_id(element)28 found = True29 except NoSuchElementException:30 self.driver.swipe(x / 2, y * 0.9, x / 2, y * 0.1, 1000)31 scroll_number += 132 if scroll_number > MAX_SCROLLS_ALLOWED:33 raise NoSuchElementException(f"Cant't find element. Too many scrolls performed ({scroll_number}) "34 f"while {MAX_SCROLLS_ALLOWED} allowed")35 # restoring timeout:36 self.driver.implicitly_wait(TestUtils.WAIT_TIME)37 return el_we_seek38 def get_info_button(self):39 binfo = self.__scroll_to(SAL.BINFO_ID)40 return binfo41 def get_cb_nazwa(self):42 cb = self.__scroll_to(SAL.CB_NAZWA_ID)43 return cb44 def get_rb_nopicture(self):45 rb = self.__scroll_to(SAL.RB_NOPICTURE_ID)...
LineJumper.py
Source:LineJumper.py
...12 def __destroy(self):13 self.disconnect()14 del self15 return False16 def __scroll_to(self, line):17 iterator = self.__editor.textbuffer.get_iter_at_line(line)18 self.__editor.textbuffer.place_cursor(iterator)19 self.__editor.textview.scroll_to_iter(iterator, 0.001, use_align=True, xalign=1.0)20 return False21 def __destroy_cb(self, *args):22 self.__destroy()23 return False24 def __scroll_cb(self, manager, line):25 from gobject import idle_add26 idle_add(self.__scroll_to, line)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!