iOS Specific Touch Gestures With Appium

Appium-for-Android-on-mac

     Nowadays the iOS automation is very popular especially doing with help of Appium. Here I would like to explain some important iOS specific touch gestures.  Because these are not part of the WebDriver spec, Appium provides this access by overloading the executeScript command, as you’ll see in the examples below.

mobile: swipe

     This command ultimately calls the XCUIElement.swipe* family of methods provided by XCUITest, and thus takes two parameters: a direction (whether to swipe up, down, left, or right), and the ID of an element within which the swipe is to take place (Appium defaults to the entire Application element if no element is specified). Following is an example,

// swipe up then down
Map<String, Object> args = new HashMap<>();
args.put("direction", "up");
driver.executeScript("mobile: swipe", args);
args.put("direction", "down");
driver.executeScript("mobile: swipe", args);

mobile: scroll

     If you want to try and make sure that each movement of your gesture moves a view by the height of the scrollable content, or if you want to scroll until a particular element is visible, try mobile: scroll. It works similarly to mobile: swipe but takes more parameters:

  • element: the id of the element to scroll within (the application element by default). Call this the “bounding element”
  • direction: the opposite of how direction is used in mobile: swipe. A swipe “up” will scroll view contents down, whereas this is what a scroll “down” will do.
  • name: the accessibility ID of an element to scroll to within the bounding element
  • predicateString: the NSPredicate of an element to scroll to within the bounding element
  • toVisible: if true, and if element is set to a custom element, then simply scroll to the first visible child of element

Following are some examples:

// scroll down then up
Map<String, Object> args = new HashMap<>();
args.put("direction", "down");
driver.executeScript("mobile: scroll", args);
args.put("direction", "up");
driver.executeScript("mobile: scroll", args);

// scroll to the last item in the list by accessibility id
args.put("direction", "down");
args.put("name", "Stratus");
driver.executeScript("mobile: scroll", args);

// scroll back to the first item in the list
MobileElement list = (MobileElement) driver.findElement(By.className("XCUIElementTypeScrollView"));
args.put("direction", "up");
args.put("name", null);
args.put("element", list.getId());
driver.executeScript("mobile: scroll", args);

mobile: pinch

     To pinch (described by a two-finger gesture where the fingers start far apart and come together) or to zoom (described by the inverse gesture where fingers start together and expand outward), use mobile: pinch, which calls XCUIElement.pinch under the hood. As with the other methods described so far, you can pass in an element parameter defining the element in which the pinch will take place (the entire application by default).

The only required parameter is scale:

  • Values between 0 and 1 refer to a “pinch”
  • Values greater than 1 refer to a “zoom”

An additional optional parameter velocity can be sent, which corresponds to “the velocity of the pinch in scale factor per second”. Following is an example:

// zoom in on something
Map<String, Object> args = new HashMap<>();
args.put("scale", 5);
driver.executeScript("mobile: pinch", args);

mobile: tap

The best way to tap on an element is using element.click(). So why do we have mobile: tap? This method allows for extra parameters x and y signifying the coordinate at which to click. The nice thing is that this coordinate is either screen-relative (if an element parameter is not included, the default), or element-relative (if an element parameter is included).

This means that if you want to tap at the very top left corner of an element rather than dead center. Following is an example:

// tap an element very near its top left corner
Map<String, Object> args = new HashMap<>();
args.put("element", ((MobileElement) element).getId());
args.put("x", 2);
args.put("y", 2);
driver.executeScript("mobile: tap", args);

mobile: doubleTap

 There’s more to tapping than single-tapping! And while you can certainly build a double-tap option using the Actions API, XCUITest provides a XCUIElement.doubleTap method for this purpose, and it could presumably have greater reliability than synthesizing your own action. In terms of parameters, you should send in either an element parameter, with the ID of the element you want to tap, or both an x and y value representing the screen coordinate you wish to tap. Following is an example:

// double-tap the screen at a specific point
Map<String, Object> args = new HashMap<>();
args.put("x", 100);
args.put("y", 200);
driver.executeScript("mobile: doubleTap", args);

mobile: twoFingerTap

    Not to be confused with a double-tap, a two-finger-tap is a single tap using two fingers! This method has only one parameter, which is required: good old element (it only works in the context of an element, not a point on the screen). Following is an example:

// two-finger-tap an element (assume element object already exists)
Map<String, Object> args = new HashMap<>();
args.put("element", ((MobileElement) element).getId());
driver.executeScript("mobile: twoFingerTap", args);

mobile: touchAndHold

    Many iOS apps allow a user to trigger special behavior by tapping and holding the finger down on a certain UI element. You can specify all the same parameters as for doubleTap (elementx, and y) with the same semantics. In addition you must set the duration parameter to specify how many seconds you want the touch to be held. Following is an example:

// touch and hold an element
Map<String, Object> args = new HashMap<>();
args.put("element", ((MobileElement) element).getId());
args.put("duration", 1.5);
driver.executeScript("mobile: touchAndHold", args);

mobile: dragFromToForDuration

     Another commonly-implemented app gesture is “drag-and-drop”. As with all of these gestures, it’s possible to build a respectable drag-and-drop using the Actions API, but if for some reason this doesn’t work, XCUITest has provided a method directly for this purpose. It’s a method on the XCUICoordinate class. Really, what’s going on is that we’re defining a start and an end coordinate, and also the duration of the hold on the start coordinate. In other words, we have no control over the drag duration itself, only on how long the first coordinate is held before the drag happens. Following are the required parameters:

  • element: an element ID, which if provided will cause Appium to treat the coordinates as relative to this element. Absolute screen coordinates otherwise.
  • duration: the number of seconds (between 0.5 and 6.0) that the start coordinates should be held
  • fromX: the x-coordinate of the start position
  • fromY: the y-coordinate of the start position
  • toX: the x-coordinate of the end position
  • toY: the y-coordinate of the end position

Following is an example:

// touch, hold, and drag based on coordinates
Map<String, Object> args = new HashMap<>();
args.put("duration", 1.5);
args.put("fromX", 100);
args.put("fromY", 100);
args.put("toX", 300);
args.put("toY", 600);
driver.executeScript("mobile: dragFromToForDuration", args);

Please try to practice all the touch gestured during your iOS automation using Appium.

Reference: Appium Pro

make it perfect !

Required Software Packages for Appium Automation on Mac OS

1_cDK_xkF4TfIFqAUl_ABv8w

      I observed that most of them are struggling to deal with automation for iOS in the MAC system even during the initial stage of the setup Software Package. Most of them are unaware of which package to be installed and how can be installed. The following table will help you easily configure the required software packages for automate iOS in MAC OS,

Sl. No. Software Packages What is the usage? How to install?
1 Homebrew Homebrew is a free and open-source software package management system that simplifies the installation of software on Apple’s macOS operating system 1. Open https://brew.sh/
2. Copy /usr/bin/ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”
3. Open terminal in MAC and paste above copied command. Click enter.
2 Node Node is an open-source, cross-platform JavaScript run-time environment that executes JavaScript code outside of a browser. Here npm will get from Node. 1. Open terminal.
2. Type brew install node. Click enter
3 Appium server Appium server listen the client commands and response with actionson device side. 1. Open terminal.
2. Type npm install -g appium. Click enter
4 Appium Doctor Attempts to diagnose and fix common Node, iOS and Android configuration issues before starting Appium 1. Open terminal.
2. Type npm install -g appium-doctor. Click enter
5 Carthage It was the first dependency manager to work with Swift; in fact, Carthage itself is written in Swift! It exclusively uses dynamic frameworks instead of static libraries – this is the only way to distribute Swift binaries that are supported by iOS 8 and up. 1. Open terminal.
2. Type brew install carthage. Click enter
6 libimobiledevice In order to run our tests on real devices, we need to install libimobiledevice dependency 1. Open terminal.
2. Type brew install libimobiledevice. Click enter
7 mobiledevice mobiledevice is a command line utility for interacting with Apple’s Private Mobile Device Framework. It can be used for automating some tasks like installing and uninstalling apps on your iPhone/iPad without having to manually do it via Xcode or iTunes. You don’t need a jailbroken device! 1. Open terminal.
2. Type brew install mobiledevice. Click enter
8 Authorize-ios authorize-ios is a utility which pre-authorizes to run UIAutomation scripts on iOS devices. We need this tool for running our tests on real devices. 1. Open terminal.
2. Type npm install -g authorize-ios. Click enter
9 ios-deploy To deploy our apps to our devices by using ios-deploy. We can install and debug apps from the command line without using Xcode. 1. Open terminal.
2. Type brew install ios-deploy. Click enter
10 ideviceinstaller ideviceinstaller helps us to install, uninstall, upgrade, archive, restore and enumerate installed or archived apps. 1. Open terminal.
2. Type sudo xcode-select -r. Click enter
3. Type brew install ideviceinstaller. Click enter
11 ios_webkit_debug_proxy In order to access web views on real iOS devices, we need to install this tool. 1. Open terminal.
2. Type brew install ios-webkit-debug-proxy. Click enter
12 Maven It is used for building and managing Java-based projects by using dependencies. 1. Open terminal.
2. Type brew install maven. Click enter

     Please use all above mentioned packages prior to start scripting for iOS applications in MAC system.

make it perfect !