Automate iOS Gestures using Appium


A gesture in iOS is basically a hand/finger movement that results in some action on the device.

IOSTouchAction class in Appium:

Appium has designed a class called IOSTouchAction. By using the methods present in IOSTouchAction class, we can perform mobile gestures such as Tap, Long Press on iOS devices.

1) iOS Gesture : Scroll till Element

We can scroll the screen on iOS devices using executeScript method which accepts scroll script and HashMap object. The script used in which scrolling should be performed till the element in iOS devices is “mobile:scroll” and the HashMap object includes values such as “scroll direction” and “Attributes values” to perform scroll action till the element.

Prerequisite for all iOS gestures:

Import the Desired Capabilities method to perform any actions of iOS Gestures.
IOSDriver driver = capabilities();

Note:
Since, we don’t have any direct method to perform iOS Gestures like Scroll or Swipe etc. We have to make use of the executeScript method from JavaScript to perform some actions of iOS Gestures.

Syntax:

driver.executeScript(script, args);

script : “mobile:scroll”
args:  Need to provide an argument of HashMap object where scroll action should perform till the specified element.

Code:

IOSDriver driver = capabilities();
HashMap<String,Object> scrollObject = new HashMap<>();
scrollObject.put(“direction”, “down”);
scrollObject.put(“Attribute name ”, “Attribute value”);

driver.executeScript(“mobile:scroll”, scrollObject)

2) iOS Gestures: Picker Wheel

Picker Wheel is an iOS gesture and we can perform scroll action on Picker wheel for setting a date, time or any value. We can Automate Picker wheel by using the “sendKeys” method.

The sendKeys method will set the assigned value to a specific element.

Sample Source code:

<XCUIElementTypePickerWheel name = “Blue color component value”>
accessibility id = Blue color component value
<XCUIElementTypePickerWheel name = “Red color component value”>
accessibility id = Red color component value
<XCUIElementTypePickerWheel name = “Green color component value”>
accessibility id = Green color component value

Syntax:

IOSDriver driver = capabilities();
driver.findElementByAccessibilityId(“accessibility id”).sendKeys(“value”);

Code/Example:

driver.findElementByAccessibilityId(“Blue color component
value”).sendKeys(“4”);
driver.findElementByAccessibilityId(“Red color component
value”).sendKeys(“21”);
driver.findElementByAccessibilityId(“Green color component
value”).sendKeys(“2”);

3) iOS Gestures: Sliders

Sliders is an iOS gesture and we can Automate this using Appium.

Sample Source code:

<XCUIElementTypeSlider>
value = 38%

Syntax:

IOSElement slider = (IOSElement)driver.findElementByXPath(“xpath”);
slider.setValue(“String value”);

Code :

IOSDriver driver = capabilities();
IOSElement slider =
(IOSElement)driver.findElementByXPath(“//XCUIElementTypeSlider”);
slider.setValue(“0.38%”);

Note:

a) We need to cast findElement to the IOSElement to automate the slider by using the “setValue” method which is available in IOSElement.
b) setValue will accept the value from 0(minimum) to 1(maximum), which means to set value or slide to 38% we need to pass values as “0.38%” in the setValue method.

4) iOS Gestures: LongPress

Long Press is an action performed on a Mobile element by tapping and holding for specific time periods on iOS devices.

Sample Source Code:

<XCUIElementTypeStaticText name = “Long tap”>

Syntax:

MobileElement e = (MobileElement)driver.findElementByName(“value”);
IOSTouchAction touch = IOSTouchAction(driver);
touch.longPress(longPressOptions().withElement(element(e))).withDuration(ofSeconds(time in Seconds)).release().perform();

Sample Code:

IOSDriver driver = capabilities();
MobileElement e = (MobileElement)driver.findElementByName(“Long tap”);
IOSTouchAction touch = IOSTouchAction(driver);
touch.longPress(longPressOptions().withElement(element(e))).withDuration(ofSeconds(2)).release().perform();

Note:
The longPress method will expect longPressOptions for locating an element and the action would be performed on the Mobile Element. We need to add the below packages manually.

Packages:

import static io.appium.java_client.touch.LongPressOptions.longPressOptions;
import static io.appium.java_client.touch.offset.ElementOption.element;
import static java.time.Duration.ofSeconds;

5) iOS Gestures: Tap/Toggle

Tap is an action performed on Mobile Elements and on tapping an element on the screen, an action similar to a finger tap will be performed.

Sample Source Code:

<XCUIElementTypeSwitch>

Syntax:

MobileElement e = (MobileElement)driver.findElementByXPath(“xpath”);
IOSTouchAction touch = IOSTouchAction(driver);
touch.tap(tapOptions().withElement(element(e))).perform();

Sample Code:

IOSDriver driver = capabilities();
MobileElement tap =
(MobileElement)driver.findElementByXPath(“//XCUIElementTypeSwitch[1]”);
IOSTouchAction touch = IOSTouchAction(driver);
touch.tap(tapOptions().withElement(element(tap))).perform();

Note:

1. The tap method will expect tapOptions for locating an element and the action would be performed on the Mobile Element. We need to add the below packages manually.

Packages:

import static io.appium.java_client.touch.TapOptions.tapOptions;
import static io.appium.java_client.touch.offset.ElementOption.element;

Leave A Comment

Your email address will not be published. Required fields are marked *