(part-3) — How to Perform “Scroll” Gesture — Scrolling : Based on Percent(0.25,0.50,0.75,1.0)

Please go through the below video for complete details:
Java Code:
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 Scrollbasedonpercent {
private AndroidDriver driver;
@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 {
WebElement element=driver.findElement(AppiumBy.androidUIAutomator("new UiSelector().className(\"android.widget.ScrollView\")"));
((JavascriptExecutor)driver).executeScript("mobile: scrollGesture",ImmutableMap.of(
"elementId",((RemoteWebElement)element).getId(),
"direction","down",
"percent",0.88)); //75% = 0.75,50% = 0.5,100%= 1.0
}
@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 on “mobile: scrollGesture” Command:
mobile: scrollGesture — performs scroll gesture on the given element/area.
Supported arguments
elementId: The id of the element to be scrolled. If the element id is missing then scroll bounding area must be provided. If both the element id and the scroll bounding area are provided then this area is effectively ignored.
left: The left coordinate of the scroll bounding area
top: The top coordinate of the scroll bounding area
width: The width of the scroll bounding area
height: The height of the scroll bounding area
direction: Scrolling direction. Mandatory value. Acceptable values are: up, down, left and right (case insensitive)
percent: The size of the scroll as a percentage of the scrolling area size. Valid values must be float numbers greater than zero, 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 5000 * displayDensity
Returned value
The returned value is a boolean one and equals to true if the object can still scroll in the given direction
Usage examples: // Java
boolean canScrollMore = (Boolean) ((JavascriptExecutor) driver).executeScript(“mobile: scrollGesture”, ImmutableMap.of(
“left”, 100, “top”, 100, “width”, 200, “height”, 200,
“direction”, “down”,
“percent”, 1.0
));
Sample Code:
WebElement element=driver.findElement(AppiumBy.androidUIAutomator(“new UiSelector().className(\”android.widget.ScrollView\”)”));
((JavascriptExecutor)driver).executeScript(“mobile: scrollGesture”,ImmutableMap.of(
“elementId”,((RemoteWebElement)element).getId(),
“direction”,”down”,
“percent”,0.88)); //75% = 0.75,50% = 0.5,100%= 1.0
GitHub Link:
No comments:
Post a Comment