buttons

 

How to Perform ‘PinchOpen & PinchClose’ Gesture on a image -Using W3C MobileGestures

“PinchOpen & PinchClose” gesture

Please go through the below video for complete details:

package demo;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.DeviceRotation;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.ScreenOrientation;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Pause;
import org.openqa.selenium.interactions.PointerInput;
import org.openqa.selenium.interactions.PointerInput.Kind;
import org.openqa.selenium.interactions.Sequence;import org.openqa.selenium.remote.FileDetector;
import org.openqa.selenium.remote.LocalFileDetector;
import org.openqa.selenium.remote.RemoteWebElement;
import org.openqa.selenium.remote.SessionId;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
import io.appium.java_client.AppiumBy;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidStartScreenRecordingOptions;
import io.appium.java_client.android.AndroidStopScreenRecordingOptions;
import io.appium.java_client.android.geolocation.AndroidGeoLocation;
import io.appium.java_client.android.nativekey.AndroidKey;
import io.appium.java_client.android.nativekey.KeyEvent;
import io.appium.java_client.android.options.UiAutomator2Options;
import io.appium.java_client.screenrecording.ScreenRecordingUploadOptions;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.net.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collections;
import java.util.HashMap;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class PinchDemo {

private AndroidDriver driver;

WebElement ele;
int speedvalue,speedvalue1;

@BeforeClass
public void setup() throws MalformedURLException {
UiAutomator2Options cap=new UiAutomator2Options();
cap.setPlatformName("android");
cap.setAutomationName("uiautomator2");
cap.setDeviceName("Pixel8Pro");


driver = new AndroidDriver(new URL("http://127.0.0.1:4723"), cap);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

}

@Test
public void test123() throws InterruptedException {

driver.findElement(AppiumBy.androidUIAutomator("new UiSelector().resourceId(\"com.google.android.documentsui:id/icon_thumb\").instance(3)")).click();

ele=driver.findElement(AppiumBy.id("com.google.android.apps.photos:id/video_player_controller_fragment_container"));

//Perform 'pinchOpenGesture' based on 'elementid,left,top,width,height,percent,speed' arguments
System.out.println("Perform 'pinchOpenGesture' based on 'elementid,left,top,width,height,percent,speed' arguments");

speedvalue=(int)(100*2.625);

((JavascriptExecutor) driver).executeScript("mobile: pinchOpenGesture",ImmutableMap.of("elementId", ((RemoteWebElement)ele).getId(),
"left",80,
"top",1079,
"width",1159,
"height",1890,
"percent",1.0,
"speed",speedvalue));

Thread.sleep(2000);

//Perform 'pinchCloseGesture' based on 'elementid,left,top,width,height,percent,speed' arguments
System.out.println("Perform 'pinchCloseGesture' based on 'elementid,left,top,width,height,percent,speed' arguments");

speedvalue1=(int)(1500*2.625);

((JavascriptExecutor) driver).executeScript("mobile: pinchCloseGesture",ImmutableMap.of("elementId", ((RemoteWebElement)ele).getId(),
"left",80,
"top",1079,
"width",1159,
"height",1890,
"percent",1.0,
"speed",speedvalue1));

Thread.sleep(2000);

driver.pressKey(new KeyEvent(AndroidKey.BACK));

//Perform 'pinchOpenGesture' based on 'elementid,percent,speed' arguments

driver.findElement(AppiumBy.androidUIAutomator("new UiSelector().resourceId(\"com.google.android.documentsui:id/icon_thumb\").instance(1)")).click();

ele=driver.findElement(AppiumBy.id("com.google.android.apps.photos:id/video_player_controller_fragment_container"));

speedvalue=(int)(900*2.625);

((JavascriptExecutor) driver).executeScript("mobile: pinchOpenGesture",ImmutableMap.of("elementId", ((RemoteWebElement)ele).getId(),
"percent",0.75,
"speed",speedvalue));

Thread.sleep(2000);

//Perform 'pinchCloseGesture' based on 'elementid,left,top,width,height,percent,speed' arguments
System.out.println("Perform 'pinchCloseGesture' based on 'elementid,left,top,width,height,percent,speed' arguments");

speedvalue1=(int)(1000*2.625);

((JavascriptExecutor) driver).executeScript("mobile: pinchCloseGesture",ImmutableMap.of("elementId", ((RemoteWebElement)ele).getId(),
"left",80,
"top",1079,
"width",1159,
"height",1890,
"percent",1.0,
"speed",speedvalue1));

Thread.sleep(2000);

driver.pressKey(new KeyEvent(AndroidKey.BACK));

//Perform 'pinchOpenGesture' based on 'elementid,percent' arguments

driver.findElement(AppiumBy.androidUIAutomator("new UiSelector().resourceId(\"com.google.android.documentsui:id/icon_thumb\").instance(0)")).click();

ele=driver.findElement(AppiumBy.id("com.google.android.apps.photos:id/video_player_controller_fragment_container"));

((JavascriptExecutor) driver).executeScript("mobile: pinchOpenGesture",ImmutableMap.of("elementId", ((RemoteWebElement)ele).getId(),
"percent",0.50
));

Thread.sleep(2000);

//Perform 'pinchCloseGesture' based on 'elementid,left,top,width,height,percent' arguments
System.out.println("Perform 'pinchCloseGesture' based on 'elementid,left,top,width,height,percent' arguments");


((JavascriptExecutor) driver).executeScript("mobile: pinchCloseGesture",ImmutableMap.of("elementId", ((RemoteWebElement)ele).getId(),
"left",80,
"top",1079,
"width",1159,
"height",1890,
"percent",0.50
));

}

@AfterClass
public void teardown() {

if(driver!=null) {
driver.quit();
}

}

}

Types of Gestures:

1. Long Press — Tap and hold the screen for a specified duration.

2. Swipe — A single swipe in any direction.

3. Scroll — Simulates scrolling action on the device.

4. Tap — A single tap on the screen.

5. Double Tap — Two quick taps on the screen.

6. Pinch — A two-finger gesture to zoom in or out.

7. Zoom — A two-finger gesture to magnify or reduce content size.

8. Drag and Drop — Move an object by dragging and dropping it at a new location

W3C Mobile Gestures Commands: JavaScript commands can be executed using driver.execute_script() , so it opens the door to leveraging the power of JavaScript to perform complex tasks like gestures on the page or app.

For Android: Have total 9 W3C Mobile Gestures Commands

mobile: clickGesture

mobile: doubleClickGesture

mobile: longClickGesture

mobile: pinchCloseGesture

mobile: pinchOpenGesture

mobile: swipeGesture

mobile: scrollGesture

mobile: dragGesture

mobile: flingGesture

For iOS: Have total 9 W3C Mobile Gestures Commands

mobile: tap

mobile: doubleTap

mobile: touchAndHold

mobile: twoFingerTap

mobile: dragFromToForDuration

mobile: dragFromToWithVelocity

mobile: rotateElement

mobile: tapWithNumberOfTaps

mobile: forcePress

mobile: scrollToElement

mobile: scroll

mobile: pinch

Complete Details on “pinchOpenGesture”;

mobile: pinchOpenGesture

This gesture performs pinch-open gesture on the given element/area. Available since Appium v1.19

Supported arguments

elementId: The id of the element to be pinched. If the element id is missing then pinch bounding area must be provided. If both the element id and the pinch bounding area are provided then the area is effectively ignored.

left: The left coordinate of the pinch bounding area

top: The top coordinate of the pinch bounding area

width: The width of the pinch bounding area

height: The height of the pinch bounding area

percent: The size of the pinch as a percentage of the pinch area size. Valid values must be float numbers in range 0..1, where 1.0 is 100%. Mandatory value.

speed: The speed at which to perform this gesture in pixels per second. The value must not be negative. The default value is 2500 * displayDensity

Example : // Java

((JavascriptExecutor) driver).executeScript(“mobile: pinchOpenGesture”, ImmutableMap.of(“elementId”, ((RemoteWebElement) element).getId(),

“percent”, 0.75

));

Sample Code:

//Perform ‘pinchCloseGesture’ based on ‘elementid,left,top,width,height,percent,speed’ arguments

System.out.println(“Perform ‘pinchCloseGesture’ based on ‘elementid,left,top,width,height,percent,speed’ arguments”);

speedvalue1=(int)(1500*2.625);

((JavascriptExecutor) driver).executeScript(“mobile: pinchCloseGesture”,ImmutableMap.of(“elementId”, ((RemoteWebElement)ele).getId(),

“left”,80,

“top”,1079,

“width”,1159,

“height”,1890,

“percent”,1.0,

“speed”,speedvalue1));

Complete Details on “pinchCloseGesture”;

mobile: pinchCloseGesture

This gesture performs pinch-close gesture on the given element/area. Available since Appium v1.19

Supported arguments

elementId: The id of the element to be pinched. If the element id is missing then pinch bounding area must be provided. If both the element id and the pinch bounding area are provided then the area is effectively ignored.

left: The left coordinate of the pinch bounding area

top: The top coordinate of the pinch bounding area

width: The width of the pinch bounding area

height: The height of the pinch bounding area

percent: The size of the pinch as a percentage of the pinch area size. Valid values must be float numbers in range 0..1, where 1.0 is 100%. Mandatory value.

speed: The speed at which to perform this gesture in pixels per second. The value must not be negative. The default value is 2500 * displayDensity

Example: // Java

((JavascriptExecutor) driver).executeScript(“mobile: pinchCloseGesture”, ImmutableMap.of(“elementId”, ((RemoteWebElement) element).getId(),

“percent”, 0.75

));

Sample Code:

//Perform ‘pinchCloseGesture’ based on ‘elementid,left,top,width,height,percent,speed’ arguments

System.out.println(“Perform ‘pinchCloseGesture’ based on ‘elementid,left,top,width,height,percent,speed’ arguments”);

speedvalue1=(int)(1000*2.625);

((JavascriptExecutor) driver).executeScript(“mobile: pinchCloseGesture”,ImmutableMap.of(“elementId”, ((RemoteWebElement)ele).getId(),

“left”,80,

“top”,1079,

“width”,1159,

“height”,1890,

“percent”,1.0,

“speed”,speedvalue1));

GitHub Link:


No comments:

Post a Comment