Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

appium-mac2-driver

Package Overview
Dependencies
Maintainers
3
Versions
100
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

appium-mac2-driver

XCTest-based Appium driver for macOS apps automation

  • 0.5.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
18K
decreased by-49.63%
Maintainers
3
Weekly downloads
 
Created
Source

Appium Mac2 Driver

This is Appium driver for automating macOS applications using Apple's XCTest framework. The driver operates in scope of W3C WebDriver protocol with several custom extensions to cover operating-system specific scenarios. The original idea and parts of the source code are borrowed from the Facebook's WebDriverAgent project.

Requirements

On top of standard Appium requirements Mac2 driver also expects the following prerequisites:

  • macOS 10.15 or later
  • Xcode 12 or later should be installed
  • Xcode Helper app should be enabled for Accessibility access. The app itself could be usually found at /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Xcode/Agents/Xcode Helper.app. In order to enable Accessibility access for it simply open the parent folder in Finder: open /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Xcode/Agents/ and drag & drop the Xcode Helper app to Security & Privacy -> Privacy -> Accessibility list of your System Preferences. This action must only be done once.
  • Carthage should be present. On macOS the utility could be installed via Brew: brew install carthage

Capabilities

Capability NameDescription
platformNameShould be set to mac
automationNameMust always be set to mac2. Values of automationName are compared case-insensitively.
appium:systemPortThe number of the port for the driver to listen on. If not provided then Appium will use the default port 10100.
appium:showServerLogsSet it to true in order to include xcodebuild output to the Appium server log. false by default.
appium:bootstrapRootThe full path to WebDriverAgentMac root folder where Xcode project of the server sources lives. By default this project is located in the same folder where the corresponding driver Node.js module lives.
appium:dependenciesLoadTimeoutThe number of milliseconds to wait for Carthage dependencies load. These dependencies are loaded when the project is built for the first time. 120000 by default
appium:serverStartupTimeoutThe number of milliseconds to wait util the WebDriverAgentMac project is built and started. 120000 by default
appium:bundleIdThe bundle identifier of the application to automate, for example com.apple.TextEdit. This is an optional capability. If it is not provided then the session will be started without an application under test (actually, it will be Finder). If the application with the given identifier is not installed then an error will be thrown on session startup. If the application is already running then it will be moved to the foreground.
appium:argumentsArray of application command line arguments. This capability is only going to be applied if the application is not running on session startup.
appium:environmentA dictionary of environment variables (name->value) that are going to be passed to the application under test on top of environment variables inherited from the parent process. This capability is only going to be applied if the application is not running on session startup.
appium:skipAppKillWhether to skip the termination of the application under test when the testing session quits. false by default. This capability is only going to be applied if bundleId is set.
appium:prerunAn object containing either script or command key. The value of each key must be a valid AppleScript script or command to be executed prior to the Mac2Driver session startup. See AppleScript commands execution for more details. Example: {command: 'do shell script "echo hello"'}
appium:postrunAn object containing either script or command key. The value of each key must be a valid AppleScript script or command to be executed after Mac2Driver session is stopped. See AppleScript commands execution for more details.
appium:noResetWhether to restart the app whose bundle identifier was passed to capabilities as bundleId value if it was already running on the session startup (false, the default value) or just pick it up without changing the app state (true). Note that neither of arguments or environment capabilities will take effect if the app did not restart.

Element Attributes

Mac2 driver supports the following element attributes:

NameDescriptionExample
elementTypeInteger-encoded element class. See the official documentation on XCUIElementType enumeration for more details.'2'
frameCoordinates of bounding element rectangle{x: 1, y: 2.5, width: 100, height: 200}
placeholderValueIt is usually only present for text fields. For other element types it's mostly empty'my placeholder'
enabledContains true if the element is enabled'false'
selectedContains true if the element is selected'false'
labelElement's label value. Could be empty'my label'
titleElement's title value. Could be empty'my title'
identifierElement's accessibility identifier. Could be empty'identifier'
valueThe value could be different depending on the actual element type. For example, text fields might have their text context there and sliders would contain the float position value, while switches would have either 1 or 0'1.5'

These attribute values could be retrieved from the page source output and then used for elements location. See the official documentation on XCUIElementAttributes protocol for more details on each attribute.

Element Location

Mac2 driver supports the following location strategies:

NameDescriptionExample
accessibilityId, id, nameThese all strategies are mapped to the same Mac2 driver ByIdentifier lookup strategy. The locator matches the passed value with element's identifer attribute case-sensitively.MobileBy.accessibilityId("identifier"), By.id("identifier"), By.name("identifier")
classNameClass name uses stringified element types for lookupBy.className("XCUIElementTypePopUpButton")
predicateLookup by predicate is natively supported by XCTest and is as fast as previous lookup strategies. This lookup strategy could only use the supported element attributes. Unknown attribute names would throw an exception. Check NSPredicate cheat sheet for more details on how to build effective and flexible locators.MobileBy.iOSNsPredicateString("elementType == 2 AND label BEGINSWITH 'Safari'")
classChainThis strategy is a combination of Xpath flexibility and fast predicate lookup. Prefer it over Xpath unless there is no other way to build the desired locator. Visit Class Chain Construction Rules tutorial to get more knowledge on how to build class chain locators.MobileBy.iOSClassChain("**/XCUIElementTypeRuler[$elementType == 72 AND value BEGINSWITH '10'$]")
xpathFor elements lookup Xpath strategy the driver uses the same XML tree that is generated by page source API. Only Xpath 1.0 is supported (based on xmllib2).By.xpath("//XCUIElementTypePopUpButton[@value="Regular" and @label="type face"]")

Check the integration tests for more examples on different location strategies usage.

Platform-Specific Extensions

Beside of standard W3C APIs the driver provides the following custom command extensions to execute platform specific scenarios:

macos: click

Perform click gesture on an element or by relative/absolute coordinates

Arguments
NameTypeRequiredDescriptionExample
elementstringif x or y are unsetUnique identifier of the element to perform the click on. Either this property or/and x and y must be set. If both are set then x and y are considered as relative element coordinates. If only x and y are set then these are parsed as absolute coordinates.21045BC8-013C-43BD-9B1E-4C6DC7AB0744
xnumberif y is set or element is unsetclick X coordinate100
ynumberif y is set or element is unsetclick Y coordinate100
keyModifierFlagsnumbernoif set then the given key modifiers will be applied while click is performed. See the official documentation on XCUIKeyModifierFlags enumeration for more details`1 << 1

macos: clickAndHold

Perform long click gesture on an element or by relative/absolute coordinates

Arguments
NameTypeRequiredDescriptionExample
elementstringif x or y are unsetUnique identifier of the element to perform the long click on. Either this property or/and x and y must be set. If both are set then x and y are considered as relative element coordinates. If only x and y are set then these are parsed as absolute coordinates.21045BC8-013C-43BD-9B1E-4C6DC7AB0744
xnumberif y is set or element is unsetlong click X coordinate100
ynumberif y is set or element is unsetlong click Y coordinate100
durationnumberyesThe number of float seconds to hold the mouse button2.5
keyModifierFlagsnumbernoif set then the given key modifiers will be applied while long click is performed. See the official documentation on XCUIKeyModifierFlags enumeration for more details`1 << 1

macos: scroll

Perform scroll gesture on an element or by relative/absolute coordinates

Arguments
NameTypeRequiredDescriptionExample
elementstringif x or y are unsetUnique identifier of the element to be scrolled. Either this property or/and x and y must be set. If both are set then x and y are considered as relative element coordinates. If only x and y are set then these are parsed as absolute coordinates.21045BC8-013C-43BD-9B1E-4C6DC7AB0744
xnumberif y is set or element is unsetscroll X coordinate100
ynumberif y is set or element is unsetscroll Y coordinate100
deltaXnumberyesHorizontal delta as float number. Could be negative100
deltaYnumberyesvertical delta as float number. Could be negative100
keyModifierFlagsnumbernoif set then the given key modifiers will be applied while scroll is performed. See the official documentation on XCUIKeyModifierFlags enumeration for more details`1 << 1

macos: rightClick

Perform right click gesture on an element or by relative/absolute coordinates

Arguments
NameTypeRequiredDescriptionExample
elementstringif x or y are unsetUnique identifier of the element to perform the right click on. Either this property or/and x and y must be set. If both are set then x and y are considered as relative element coordinates. If only x and y are set then these are parsed as absolute coordinates.21045BC8-013C-43BD-9B1E-4C6DC7AB0744
xnumberif y is set or element is unsetright click X coordinate100
ynumberif y is set or element is unsetright click Y coordinate100
keyModifierFlagsnumbernoif set then the given key modifiers will be applied while right click is performed. See the official documentation on XCUIKeyModifierFlags enumeration for more details`1 << 1

macos: hover

Perform hover gesture on an element or by relative/absolute coordinates

Arguments
NameTypeRequiredDescriptionExample
elementstringif x or y are unsetUnique identifier of the element to perform the hover on. Either this property or/and x and y must be set. If both are set then x and y are considered as relative element coordinates. If only x and y are set then these are parsed as absolute coordinates.21045BC8-013C-43BD-9B1E-4C6DC7AB0744
xnumberif y is set or element is unsetlong click X coordinate100
ynumberif y is set or element is unsetlong click Y coordinate100
keyModifierFlagsnumbernoif set then the given key modifiers will be applied while hover is performed. See the official documentation on XCUIKeyModifierFlags enumeration for more details`1 << 1

macos: doubleClick

Perform double click gesture on an element or by relative/absolute coordinates

Arguments
NameTypeRequiredDescriptionExample
elementstringif x or y are unsetUnique identifier of the element to perform the double click on. Either this property or/and x and y must be set. If both are set then x and y are considered as relative element coordinates. If only x and y are set then these are parsed as absolute coordinates.21045BC8-013C-43BD-9B1E-4C6DC7AB0744
xnumberif y is set or element is unsetdouble click X coordinate100
ynumberif y is set or element is unsetdouble click Y coordinate100
keyModifierFlagsnumbernoif set then the given key modifiers will be applied while click is performed. See the official documentation on XCUIKeyModifierFlags enumeration for more details`1 << 1

macos: clickAndDrag

Perform long click and drag gesture on an element or by absolute coordinates

Arguments
NameTypeRequiredDescriptionExample
sourceElementstringif startX, startY, endX and endY are unset or if destinationElement is setUuid of the element to start the drag from.21045BC8-013C-43BD-9B1E-4C6DC7AB0744
destinationElementstringif startX, startY, endX and endY are unset or if sourceElement is setUuid of the element to end the drag on.21045BC8-013C-43BD-9B1E-4C6DC7AB0745
startXnumberif sourceElement and destinationElement are unsetstarting X coordinate100
startYnumberif sourceElement and destinationElement are unsetstarting Y coordinate110
endXnumberif sourceElement and destinationElement are unsetend X coordinate200
endYnumberif sourceElement and destinationElement are unsetend Y coordinate220
durationnumberyesThe number of float seconds to hold the mouse button2.5
keyModifierFlagsnumbernoif set then the given key modifiers will be applied while drag is performed. See the official documentation on XCUIKeyModifierFlags enumeration for more details`1 << 1

macos: clickDragAndDrag

Perform long click, drag and hold gesture on an element or by absolute coordinates

Arguments
NameTypeRequiredDescriptionExample
sourceElementstringif startX, startY, endX and endY are unset or if destinationElement is setUuid of the element to start the drag from.21045BC8-013C-43BD-9B1E-4C6DC7AB0744
destinationElementstringif startX, startY, endX and endY are unset or if sourceElement is setUuid of the element to end the drag on.21045BC8-013C-43BD-9B1E-4C6DC7AB0745
startXnumberif sourceElement and destinationElement are unsetstarting X coordinate100
startYnumberif sourceElement and destinationElement are unsetstarting Y coordinate110
endXnumberif sourceElement and destinationElement are unsetend X coordinate200
endYnumberif sourceElement and destinationElement are unsetend Y coordinate220
durationnumberyesThe number of float seconds to hold the mouse button2.5
velocitynumbernoDragging velocity in pixels per second. If not provided then the default velocity is used. See official documentation on XCUIGestureVelocity structure for more details2500
keyModifierFlagsnumbernoif set then the given key modifiers will be applied while drag is performed. See the official documentation on XCUIKeyModifierFlags enumeration for more details`1 << 1

macos: keys

Send keys to the given element or to the application under test

Arguments
NameTypeRequiredDescriptionExample
elementstringnoUnique identifier of the element to send the keys to. If unset then keys are sent to the current application under test.21045BC8-013C-43BD-9B1E-4C6DC7AB0744
keysarrayyesArray of keys to type. Each item could either be a string, that represents a key itself (see the official documentation on XCUIElement's typeKey:modifierFlags: method and on XCUIKeyboardKey constants) or a dictionary with key and modifierFlags entries, if the key should also be entered with modifiers.['h', 'i'] or [{key: 'h', modifierFlags: 1 << 1}, {key: 'i', modifierFlags: 1 << 2}]

macos: source

Retrieves the string representation of the current application

Arguments
NameTypeRequiredDescriptionExample
formatstringyesThe format of the application source to retrieve. Only two formats are supported: xml (returns the source formatted as XML document, the default value) and description (returns the source formatted as debugDescription output). See the official documentation on XCUIElement's debugDescription method for more details.description
Returns

The source of the current page in a string representation

macos: launchApp

Start an app with the given bundle identifier or activates it if the app is already running. An exception is thrown if the app with the given identifier cannot be found. This API influences the state of the Application Under Test.

Arguments
NameTypeRequiredDescriptionExample
bundleIdstringyesbundle identifier of the app to be launched or activatedcom.apple.TextEdit
argumentsarraynothe list of command line arguments for the app to be be launched with. This argument is ignored if the app is already running.['--help']
environmentdictionarynoEnvironment variables mapping. Custom variables are added to the default process environment. This argument is ignored if the app is already running.{ myEnvVar: 'value' }

macos: activateApp

Activates an app with the given bundle identifier. An exception is thrown if the app with the given identifier cannot be found or if the app is not running. This API influences the state of the Application Under Test.

Arguments
NameTypeRequiredDescriptionExample
bundleIdstringyesbundle identifier of the app to be activatedcom.apple.Finder

macos: terminateApp

Terminate an app with the given bundle identifier. An exception is thrown if the app cannot be found. This API influences the state of the Application Under Test.

Arguments
NameTypeRequiredDescriptionExample
bundleIdstringyesbundle identifier of the app to be terminatedcom.apple.Finder
Returns

true if the app was running before being terminated

macos: queryAppState

Query an app state with given bundle identifier. An exception is thrown if the pp cannot be found.

Arguments
NameTypeRequiredDescriptionExample
bundleIdstringyesbundle identifier of the app to be queriedcom.apple.TextEdit
Returns

An integer value representing the application state. See the official documentation on XCUIApplicationState enumeration for more details.

macos: appleScript

Executes the given AppleScript command or a whole script based on the given options. Either of these options must be provided. If both are provided then the command one gets the priority. Note that AppleScript command cannot contain line breaks. Consider making it to a script in such case. Note that by default AppleScript engine blocks commands/scripts execution if your script is trying to access some private entities, like cameras or the desktop screen and no permissions to do it are given to the parent (for example, Appium or Terminal) process in System Preferences -> Privacy list. See AppleScript Commands Execution for more details.

Arguments
NameTypeRequiredDescriptionExample
commandstringyes if script is not providedAppleScript command to executedo shell script "echo hello"
scriptstringyes if command is not providedAppleScript script to executedo shell script "echo hello"\ndo shell script "echo hello2"
timeoutnumbernoThe number of seconds to wait until a long-running blocking command is finished. An error is thrown if the command is still running after this timeout expires.60000
cwdstringnoThe path to an existing folder which is going to be set as the working directory for the command/script being executed./tmp
Returns

The actual stdout of the provided script if its execution was successful (e.g. got zero return code).

Application Under Test Concept

The Mac2 driver has the concept of Application Under Test. This is the app, whose bundle identifier has been passed as bundleId capability or as an argument to macos: activateApp or macos: launchApp extensions. If this application is unexpectedly terminated during test session execution then an exception is going to be thrown upon any following session command invocation. In such case the driver assumes the application under test is crashed and it is impossible to proceed. Also, the Application Under Test is going to be terminated when the testing session quits (unless canceled by skipAppKill capability). It is possible to reset the Application Under Test value to none by executing macos: terminateApp helper and providing the bundle identifier of this app to it. Also, macos: activateApp or macos: launchApp calls can change Application Under Test value if a bundle identifier different from the current one is provided to them.

AppleScript Commands Execution

There is a possibility to run custom AppleScript from your client code. This feature is potentially insecure and thus needs to be explicitly enabled when executing the server by providing apple_script key to the list of enabled insecure features. Check Appium Security document for more details. It is possible to either execute a single AppleScript command (use the command argument) or a whole script (use the script argument) and get its stdout in response. If the script execution returns non-zero exit code then an exception is going to be thrown. The exception message will contain the actual stderr. If the script is a blocking one then it could only run up to 20 seconds long. After that the script will be terminated and a timeout error will be thrown. This timeout could be customized by providing the timeout option value. You could also customize the script working directory by providing the cwd option. Here's an example code of how to get a shell command output:

// java
String appleScript = "do shell script \"echo hello\"";
System.println(driver.executeScript("macos: appleScript", ImmutableMap.of("command", appleScript)));

W3C Action Recipes

In theory it is possible to emulate a mouse gesture of any complexity with W3C actions. However, there is a set of "standard" gestures, where each operating system has its own requirements, like clicks, double clicks, etc. All such action parameters must comply with these requirements to be recognized properly. Here is a short list of examples for the most common macOS pointer gestures:

Click

[
  {"type": "pointerMove", "duration": 10, "x": 100, "y": 100},
  {"type": "pointerDown", "button": 0},
  {"type": "pause", "duration": 100},
  {"type": "pointerUp", "button": 0}
]

The duration of mouse button suppression should be 0-125 ms.

Right Click

[
  {"type": "pointerMove", "duration": 10, "x": 100, "y": 100},
  {"type": "pointerDown", "button": 2},
  {"type": "pointerUp", "button": 2}
]

The duration of mouse button suppression should be 0-125 ms.

Double Click

[
  {"type": "pointerMove", "duration": 10, "x": 100, "y": 100},
  {"type": "pointerDown", "button": 0},
  {"type": "pointerUp", "button": 0},
  {"type": "pause", "duration": 1000},
  {"type": "pointerDown", "button": 0},
  {"type": "pointerUp", "button": 0}
]

The duration between two clicks should be 600-1000 ms.

Drag & Drop

[
  {"type": "pointerMove", "duration": 10, "x": 100, "y": 100},
  {"type": "pointerDown", "button": 0},
  {"type": "pause", "duration": 600},
  {"type": "pointerMove", "duration": 10, "x": 200, "y": 200},
  {"type": "pointerUp", "button": 0}
]

The longer is the duration of the second pointerMove action the lesser is the drag velocity and vice versa. One could add more pointerMove actions before releasing the mouse button to simulate complex cursor moving paths. Mac2Driver terminates action execution with a timeout error if the duration of it exceeds 5 minutes.

Hover

[
  {"type": "pointerMove", "duration": 10, "x": 100, "y": 100},
  {"type": "pointerMove", "duration": 1000, "x": 200, "y": 200}
]

This snippet tells the mouse pointer to hover over [100, 100, 200, 200] area for 1 second. In general, hover action will be performed every time if there is no preceding pointerDown to the current pointerMove one or the preceding pointerDown action has been ended by the pointerUp one.

Settings API

Mac2Driver supports Appium Settings API. Along with the common settings the following driver-specific settings are currently available:

NameTypeDescription
boundElementsByIndexbooleanWhether to use elements binding by index (true) or by accessibility identifier (the default setting, false). It makes sense to switch the binding strategy to workaround stale element reference errors containing Identity Binding text in their descriptions. See the corresponding Stack Overflow discussion to know more details on the difference between these two binding strategies.
useDefaultUiInterruptionsHandlingbooleanWhether to use the default XCTest UI interruptions handling (true, the default setting) or to disable it for the Application Under Test (false). It makes sense to disable the default handler if it is necessary to validate the interrupting element's presence in your test or do some other actions on it rather than just closing the view implicitly. Check this WWDC presentation from Apple to get more details on the UI interruptions handling.

Examples

# Python3 + PyTest
import pytest

from appium import webdriver


@pytest.fixture()
def driver():
    drv = webdriver.Remote('http://localhost:4723/wd/hub', {
        # automationName capability presence is mandatory for this Mac2 Driver to be selected
        'automationName': 'Mac2',
        'platformName': 'mac',
        'bundleId': 'com.apple.TextEdit',
    })
    yield drv
    drv.quit()


def test_edit_text(driver):
    edit_field = driver.find_element_by_class_name('XCUIElementTypeTextView')
    edit_field.send_keys('hello world')
    assert edit_field.text == 'hello world'
    edit_field.clear()
    assert edit_field.text == ''


def test_sending_custom_keys(driver):
    edit_field = driver.find_element_by_class_name('XCUIElementTypeTextView')
    flagsShift = 1 << 1
    edit_field.execute_script('macos: keys', {
        'keys': [{
            'key': 'h',
            'modifierFlags': flagsShift,
        }, {
            'key': 'i',
            'modifierFlags': flagsShift,
        }]
    })
    assert edit_field.text == 'HI'

Parallel Execution

Parallel execution of multiple Mac2 driver instances is highly discouraged. Only one UI test must be running at the same time, since the access to accessibility layer is single-threaded. Also HID devices, like the mouse or the keyboard, must be acquired exclusively.

Development & Testing

This module uses the same development tools as the other Appium drivers.

Check out the source. Then run:

npm install
gulp watch

Execute npm run test to run unit tests and npm run e2e-test to run integration tests.

Notes

  • W3C actions support is limited (only mouse actions are supported). You could also use macos: extension APIs to cover your test scenarios

Keywords

FAQs

Package last updated on 15 Dec 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc