Best Python code snippet using pyatom_python
__init__.py
Source:__init__.py
...184def logFailures(*args):185 # Do nothing. For backward compatability186 warnings.warn('Use Mago framework - http://mago.ubuntu.com', DeprecationWarning)187 pass188def _populateNamespace(d):189 for method in client._client.system.listMethods():190 if method.startswith('system.'):191 continue192 if method in d:193 local_name = '_remote_' + method194 else:195 local_name = method196 d[local_name] = getattr(client._client, method)197 d[local_name].__doc__ = client._client.system.methodHelp(method)198class PollEvents:199 """200 Class to poll callback events, NOTE: *NOT* for external use201 """202 def __init__(self):203 self._stop = False204 # Initialize callback dictionary205 self._callback = {}206 def __del__(self):207 """208 Stop callback when destroying this class209 """210 self._stop = True211 def run(self):212 while not self._stop:213 try:214 if not self.poll_server():215 # Socket error216 break217 except:218 log(traceback.format_exc())219 self._stop = False220 break221 def poll_server(self):222 if not self._callback:223 # If callback not registered, don't proceed further224 # Sleep a second and then return225 time.sleep(1)226 return True227 try:228 event = poll_events()229 except socket.error:230 log(traceback.format_exc())231 # Connection to server might be failed232 return False233 if not event:234 # No event in queue, sleep a second235 time.sleep(1)236 return True237 # Event format:238 # window:create-Untitled Document 1 - gedit239 event = event.split('-', 1) # Split first -240 data = event[1] # Rest of data241 event_type = event[0] # event type242 # self._callback[name][0] - Event type243 # self._callback[name][1] - Callback function244 # self._callback[name][2] - Arguments to callback function245 for name in self._callback:246 # Window created event247 # User registered window events248 # Keyboard event249 if (event_type == "onwindowcreate" and \250 re.match(glob_trans(name), data, re.M | re.U | re.L)) or \251 (event_type != "onwindowcreate" and \252 self._callback[name][0] == event_type) or \253 event_type == 'kbevent':254 if event_type == 'kbevent':255 # Special case256 keys, modifiers = data.split('-')257 fname = 'kbevent%s%s' % (keys, modifiers)258 else:259 fname = name260 # Get the callback function261 callback = self._callback[fname][1]262 if not callable(callback):263 # If not callable, ignore the event264 continue265 args = self._callback[fname][2]266 try:267 if len(args) and args[0]:268 # Spawn a new thread, for each event269 # If one or more arguments to the callback function270 thread.start_new_thread(callback, args)271 else:272 # Spawn a new thread, for each event273 # No arguments to the callback function274 thread.start_new_thread(callback, ())275 except:276 # Log trace incase of exception277 log(traceback.format_exc())278 # Silently ignore !?! any exception thrown279 pass280 # When multiple kb events registered, the for281 # loop keeps iterating, so just break the loop282 break283 return True284def imagecapture(window_name = None, out_file = None, x = 0, y = 0,285 width = None, height = None):286 """287 Captures screenshot of the whole desktop or given window288 @param window_name: Window name to look for, either full name,289 LDTP's name convention, or a Unix glob.290 @type window_name: string291 @param x: x co-ordinate value292 @type x: integer293 @param y: y co-ordinate value294 @type y: integer295 @param width: width co-ordinate value296 @type width: integer297 @param height: height co-ordinate value298 @type height: integer299 @return: screenshot filename300 @rtype: string301 """302 if not out_file:303 out_file = tempfile.mktemp('.png', 'ldtp_')304 else:305 out_file = os.path.expanduser(out_file)306 307 ### Windows compatibility308 if _ldtp_windows_env:309 if width == None:310 width = -1311 if height == None:312 height = -1313 if window_name == None:314 window_name = ''315 ### Windows compatibility - End316 data = _remote_imagecapture(window_name, x, y, width, height)317 f = open(out_file, 'wb')318 f.write(b64decode(data))319 f.close()320 return out_file321def wait(timeout=5):322 return _remote_wait(timeout)323def waittillguiexist(window_name, object_name = '',324 guiTimeOut = 30, state = ''):325 return _remote_waittillguiexist(window_name, object_name,326 guiTimeOut)327def waittillguinotexist(window_name, object_name = '',328 guiTimeOut = 30, state = ''):329 return _remote_waittillguinotexist(window_name, object_name,330 guiTimeOut)331def guiexist(window_name, object_name = ''):332 return _remote_guiexist(window_name, object_name)333def launchapp(cmd, args = [], delay = 0, env = 1, lang = "C"):334 return _remote_launchapp(cmd, args, delay, env, lang)335def hasstate(window_name, object_name, state, guiTimeOut = 0):336 return _remote_hasstate(window_name, object_name, state, guiTimeOut)337def selectrow(window_name, object_name, row_text):338 return _remote_selectrow(window_name, object_name, row_text, False)339def getchild(window_name, child_name = '', role = '', parent = ''):340 return _remote_getchild(window_name, child_name, role, parent)341def enterstring(window_name, object_name = '', data = ''):342 return _remote_enterstring(window_name, object_name, data)343def setvalue(window_name, object_name, data):344 return _remote_setvalue(window_name, object_name, float(data))345def grabfocus(window_name, object_name = ''):346 # On Linux just with window name, grab focus doesn't work347 # So, we can't make this call generic348 return _remote_grabfocus(window_name, object_name)349def copytext(window_name, object_name, start, end = -1):350 return _remote_copytext(window_name, object_name, start, end)351def cuttext(window_name, object_name, start, end = -1):352 return _remote_cuttext(window_name, object_name, start, end)353def deletetext(window_name, object_name, start, end = -1):354 return _remote_deletetext(window_name, object_name, start, end)355def startprocessmonitor(process_name, interval = 2):356 return _remote_startprocessmonitor(process_name, interval)357def gettextvalue(window_name, object_name, startPosition = 0, endPosition = 0):358 return unicode(_remote_gettextvalue(window_name, object_name, startPosition, endPosition))359def getcellvalue(window_name, object_name, row_index, column = 0):360 return _remote_getcellvalue(window_name, object_name, row_index, column)361def getcellsize(window_name, object_name, row_index, column = 0):362 return _remote_getcellsize(window_name, object_name, row_index, column)363def getobjectnameatcoords(waitTime = 0):364 return _remote_getobjectnameatcoords(waitTime)365def onwindowcreate(window_name, fn_name, *args):366 """367 On window create, call the function with given arguments368 @param window_name: Window name to look for, either full name,369 LDTP's name convention, or a Unix glob.370 @type window_name: string371 @param fn_name: Callback function372 @type fn_name: function373 @param *args: arguments to be passed to the callback function374 @type *args: var args375 @return: 1 if registration was successful, 0 if not.376 @rtype: integer377 """378 _pollEvents._callback[window_name] = ["onwindowcreate", fn_name, args]379 return _remote_onwindowcreate(window_name)380def removecallback(window_name):381 """382 Remove registered callback on window create383 @param window_name: Window name to look for, either full name,384 LDTP's name convention, or a Unix glob.385 @type window_name: string386 @return: 1 if registration was successful, 0 if not.387 @rtype: integer388 """389 if window_name in _pollEvents._callback:390 del _pollEvents._callback[window_name]391 return _remote_removecallback(window_name)392def registerevent(event_name, fn_name, *args):393 """394 Register at-spi event395 @param event_name: Event name in at-spi format.396 @type event_name: string397 @param fn_name: Callback function398 @type fn_name: function399 @param *args: arguments to be passed to the callback function400 @type *args: var args401 @return: 1 if registration was successful, 0 if not.402 @rtype: integer403 """404 if not isinstance(event_name, str):405 raise ValueError, "event_name should be string"406 _pollEvents._callback[event_name] = [event_name, fn_name, args]407 return _remote_registerevent(event_name)408def deregisterevent(event_name):409 """410 Remove callback of registered event411 @param event_name: Event name in at-spi format.412 @type event_name: string413 @return: 1 if registration was successful, 0 if not.414 @rtype: integer415 """416 if event_name in _pollEvents._callback:417 del _pollEvents._callback[event_name]418 return _remote_deregisterevent(event_name)419def registerkbevent(keys, modifiers, fn_name, *args):420 """421 Register keystroke events422 @param keys: key to listen423 @type keys: string424 @param modifiers: control / alt combination using gtk MODIFIERS425 @type modifiers: int426 @param fn_name: Callback function427 @type fn_name: function428 @param *args: arguments to be passed to the callback function429 @type *args: var args430 @return: 1 if registration was successful, 0 if not.431 @rtype: integer432 """433 event_name = u"kbevent%s%s" % (keys, modifiers)434 _pollEvents._callback[event_name] = [event_name, fn_name, args]435 return _remote_registerkbevent(keys, modifiers)436def deregisterkbevent(keys, modifiers):437 """438 Remove callback of registered event439 @param keys: key to listen440 @type keys: string441 @param modifiers: control / alt combination using gtk MODIFIERS442 @type modifiers: int443 @return: 1 if registration was successful, 0 if not.444 @rtype: integer445 """446 event_name = u"kbevent%s%s" % (keys, modifiers)447 if event_name in _pollEvents._callback:448 del _pollEvents._callback[event_name]449 return _remote_deregisterkbevent(keys, modifiers)450def windowuptime(window_name):451 """452 Get window uptime453 454 @param window_name: Window name to look for, either full name,455 LDTP's name convention, or a Unix glob.456 @type window_name: string457 @return: "starttime, endtime" as datetime python object458 """459 tmp_time = _remote_windowuptime(window_name)460 if tmp_time:461 tmp_time = tmp_time.split('-')462 start_time = tmp_time[0].split(' ')463 end_time = tmp_time[1].split(' ')464 _start_time = datetime.datetime(int(start_time[0]), int(start_time[1]),465 int(start_time[2]), int(start_time[3]),466 int(start_time[4]), int(start_time[5]))467 _end_time = datetime.datetime(int(end_time[0]), int(end_time[1]),468 int(end_time[2]),int(end_time[3]),469 int(end_time[4]), int(end_time[5]))470 return _start_time, _end_time471 return None472_populateNamespace(globals())473_pollEvents = PollEvents()474thread.start_new_thread(_pollEvents.run, ())475_pollLogs = PollLogs()476thread.start_new_thread(_pollLogs.run, ())...
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!!