How to Swipe Up or Scroll Up in Appium with example | Appium Mobile Automation
Lets learn how to swipe up or scroll up in android
mobile, In most of the time we use this action to scroll from top to bottom
page elements. Some times we will not find the elements on the screen,
before clicking or doing any action first we need to make sure that
element is visible.
Example:
I’m taking example of Api Demos app and swiping up
from bottom to up.
App used: Api Demos
Steps:1. Open Api Demos app
2. First we need find the xpath for “Views” menu :
Xpath for views menu is : //*[@text='Views']
3. Click on “Views”
4. In the views page perform swipe up action
Before swipe up screen looks like this :
5. First find the element locator from where you want to swipe
down:3. Click on “Views”
4. In the views page perform swipe up action
Before swipe up screen looks like this :
in the above screenshot, i'm taking "Gallery" xpath.
6. Find the element location - X axis and Y axis & provide how many pixels you want swipe up:
7. Then by passing x axis and Y axis you can call the swipe() method:
swipe(x, y, x, y1);
6. Find the element location - X axis and Y axis & provide how many pixels you want swipe up:
Point value = null;
value = driver.findElement(By.xpath("//*[@text='Gallery']")).getLocation();
int x = value.x;
int y = value.y;
int y1 = value.y+pixelsToSwipe;
7. Then by passing x axis and Y axis you can call the swipe() method:
swipe(x, y, x, y1);
8. Swipe method code:
public static void swipe(int fromX,int fromY,int toX,int toY) {
TouchAction action = new TouchAction(driver);
action.press(PointOption.point(fromX,fromY))
.waitAction(new WaitOptions().withDuration(Duration.ofMillis(3000))) //you can change wait durations as per your requirement
.moveTo(PointOption.point(toX, toY))
.release()
.perform();
}
9. Copy the below complete code for swipe up action and paste in
you class file:
package practise;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Point;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;
import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;
public class SwipeToUp {
static AndroidDriver<MobileElement> driver;
@Test
public void setup() throws InterruptedException, MalformedURLException {
DesiredCapabilities cap=new DesiredCapabilities();
cap.setCapability("deviceName", "emulator-5554");
cap.setCapability("udid", "emulator-5554");
cap.setCapability("appPackage", "io.appium.android.apis");
cap.setCapability("appActivity", "io.appium.android.apis.ApiDemos");
cap.setCapability("platformName", "Android");
cap.setCapability("platformVersion", "9.0");
driver=new AndroidDriver<MobileElement>(new URL("http://0.0.0.0:4723/wd/hub"), cap);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
Thread.sleep(5000);
driver.findElementByXPath("//*[@text='Views']").click();
Thread.sleep(5000);
swipeDown(250);
Thread.sleep(5000);
swipeUp(250);
}
public void swipeDown(int pixelsToSwipe) {
try {
Point value = null;
value = driver.findElement(By.xpath("//*[@text='Gallery']")).getLocation();
int x = value.x;
int y = value.y;
int y1 = value.y+pixelsToSwipe;
swipe(x,y1,x,y);
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
public void swipeUp(int pixelsToSwipe) {
try {
Point value = null;
value = driver.findElement(By.xpath("//*[@text='Gallery']")).getLocation();
int x = value.x;
int y = value.y;
int y1 = value.y+pixelsToSwipe;
swipe(x, y, x, y1);
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
public static void swipe(int fromX,int fromY,int toX,int toY) {
TouchAction action = new TouchAction(driver);
action.press(PointOption.point(fromX,fromY))
.waitAction(new WaitOptions().withDuration(Duration.ofMillis(3000))) //you can change wait durations as per your requirement
.moveTo(PointOption.point(toX, toY))
.release()
.perform();
}
}
10. Run the above as TestNg Test:
After swipe up : execution result of the screen
Console Output:
TestNg Output:
No comments:
Post a Comment
If any suggestions or issue, please provide