Best Python code snippet using pyatom_python
AXClasses.py
Source:AXClasses.py  
...317        modifiers.reverse()318        modFlags = self._pressModifiers(modifiers, pressed=False,319                                        globally=globally)320        return modFlags321    def _releaseModifierKeys(self, modifiers):322        """Release given modifier keys (provided in list form).323        Parameters: modifiers list324        Returns: Unsigned int representing flags to set325        """326        modFlags = self._releaseModifiers(modifiers)327        # Post the queued keypresses:328        self._postQueuedEvents()329        return modFlags330    @staticmethod331    def _isSingleCharacter(keychr):332        """Check whether given keyboard character is a single character.333        Parameters: key character which will be checked.334        Returns: True when given key character is a single character.335        """336        if not keychr:337            return False338        # Regular character case.339        if len(keychr) == 1:340            return True341        # Tagged character case.342        return keychr.count('<') == 1 and keychr.count('>') == 1 and \343               keychr[0] == '<' and keychr[-1] == '>'344    def _sendKeyWithModifiers(self, keychr, modifiers, globally=False):345        """Send one character with the given modifiers pressed.346        Parameters: key character, list of modifiers, global or app specific347        Returns: None or raise ValueError exception348        """349        if not self._isSingleCharacter(keychr):350            raise ValueError('Please provide only one character to send')351        if not hasattr(self, 'keyboard'):352            self.keyboard = AXKeyboard.loadKeyboard()353        modFlags = self._pressModifiers(modifiers, globally=globally)354        # Press the non-modifier key355        self._sendKey(keychr, modFlags, globally=globally)356        # Release the modifiers357        self._releaseModifiers(modifiers, globally=globally)358        # Post the queued keypresses:359        self._postQueuedEvents()360    def _queueMouseButton(self, coord, mouseButton, modFlags, clickCount=1,361                          dest_coord=None):362        """Private method to handle generic mouse button clicking.363        Parameters: coord (x, y) to click, mouseButton (e.g.,364                    kCGMouseButtonLeft), modFlags set (int)365        Optional: clickCount (default 1; set to 2 for double-click; 3 for366                  triple-click on host)367        Returns: None368        """369        # For now allow only left and right mouse buttons:370        mouseButtons = {371            Quartz.kCGMouseButtonLeft: 'LeftMouse',372            Quartz.kCGMouseButtonRight: 'RightMouse',373        }374        if mouseButton not in mouseButtons:375            raise ValueError('Mouse button given not recognized')376        eventButtonDown = getattr(Quartz,377                                  'kCGEvent%sDown' % mouseButtons[mouseButton])378        eventButtonUp = getattr(Quartz,379                                'kCGEvent%sUp' % mouseButtons[mouseButton])380        eventButtonDragged = getattr(Quartz,381                                     'kCGEvent%sDragged' % mouseButtons[382                                         mouseButton])383        # Press the button384        buttonDown = Quartz.CGEventCreateMouseEvent(None,385                                                    eventButtonDown,386                                                    coord,387                                                    mouseButton)388        # Set modflags (default None) on button down:389        Quartz.CGEventSetFlags(buttonDown, modFlags)390        # Set the click count on button down:391        Quartz.CGEventSetIntegerValueField(buttonDown,392                                           Quartz.kCGMouseEventClickState,393                                           int(clickCount))394        if dest_coord:395            # Drag and release the button396            buttonDragged = Quartz.CGEventCreateMouseEvent(None,397                                                           eventButtonDragged,398                                                           dest_coord,399                                                           mouseButton)400            # Set modflags on the button dragged:401            Quartz.CGEventSetFlags(buttonDragged, modFlags)402            buttonUp = Quartz.CGEventCreateMouseEvent(None,403                                                      eventButtonUp,404                                                      dest_coord,405                                                      mouseButton)406        else:407            # Release the button408            buttonUp = Quartz.CGEventCreateMouseEvent(None,409                                                      eventButtonUp,410                                                      coord,411                                                      mouseButton)412        # Set modflags on the button up:413        Quartz.CGEventSetFlags(buttonUp, modFlags)414        # Set the click count on button up:415        Quartz.CGEventSetIntegerValueField(buttonUp,416                                           Quartz.kCGMouseEventClickState,417                                           int(clickCount))418        # Queue the events419        self._queueEvent(Quartz.CGEventPost,420                         (Quartz.kCGSessionEventTap, buttonDown))421        if dest_coord: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....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!!
