# 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);
```
