buttons

 

Appium Gestures-(Part-1)-How to Perform ‘FlingGesture’ based on element-Using W3C MobileGestures

“Fling” Gesture Using W3C Mobile Gestures API

Please go through the below video for complete details:

package demo;

public class FlingDemo {

private AndroidDriver driver;
WebElement element;
boolean canFlingMore;
int speedvalue;
@BeforeTest
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(100));

}

@Test
public void test() throws InterruptedException {
driver.activateApp("net.organicbazar");
Thread.sleep(3000);
driver.findElement(AppiumBy.androidUIAutomator("new UiSelector().className(\"android.widget.ImageView\").instance(2)")).click();
Thread.sleep(3000);
driver.findElement(AppiumBy.androidUIAutomator("new UiSelector().className(\"android.widget.ImageView\").instance(0)")).click();
Thread.sleep(3000);
element=driver.findElement(AppiumBy.androidUIAutomator("new UiSelector().className(\"android.widget.ImageView\")"));
//Fling to Right(direction)
System.out.println("Fling to Right(direction)");

canFlingMore=(boolean)((JavascriptExecutor)driver).executeScript("mobile: flingGesture",ImmutableMap.of(
"elementId",((RemoteWebElement)element).getId(),
"direction","right",
"speed",2500
));

Thread.sleep(3000);
//Fling to Left(direction)
System.out.println("Fling to Left(direction)");
speedvalue=(int)(7500*2.625);

((JavascriptExecutor)driver).executeScript("mobile: flingGesture",ImmutableMap.of(
"elementId", ((RemoteWebElement)element).getId(),
"direction","left",
"speed",speedvalue
));
Thread.sleep(3000);
//enhance fling right with loop
System.out.println("enhance fling right with loop");
speedvalue=(int)(500*2.625);
for(int i=1;i<6;i++) {
canFlingMore=(boolean)((JavascriptExecutor)driver).executeScript("mobile: flingGesture",ImmutableMap.of(
"elementId",((RemoteWebElement)element).getId(),
"direction","right",
"speed",2500
));
}
Thread.sleep(3000);
//enhance fling left with loop
System.out.println("enhance fling left with loop");

for(int i=1;i<6;i++) {
canFlingMore=(boolean)((JavascriptExecutor)driver).executeScript("mobile: flingGesture",ImmutableMap.of(
"elementId",((RemoteWebElement)element).getId(),
"direction","left"
));
}
}


@AfterTest
public void teardown() {

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

}

}

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

Complete Details “flingGesture” Command:

mobile: flingGesture : performs fling gesture on the given element/area.

Supported arguments:

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

left: The left coordinate of the fling bounding area

top: The top coordinate of the fling bounding area

width: The width of the fling bounding area

height: The height of the fling bounding area

direction: Direction of the fling. Mandatory value. Acceptable values are: up, down, left and right (case insensitive)

speed: The speed at which to perform this gesture in pixels per second. The value must be greater than the minimum fling velocity for the given view (50 by default). The default value is 7500 * displayDensity

Returned value:

The returned value is a boolean one and equals to true if the object can still scroll in the given direction

Example: // Java

boolean canFlingMore = (Boolean) ((JavascriptExecutor) driver).executeScript(“mobile: flingGesture”, ImmutableMap.of(

“elementId”, ((RemoteWebElement) element).getId(),

“direction”, “down”,

“speed”, 500

));

Sample Code:

//Fling to Right(direction)

WebElement element=driver.findElement(AppiumBy.androidUIAutomator(“new UiSelector().className(\”android.widget.ImageView\”)”));

System.out.println(“Fling to Right(direction)”);

canFlingMore=(boolean)((JavascriptExecutor)driver).executeScript(“mobile: flingGesture”,ImmutableMap.of(

“elementId”,((RemoteWebElement)element).getId(),

“direction”,”right”,

“speed”,2500

));

GitHub Link:

No comments:

Post a Comment