diff --git a/src/main/java/io/appium/java_client/ios/IOSTouchAction.java b/src/main/java/io/appium/java_client/ios/IOSTouchAction.java index aa9736892..a7288b7a9 100644 --- a/src/main/java/io/appium/java_client/ios/IOSTouchAction.java +++ b/src/main/java/io/appium/java_client/ios/IOSTouchAction.java @@ -20,6 +20,7 @@ import io.appium.java_client.PerformsTouchActions; import io.appium.java_client.TouchAction; +import io.appium.java_client.ios.touch.IOSPressOptions; import io.appium.java_client.touch.offset.ElementOption; import io.appium.java_client.touch.offset.PointOption; import org.openqa.selenium.WebElement; @@ -68,4 +69,15 @@ public IOSTouchAction doubleTap(PointOption doubleTapOption) { parameterBuilder.add(action); return this; } + + /** + * Press action on the screen. + * + * @param pressOptions see {@link IOSPressOptions} + * @return this TouchAction, for chaining. + */ + public IOSTouchAction press(IOSPressOptions pressOptions) { + parameterBuilder.add(new ActionParameter("press", pressOptions)); + return this; + } } diff --git a/src/main/java/io/appium/java_client/ios/touch/IOSPressOptions.java b/src/main/java/io/appium/java_client/ios/touch/IOSPressOptions.java new file mode 100644 index 000000000..0720bd325 --- /dev/null +++ b/src/main/java/io/appium/java_client/ios/touch/IOSPressOptions.java @@ -0,0 +1,59 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.appium.java_client.ios.touch; + +import static java.util.Optional.ofNullable; + +import io.appium.java_client.touch.offset.AbstractOptionCombinedWithPosition; + +import java.util.Map; + +public class IOSPressOptions extends AbstractOptionCombinedWithPosition { + private Double pressure = null; + + /** + * It creates an empty instance of {@link IOSPressOptions}. + * + * @return an empty instance of {@link IOSPressOptions} + */ + public static IOSPressOptions iosPressOptions() { + return new IOSPressOptions(); + } + + /** + * Set the pressure value. This allows to simulate force/3D touch on + * devices, that support it. + * + * @param pressure the value to set. + * See the description of `force` property on Apple's UITouch class + * (https://developer.apple.com/documentation/uikit/uitouch?language=objc) + * for more details on possible value ranges. + * + * @return this instance for chaining. + */ + public IOSPressOptions withPressure(double pressure) { + this.pressure = pressure; + return this; + } + + @Override + public Map build() { + final Map result = super.build(); + ofNullable(pressure).ifPresent(x -> result.put("pressure", x)); + return result; + } +} diff --git a/src/test/java/io/appium/java_client/ios/IOSTouchTest.java b/src/test/java/io/appium/java_client/ios/IOSTouchTest.java index e8daa149f..852f177b7 100644 --- a/src/test/java/io/appium/java_client/ios/IOSTouchTest.java +++ b/src/test/java/io/appium/java_client/ios/IOSTouchTest.java @@ -1,9 +1,11 @@ package io.appium.java_client.ios; import static io.appium.java_client.MobileBy.IosUIAutomation; +import static io.appium.java_client.ios.touch.IOSPressOptions.iosPressOptions; import static io.appium.java_client.touch.TapOptions.tapOptions; import static io.appium.java_client.touch.WaitOptions.waitOptions; import static io.appium.java_client.touch.offset.ElementOption.element; +import static java.time.Duration.ofMillis; import static java.time.Duration.ofSeconds; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -36,6 +38,26 @@ public void tapTest() { assertEquals(driver.findElementByXPath("//*[@name = \"Answer\"]").getText(), "6"); } + @Test + public void touchWithPressureTest() { + IOSElement intA = driver.findElementById("IntegerA"); + IOSElement intB = driver.findElementById("IntegerB"); + intA.clear(); + intB.clear(); + intA.sendKeys("2"); + intB.sendKeys("4"); + + MobileElement e = driver.findElementByAccessibilityId("ComputeSumButton"); + new IOSTouchAction(driver) + .press(iosPressOptions() + .withElement(element(e)) + .withPressure(1)) + .waitAction(waitOptions(ofMillis(100))) + .release() + .perform(); + assertEquals(driver.findElementByXPath("//*[@name = \"Answer\"]").getText(), "6"); + } + @Test public void swipeTest() { MobileElement slider = driver.findElementByClassName("UIASlider"); Dimension size = slider.getSize();