Best Python code snippet using pyatom_python
AXClasses.py
Source:AXClasses.py
...422 self._queueEvent(Quartz.CGEventPost,423 (Quartz.kCGHIDEventTap, buttonDragged))424 self._queueEvent(Quartz.CGEventPost,425 (Quartz.kCGSessionEventTap, buttonUp))426 def _leftMouseDragged(self, stopCoord, strCoord, speed):427 """Private method to handle generic mouse left button dragging and428 dropping.429 Parameters: stopCoord(x,y) drop point430 Optional: strCoord (x, y) drag point, default (0,0) get current431 mouse position432 speed (int) 1 to unlimit, simulate mouse moving433 action from some special requirement434 Returns: None435 """436 # Get current position as start point if strCoord not given437 if strCoord == (0, 0):438 loc = AppKit.NSEvent.mouseLocation()439 strCoord = (loc.x, Quartz.CGDisplayPixelsHigh(0) - loc.y)440 # Press left button down441 pressLeftButton = Quartz.CGEventCreateMouseEvent(442 None,443 Quartz.kCGEventLeftMouseDown,444 strCoord,445 Quartz.kCGMouseButtonLeft446 )447 # Queue the events448 Quartz.CGEventPost(Quartz.CoreGraphics.kCGHIDEventTap, pressLeftButton)449 # Wait for reponse of system, a fuzzy icon appears450 time.sleep(5)451 # Simulate mouse moving speed, k is slope452 speed = round(1 / float(speed), 2)453 xmoved = stopCoord[0] - strCoord[0]454 ymoved = stopCoord[1] - strCoord[1]455 if ymoved == 0:456 raise ValueError('Not support horizontal moving')457 else:458 k = abs(ymoved / xmoved)459 if xmoved != 0:460 for xpos in range(int(abs(xmoved))):461 if xmoved > 0 and ymoved > 0:462 currcoord = (strCoord[0] + xpos, strCoord[1] + xpos * k)463 elif xmoved > 0 and ymoved < 0:464 currcoord = (strCoord[0] + xpos, strCoord[1] - xpos * k)465 elif xmoved < 0 and ymoved < 0:466 currcoord = (strCoord[0] - xpos, strCoord[1] - xpos * k)467 elif xmoved < 0 and ymoved > 0:468 currcoord = (strCoord[0] - xpos, strCoord[1] + xpos * k)469 # Drag with left button470 dragLeftButton = Quartz.CGEventCreateMouseEvent(471 None,472 Quartz.kCGEventLeftMouseDragged,473 currcoord,474 Quartz.kCGMouseButtonLeft475 )476 Quartz.CGEventPost(Quartz.CoreGraphics.kCGHIDEventTap,477 dragLeftButton)478 # Wait for reponse of system479 time.sleep(speed)480 else:481 raise ValueError('Not support vertical moving')482 upLeftButton = Quartz.CGEventCreateMouseEvent(483 None,484 Quartz.kCGEventLeftMouseUp,485 stopCoord,486 Quartz.kCGMouseButtonLeft487 )488 # Wait for reponse of system, a plus icon appears489 time.sleep(5)490 # Up left button up491 Quartz.CGEventPost(Quartz.CoreGraphics.kCGHIDEventTap, upLeftButton)492 def _waitFor(self, timeout, notification, **kwargs):493 """Wait for a particular UI event to occur; this can be built494 upon in NativeUIElement for specific convenience methods.495 """496 callback = self._matchOther497 retelem = None498 callbackArgs = None499 callbackKwargs = None500 # Allow customization of the callback, though by default use the basic501 # _match() method502 if 'callback' in kwargs:503 callback = kwargs['callback']504 del kwargs['callback']505 # Deal with these only if callback is provided:506 if 'args' in kwargs:507 if not isinstance(kwargs['args'], tuple):508 errStr = 'Notification callback args not given as a tuple'509 raise TypeError(errStr)510 # If args are given, notification will pass back the returned511 # element in the first positional arg512 callbackArgs = kwargs['args']513 del kwargs['args']514 if 'kwargs' in kwargs:515 if not isinstance(kwargs['kwargs'], dict):516 errStr = 'Notification callback kwargs not given as a dict'517 raise TypeError(errStr)518 callbackKwargs = kwargs['kwargs']519 del kwargs['kwargs']520 # If kwargs are not given as a dictionary but individually listed521 # need to update the callbackKwargs dict with the remaining items in522 # kwargs523 if kwargs:524 if callbackKwargs:525 callbackKwargs.update(kwargs)526 else:527 callbackKwargs = kwargs528 else:529 callbackArgs = (retelem, )530 # Pass the kwargs to the default callback531 callbackKwargs = kwargs532 return self._setNotification(timeout, notification, callback,533 callbackArgs,534 callbackKwargs)535 def waitForFocusToMatchCriteria(self, timeout=10, **kwargs):536 """Convenience method to wait for focused element to change537 (to element matching kwargs criteria).538 Returns: Element or None539 """540 def _matchFocused(retelem, **kwargs):541 return retelem if retelem._match(**kwargs) else None542 retelem = None543 return self._waitFor(timeout, 'AXFocusedUIElementChanged',544 callback=_matchFocused,545 args=(retelem, ),546 **kwargs)547 def _getActions(self):548 """Retrieve a list of actions supported by the object."""549 actions = _a11y.AXUIElement._getActions(self)550 # strip leading AX from actions - help distinguish them from attributes551 return [action[2:] for action in actions]552 def _performAction(self, action):553 """Perform the specified action."""554 _a11y.AXUIElement._performAction(self, 'AX%s' % action)555 def _generateChildren(self):556 """Generator which yields all AXChildren of the object."""557 try:558 children = self.AXChildren559 except _a11y.Error:560 return561 if children:562 for child in children:563 yield child564 def _generateChildrenR(self, target=None):565 """Generator which recursively yields all AXChildren of the object."""566 if target is None:567 target = self568 try:569 children = target.AXChildren570 except _a11y.Error:571 return572 if children:573 for child in children:574 yield child575 for c in self._generateChildrenR(child):576 yield c577 def _match(self, **kwargs):578 """Method which indicates if the object matches specified criteria.579 Match accepts criteria as kwargs and looks them up on attributes.580 Actual matching is performed with fnmatch, so shell-like wildcards581 work within match strings. Examples:582 obj._match(AXTitle='Terminal*')583 obj._match(AXRole='TextField', AXRoleDescription='search text field')584 """585 for k in kwargs.keys():586 try:587 val = getattr(self, k)588 except _a11y.Error:589 return False590 # Not all values may be strings (e.g. size, position)591 if isinstance(val, str):592 if not fnmatch.fnmatch(val, kwargs[k]):593 return False594 else:595 if val != kwargs[k]:596 return False597 return True598 def _matchOther(self, obj, **kwargs):599 """Perform _match but on another object, not self."""600 if obj is not None:601 # Need to check that the returned UI element wasn't destroyed first:602 if self._findFirstR(**kwargs):603 return obj._match(**kwargs)604 return False605 def _generateFind(self, **kwargs):606 """Generator which yields matches on AXChildren."""607 for needle in self._generateChildren():608 if needle._match(**kwargs):609 yield needle610 def _generateFindR(self, **kwargs):611 """Generator which yields matches on AXChildren and their children."""612 for needle in self._generateChildrenR():613 if needle._match(**kwargs):614 yield needle615 def _findAll(self, **kwargs):616 """Return a list of all children that match the specified criteria."""617 result = []618 for item in self._generateFind(**kwargs):619 result.append(item)620 return result621 def _findAllR(self, **kwargs):622 """Return a list of all children (recursively) that match the specified623 criteria.624 """625 result = []626 for item in self._generateFindR(**kwargs):627 result.append(item)628 return result629 def _findFirst(self, **kwargs):630 """Return the first object that matches the criteria."""631 for item in self._generateFind(**kwargs):632 return item633 def _findFirstR(self, **kwargs):634 """Search recursively for the first object that matches the criteria."""635 for item in self._generateFindR(**kwargs):636 return item637 def _getApplication(self):638 """Get the base application UIElement.639 If the UIElement is a child of the application, it will try640 to get the AXParent until it reaches the top application level641 element.642 """643 app = self644 while True:645 try:646 app = app.AXParent647 except _a11y.ErrorUnsupported:648 break649 return app650 def _menuItem(self, menuitem, *args):651 """Return the specified menu item.652 Example - refer to items by name:653 app._menuItem(app.AXMenuBar, 'File', 'New').Press()654 app._menuItem(app.AXMenuBar, 'Edit', 'Insert', 'Line Break').Press()655 Refer to items by index:656 app._menuitem(app.AXMenuBar, 1, 0).Press()657 Refer to items by mix-n-match:658 app._menuitem(app.AXMenuBar, 1, 'About TextEdit').Press()659 """660 self._activate()661 for item in args:662 # If the item has an AXMenu as a child, navigate into it.663 # This seems like a silly abstraction added by apple's a11y api.664 if menuitem.AXChildren[0].AXRole == 'AXMenu':665 menuitem = menuitem.AXChildren[0]666 # Find AXMenuBarItems and AXMenuItems using a handy wildcard667 role = 'AXMenu*Item'668 try:669 menuitem = menuitem.AXChildren[int(item)]670 except ValueError:671 menuitem = menuitem.findFirst(AXRole='AXMenu*Item',672 AXTitle=item)673 return menuitem674 def _activate(self):675 """Activate the application (bringing menus and windows forward)."""676 ra = AppKit.NSRunningApplication677 app = ra.runningApplicationWithProcessIdentifier_(678 self._getPid())679 # NSApplicationActivateAllWindows | NSApplicationActivateIgnoringOtherApps680 # == 3 - PyObjC in 10.6 does not expose these constants though so I have681 # to use the int instead of the symbolic names682 app.activateWithOptions_(3)683 def _getBundleId(self):684 """Return the bundle ID of the application."""685 ra = AppKit.NSRunningApplication686 app = ra.runningApplicationWithProcessIdentifier_(687 self._getPid())688 return app.bundleIdentifier()689 def _getLocalizedName(self):690 """Return the localized name of the application."""691 return self._getApplication().AXTitle692 def __getattr__(self, name):693 """Handle attribute requests in several ways:694 1. If it starts with AX, it is probably an a11y attribute. Pass695 it to the handler in _a11y which will determine that for sure.696 2. See if the attribute is an action which can be invoked on the697 UIElement. If so, return a function that will invoke the attribute.698 """699 if name.startswith('AX'):700 try:701 attr = self._getAttribute(name)702 return attr703 except AttributeError:704 pass705 # Populate the list of callable actions:706 actions = []707 try:708 actions = self._getActions()709 except Exception:710 pass711 if name.startswith('AX') and (name[2:] in actions):712 errStr = 'Actions on an object should be called without AX ' \713 'prepended'714 raise AttributeError(errStr)715 if name in actions:716 def performSpecifiedAction():717 # activate the app before performing the specified action718 self._activate()719 return self._performAction(name)720 return performSpecifiedAction721 else:722 raise AttributeError('Object %s has no attribute %s' % (self, name))723 def __setattr__(self, name, value):724 """Set attributes on the object."""725 if name.startswith('AX'):726 return self._setAttribute(name, value)727 else:728 _a11y.AXUIElement.__setattr__(self, name, value)729 def __repr__(self):730 """Build a descriptive string for UIElements."""731 title = repr('')732 role = '<No role!>'733 c = repr(self.__class__).partition('<class \'')[-1].rpartition('\'>')[0]734 try:735 title = repr(self.AXTitle)736 except Exception:737 try:738 title = repr(self.AXValue)739 except Exception:740 try:741 title = repr(self.AXRoleDescription)742 except Exception:743 pass744 try:745 role = self.AXRole746 except Exception:747 pass748 if len(title) > 20:749 title = title[:20] + '...\''750 return '<%s %s %s>' % (c, role, title)751class NativeUIElement(BaseAXUIElement):752 """NativeUIElement class - expose the accessibility API in the simplest,753 most natural way possible.754 """755 def getAttributes(self):756 """Get a list of the attributes available on the element."""757 return self._getAttributes()758 def getActions(self):759 """Return a list of the actions available on the element."""760 return self._getActions()761 def setString(self, attribute, string):762 """Set the specified attribute to the specified string."""763 return self._setString(attribute, string)764 def findFirst(self, **kwargs):765 """Return the first object that matches the criteria."""766 return self._findFirst(**kwargs)767 def findFirstR(self, **kwargs):768 """Search recursively for the first object that matches the769 criteria.770 """771 return self._findFirstR(**kwargs)772 def findAll(self, **kwargs):773 """Return a list of all children that match the specified criteria."""774 return self._findAll(**kwargs)775 def findAllR(self, **kwargs):776 """Return a list of all children (recursively) that match777 the specified criteria.778 """779 return self._findAllR(**kwargs)780 def getElementAtPosition(self, coord):781 """Return the AXUIElement at the given coordinates.782 If self is behind other windows, this function will return self.783 """784 return self._getElementAtPosition(float(coord[0]), float(coord[1]))785 def activate(self):786 """Activate the application (bringing menus and windows forward)"""787 return self._activate()788 def getApplication(self):789 """Get the base application UIElement.790 If the UIElement is a child of the application, it will try791 to get the AXParent until it reaches the top application level792 element.793 """794 return self._getApplication()795 def menuItem(self, *args):796 """Return the specified menu item.797 Example - refer to items by name:798 app.menuItem('File', 'New').Press()799 app.menuItem('Edit', 'Insert', 'Line Break').Press()800 Refer to items by index:801 app.menuitem(1, 0).Press()802 Refer to items by mix-n-match:803 app.menuitem(1, 'About TextEdit').Press()804 """805 menuitem = self._getApplication().AXMenuBar806 return self._menuItem(menuitem, *args)807 def popUpItem(self, *args):808 """Return the specified item in a pop up menu."""809 self.Press()810 time.sleep(.5)811 return self._menuItem(self, *args)812 def getBundleId(self):813 """Return the bundle ID of the application."""814 return self._getBundleId()815 def getLocalizedName(self):816 """Return the localized name of the application."""817 return self._getLocalizedName()818 def sendKey(self, keychr):819 """Send one character with no modifiers."""820 return self._sendKey(keychr)821 def sendGlobalKey(self, keychr):822 """Send one character without modifiers to the system.823 It will not send an event directly to the application, system will824 dispatch it to the window which has keyboard focus.825 Parameters: keychr - Single keyboard character which will be sent.826 """827 return self._sendKey(keychr, globally=True)828 def sendKeys(self, keystr):829 """Send a series of characters with no modifiers."""830 return self._sendKeys(keystr)831 def pressModifiers(self, modifiers):832 """Hold modifier keys (e.g. [Option])."""833 return self._holdModifierKeys(modifiers)834 def releaseModifiers(self, modifiers):835 """Release modifier keys (e.g. [Option])."""836 return self._releaseModifierKeys(modifiers)837 def sendKeyWithModifiers(self, keychr, modifiers):838 """Send one character with modifiers pressed839 Parameters: key character, modifiers (list) (e.g. [SHIFT] or840 [COMMAND, SHIFT] (assuming you've first used841 from pyatom.AXKeyCodeConstants import *))842 """843 return self._sendKeyWithModifiers(keychr, modifiers, False)844 def sendGlobalKeyWithModifiers(self, keychr, modifiers):845 """Global send one character with modifiers pressed.846 See sendKeyWithModifiers847 """848 return self._sendKeyWithModifiers(keychr, modifiers, True)849 def dragMouseButtonLeft(self, coord, dest_coord, interval=0.5):850 """Drag the left mouse button without modifiers pressed.851 Parameters: coordinates to click on screen (tuple (x, y))852 dest coordinates to drag to (tuple (x, y))853 interval to send event of btn down, drag and up854 Returns: None855 """856 modFlags = 0857 self._queueMouseButton(coord, Quartz.kCGMouseButtonLeft, modFlags,858 dest_coord=dest_coord)859 self._postQueuedEvents(interval=interval)860 def doubleClickDragMouseButtonLeft(self, coord, dest_coord, interval=0.5):861 """Double-click and drag the left mouse button without modifiers862 pressed.863 Parameters: coordinates to double-click on screen (tuple (x, y))864 dest coordinates to drag to (tuple (x, y))865 interval to send event of btn down, drag and up866 Returns: None867 """868 modFlags = 0869 self._queueMouseButton(coord, Quartz.kCGMouseButtonLeft, modFlags,870 dest_coord=dest_coord)871 self._queueMouseButton(coord, Quartz.kCGMouseButtonLeft, modFlags,872 dest_coord=dest_coord,873 clickCount=2)874 self._postQueuedEvents(interval=interval)875 def clickMouseButtonLeft(self, coord, interval=None):876 """Click the left mouse button without modifiers pressed.877 Parameters: coordinates to click on screen (tuple (x, y))878 Returns: None879 """880 modFlags = 0881 self._queueMouseButton(coord, Quartz.kCGMouseButtonLeft, modFlags)882 if interval:883 self._postQueuedEvents(interval=interval)884 else:885 self._postQueuedEvents()886 def clickMouseButtonRight(self, coord):887 """Click the right mouse button without modifiers pressed.888 Parameters: coordinates to click on scren (tuple (x, y))889 Returns: None890 """891 modFlags = 0892 self._queueMouseButton(coord, Quartz.kCGMouseButtonRight, modFlags)893 self._postQueuedEvents()894 def clickMouseButtonLeftWithMods(self, coord, modifiers, interval=None):895 """Click the left mouse button with modifiers pressed.896 Parameters: coordinates to click; modifiers (list) (e.g. [SHIFT] or897 [COMMAND, SHIFT] (assuming you've first used898 from pyatom.AXKeyCodeConstants import *))899 Returns: None900 """901 modFlags = self._pressModifiers(modifiers)902 self._queueMouseButton(coord, Quartz.kCGMouseButtonLeft, modFlags)903 self._releaseModifiers(modifiers)904 if interval:905 self._postQueuedEvents(interval=interval)906 else:907 self._postQueuedEvents()908 def clickMouseButtonRightWithMods(self, coord, modifiers):909 """Click the right mouse button with modifiers pressed.910 Parameters: coordinates to click; modifiers (list)911 Returns: None912 """913 modFlags = self._pressModifiers(modifiers)914 self._queueMouseButton(coord, Quartz.kCGMouseButtonRight, modFlags)915 self._releaseModifiers(modifiers)916 self._postQueuedEvents()917 def leftMouseDragged(self, stopCoord, strCoord=(0, 0), speed=1):918 """Click the left mouse button and drag object.919 Parameters: stopCoord, the position of dragging stopped920 strCoord, the position of dragging started921 (0,0) will get current position922 speed is mouse moving speed, 0 to unlimited923 Returns: None924 """925 self._leftMouseDragged(stopCoord, strCoord, speed)926 def doubleClickMouse(self, coord):927 """Double-click primary mouse button.928 Parameters: coordinates to click (assume primary is left button)929 Returns: None930 """931 modFlags = 0932 self._queueMouseButton(coord, Quartz.kCGMouseButtonLeft, modFlags)933 # This is a kludge:934 # If directed towards a Fusion VM the clickCount gets ignored and this935 # will be seen as a single click, so in sequence this will be a double-936 # click937 # Otherwise to a host app only this second one will count as a double-938 # click939 self._queueMouseButton(coord, Quartz.kCGMouseButtonLeft, modFlags,...
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!!