# Swipe Actions

## Swipe Vertical on screen:

Create a reusable function and call when required multiple times in test script.

Using `size.width`  `size.height` and TouchAction

{% code title="helper.java" %}

```java
 public static void swipeVertical(double startPercentage, double finalPercentage, double anchorPercentage) throws InterruptedException {
        Dimension size = driver.manage().window().getSize();
        int anchor = (int) (size.width * anchorPercentage);
        int startPoint = (int) (size.height * startPercentage);
        int endPoint = (int) (size.height * finalPercentage);
        new TouchAction(driver).press(anchor, startPoint).moveTo(0, endPoint - startPoint).release().perform();
    }
```

{% endcode %}

To Swipe Vertical,  import above function from `helper.java` class

```
test.java

$ helper.swipeVertical(0.9, 0.1, 0.5);
```

## Swipe Horizontal on screen:

Create a reusable function and call when required multiple times in test script.

Using `size.width`  `size.height` and TouchAction

{% code title="helper.java" %}

```java
public static void swipeHorizontal(double startPercentage, double finalPercentage, double anchorPercentage) {
        Dimension size = driver.manage().window().getSize();
        int anchor = (int) (size.width * anchorPercentage);
        int startPoint = (int) (size.height * startPercentage);
        int endPoint = (int) (size.height * finalPercentage);
        new TouchAction(driver).press(startPoint, anchor).moveTo(endPoint - startPoint, 0).release().perform();
    }
```

{% endcode %}

To Swipe Horizontal, import above function from `helper.java` class

```
test.java

$ helper.swipeHorizontal(0.9, 0.01, 0.5);
```

## Swipe using TouchAction perform of screen:

Create a reusable function and call when required multiple times in test script.

{% code title="helper.java" %}

```java
 public static void swipeUsingPerformTouchAction(AndroidElement element) {
        driver.performTouchAction(new TouchAction(driver).press(element)
                .moveTo(0, element.getLocation().getY()).release().perform());
    }
```

{% endcode %}

To Swipe using TouchAction perform, import above function from `helper.java` class

```
test.java

$ helper.swipeUsingPerformTouchAction(element);
```

## Swipe using Elements of screen:

Create a reusable function and call when required multiple times in test script.

{% code title="helper.java" %}

```java
  public static void swipeUsingElements(WebElement element1, WebElement element2) {
        TouchAction action = new TouchAction(driver);
        action.press(element1).waitAction(Duration.ofSeconds(2)).moveTo(element2).release();
    }
```

{% endcode %}

To hide keyboard call below function from `helper.java` class

```
test.java

$ helper.swipeUsingElements(sourceElement, destinationElement);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://vinayak-titti.gitbook.io/appium-cookbook/recipes/swipe-actions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
